Handling States In a Combat System

Hello Developers, this is my question for more experienced programmers.

I want to get your opinions on handling sates on a Character (e.g: player attacking, player stunned, player slowed).

The way I do it right now is:

local AddedSpeeds = {
   ["BasicAttack1"] = {
      Dura = .5,
      Priority = 3,
      WalkSpeedValue = 4,
   }
}

To explain, it adds an attack that was inflicted on the target to a table, loops through it and gathers data.
Same with attacking state, I add moves to a table and set a boolean to true, which I return upon checking.
Now I know this is probably an okay way of doing it; but it feels unclean to me and I feel like there is a better, and more efficient way of doing this. I want your opinion.
Please go into detail so I can understand as much as your view I can. Thanks for reading!

1 Like

I have no idea what each of the values is for. If I am correct and dura = duration, priority = which attack is more important when there is more than 1, and walk speed = the speed of the victim. then it is good. it may feel unclean, but it seems fine to me.

Yes but I’m sure people don’t use this way of scripting it because i developed it myself, when I was a lot less experienced as I am now, so I’m curious how other people are doing this.

1 Like
  1. I’d use a server-side module for only updating and checking values. This allowed me to update values to a character/player from other scripts and read them more easily.

  2. Another way I would do this is by creating a folder under a player and then putting values in there such as object values.

1 Like

Good tip but no way i’m using a folder, unless I’m trying to make the client read it itself. Though that’s something i want to know, would it be faster to :FireClient() with information for the stun, or update values in a folder and make the client read?

To be honest, because you will most likely be firing the client so many times this would affect the server. Imagine 12 players getting fired by the server every second don’t you think that would affect the server? Even though using folders can be tedious, reading info from the client wouldn’t affect the server if you care about server performance

To be honest if want a less tedious way just try the module method I was talking about but add an event where the client can ask the module for info

Storing state in ValueObjects under the player actually sends significantly more data through the network than passing player data through RemoteEvents. While the automatic replication is convenient, it sends all of the data to every client which is usually unnecessary. However, if you have your own networking system (or if you’re using OSS like Zap or Bytenet), manual replication becomes trivial and much more efficient.

What about your system feels unclean? I think you’re actually on the right track. Based on the description of your current system, it shares a few similarities with an entity component system (ECS) architecture, which is widely used in game development. Engines like Unity and Godot adopt many ECS principles.

Depending on how willing you are to modify some of your game’s core mechanics, you could go with an ECS approach. ECS architectures are designed to handle situations exactly like this - you create an entity, add components (blocks of data; tables) to it, and perform operations on components by retrieving an entity’s component(s) or looping through all the entities that contain some component. In this case, you’d have an entity that represents the player and contains components like health, status effects, and equipped items. If you’re interested, there is a great (albeit technical) devforum post that covers the basics of ECS as well as a library called Matter that does a lot of the heavy lifting for you. It can be a challenging pattern to learn, but I firmly believe that utilizing ECS along with other paradigms like OOP/FP can make adding any system to your game a breeze.

1 Like

I appreciate your reply so much, I’ll look into everything you said and show you what I came up with. Also the unclean part of my script is networking it, I’ll show you if you’re bothered enough to take a look, but for me you helped more than enough. Thank you again.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.