legion icon indicating copy to clipboard operation
legion copied to clipboard

How to sequence events, one after another?

Open canadaduane opened this issue 5 years ago • 2 comments

Looking for advice on this. How would you wait for an animation sequence and then trigger something, then trigger another thing, and so on?

For example, let's say the player wants to throw a fireball at an enemy. It should cast a spell with an initial player animation, then create the fireball entity, then the fireball should follow the target pointed at by the player initially, then it should explode and make damage to all the entities around the target.

A comment that got me thinking about this: "I really, really like to code all that stuff in one place in code that looks something roughly similar to that sentence above. But usually [with ECS systems in general] I end up with 5 systems, 5 components, everything decoupled and there is no way to see how the fireball will actually fly, without knowledge of all the systems in the game."

canadaduane avatar Jan 26 '20 19:01 canadaduane

I really, really like to code all that stuff in one place in code that looks something roughly similar to that sentence above.

I would think deeply about the answer to this question, because ECS thinks the answer is you should not, it doesn't stop you from doing that but it certainly encourages you to do something else.

It encourages you to instead think about what sharable bits do you have. For instance all spells cast by the player have a casting animation, sure it will be a different one but you probably don't need each spell to have code to handle that, just some data that is referenced.

Next you have a missile, depending on your game there are different ways to handle their path, but again many games end up with quite complex logic to handle them and they want to share calculating the flight path with different kinds. Note that it wouldn't be as obvious if the logic should be shared if it wasn't a missile but was a breath weapon.

Finally you have an impact, who got hit, how much damage was done will need to be determined again a question of how often those calculations will be done and how best share them is needed.

I will point out though that sharing everything isn't mandatory at all. Choosing what to share and what not to share can help simplify your life a lot. If you are making a game whose combat looks like Final Fantasy 6 you could probably get away without having a full blown missile system for instance.

Now that I explained why you don't want that sentence I will prove myself wrong. Often you do want that sentence just not directly in code. There is no need to have a function (or even group of functions) that could have that as a header and it make logical sense. Instead what you can do is have that sentence be data.

How you transform that sentence into data is basically an unresolved question best I can tell (I have seen so many solutions). The most common are a scripting engine of some kind where you describe what actions happen when the ability is used at each step and relying on the underlying systems to poll you for changes when needed. A less flexible but easier approach is to have a semi-flexible data structure for abilities. The simplest form be "Fireball is a missile ability with AoE + damage that has animations cast, mid-air and impact" where code blocks denote "fields". I am being super vague on how you would lay that out because there are so many ways to do so.

After all that talking (sorry I tend to over-explain when I don't have any history of cues for knowledge) I will point out that isn't all fundamentally necessary. You don't need each system to be a tiny bit of code, you can totally have a Fireball system that handles each step. As a simple example you can have a FireballState component that has (or is) an enum of what part you are in and detects if that needs to change. Then it can change whatever needs to be changed about the fireball entity (or source/target entities if needed) to allow graphics to work.

As a final note you should find a better place to ask about your architecture choices since nothing about your question is related to maintenance of the project at all.

Guvante avatar Feb 03 '20 00:02 Guvante

To add to Guvante's excellent response, someone recently created a Discord server for game devs who are using Rust. That'd probably be a good place to discuss architecture questions like this one. invite link

OliverUv avatar Mar 07 '20 05:03 OliverUv