Trying to figure out how to implement a global array of "movement states"

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
wauterboi
Posts: 1
Joined: Wed Oct 12, 2022 9:37 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): EndeavourOS
Graphics Processor: nVidia with Vulkan support

Trying to figure out how to implement a global array of "movement states"

Post by wauterboi »

Hello,

I've been experimenting with replacing the player movement system in GZDoom for a while. My latest idea is to treat every kind of movement as a "movement state". Each movement state is a class and comes with two methods: "CheckConditions" to see if the player is in the movement state, and "MovePlayer" to actually process movement on the player.

There's a base movement state class that is abstract, and from there I would imagine "NoclipMoveState", "WaterMoveState", "GroundMoveState", and "AirMoveState". In the future, if I need to add a new MoveState, it would be as simple as making a new subclass of the base movement state and somehow referencing it in an array.

I've been able to get this system working by using a static array to hold all of the classes and the initialize an private array of instances - one instance for each MoveState. The problem I have is that this would be wasteful in the scenario that there are multiple players. I only really need to create a single global registry of all possible move states and make it available for reference/implementation in my PlayerPawn derivatives.

My current idea is that the player class should be responsible for iterating through the movement states and running its methods. The best candidate for implementation I can imagine is with a StaticEventHandler subclass to manage these movement states, but I've seen warnings about their usage in regards to networking, and I'm not well-versed enough to know if what I'm doing is a problem.

Sorry for not providing any code - I'm speaking more in a conceptual sense.
User avatar
Player701
 
 
Posts: 1651
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: Trying to figure out how to implement a global array of "movement states"

Post by Player701 »

wauterboi wrote: Sat Dec 10, 2022 1:38 amThe best candidate for implementation I can imagine is with a StaticEventHandler subclass to manage these movement states, but I've seen warnings about their usage in regards to networking, and I'm not well-versed enough to know if what I'm doing is a problem.
It depends on what exactly you want to do with your array and/or its items.

Think of it like this: each player gets their own copy of the event handler. Now, consider the following possibilities:
  1. Can the number and/or the order of items in the array change?
  2. Do the items in the array have some sort of state (e.g. non-local variables) that can affect their behavior?
If at least one of the above is true, then consider the following: Does a scenario exist in which the state of the array and/or its items may become different on each side? If the answer is "yes", then your setup is probably not multiplayer-safe. In this case, the next best option is to keep a separate copy of the array for each player, so that they do not interfere with each other:

Code: Select all

Array<MyClass>[MAXPLAYERS] MyArray;
To choose the correct array, use PlayerNumber(), and you should be fine - but I can't guarantee it without looking at the actual code.

Return to “Scripting”