Replies: 8 comments
-
Why do you have two players in this case? Instead of reflection, I would use delegates with events subscription and broadcast them from world events. |
Beta Was this translation helpful? Give feedback.
-
I won't really have 2 players, this is just what the class I tested in happened to be. There's a low overhead to this reflection though as it's all done upfront so I'm just calling Invoke on a stored MethodInfo. |
Beta Was this translation helpful? Give feedback.
-
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Numerics;
using UnrealEngine.Framework;
namespace UnrealEngine.Tests {
public static class Main {
private static List<Player> _players = new List<Player>();
public static void OnWorldBegin() {
_players.Add(new Player($"{nameof(Player)}1"));
_players.Add(new Player($"{nameof(Player)}2"));
}
public static void OnWorldEnd() => Debug.ClearOnScreenMessages();
public static void OnWorldPrePhysicsTick(float deltaTime) {
foreach (var player in _players) {
if (player.TickEnabled)
player.OnTick(deltaTime);
}
}
}
public class Player : Actor {
private string _name;
public bool TickEnabled { get; set; }
public Player(string name = null, bool tickEnabled = true) : base(name) {
_name = name;
TickEnabled = tickEnabled;
Debug.AddOnScreenMessage(-1, 3.0f, Color.Orange, name);
}
public void OnTick(float deltaTime) => Debug.AddOnScreenMessage(-1, 3.0f, Color.Aqua, $"Tick {_name}");
}
} |
Beta Was this translation helpful? Give feedback.
-
Or if you prefer to keep data in contiguous memory: using System;
using System.Collections.Generic;
using System.Drawing;
using System.Numerics;
using UnrealEngine.Framework;
namespace UnrealEngine.Tests {
public static class Main {
internal static int uniqueIDs = 0;
internal static List<Player> _players = new List<Player>();
internal static List<bool> _playersTickEnabled = new List<bool>();
internal static List<string> _playersNames = new List<string>();
public static void OnWorldBegin() {
_players.Add(new Player($"{nameof(Player)}1"));
_players.Add(new Player($"{nameof(Player)}2"));
}
public static void OnWorldEnd() => Debug.ClearOnScreenMessages();
public static void OnWorldPrePhysicsTick(float deltaTime) {
for (int i = 0; i < _players.Count; i++) {
if (_playersTickEnabled[i])
_players[i].OnTick(deltaTime);
}
}
}
public class Player : Actor {
public int UniqueID { get; set; }
public Player(string name = null, bool tickEnabled = true) : base(name) {
UniqueID = Main.uniqueIDs++;
Main._playersNames.Add(name);
Main._playersTickEnabled.Add(tickEnabled);
Debug.AddOnScreenMessage(-1, 3.0f, Color.Orange, name);
}
public void OnTick(float deltaTime) => Debug.AddOnScreenMessage(-1, 3.0f, Color.Aqua, $"Tick {Main._playersNames[UniqueID]}");
}
} |
Beta Was this translation helpful? Give feedback.
-
A side note about action mappings. In case you will create a game with a local split-screen (Unreal supports that), then every player should have its own unique mappings, otherwise, controls will be messed up. |
Beta Was this translation helpful? Give feedback.
-
I have another method that gives the same result with no reflection using IObserver. Doing it this way with System.Reactive/System.Reactive.Linq allows me to have an event stream that I can merge/cherry pick |
Beta Was this translation helpful? Give feedback.
-
And yeah I noticed that you added action/axis mappings to PlayerInput so will use that if I do use local split screen. Probably best to just use this approach regardless |
Beta Was this translation helpful? Give feedback.
-
Feel free to use what makes you happy, the plugin was created specifically for that. 😜 |
Beta Was this translation helpful? Give feedback.
-
Can anybody see any immediate issues with the following?
Beta Was this translation helpful? Give feedback.
All reactions