I started looking into custom events some days ago but i am not sure if i understood 100%. Maybe you can help. I have a lot of systems in Space Pirate that kind of needs "always" or atleast "every x seconds".
Examples:
- A terminal which checks if the actor is still touching it
- An enemy that needs to check where the player is and adjust its state
- Spreading fire (which checks where it could spread to next)
I understand how i setup custom events but i am not sure how that would save performance.
Lets stick to example 2:
Would it save performance if i put the "trigger custom event block" into "always/every x second" and the whole function into the custom events instead of having the whole thing in the "always/every x second"?
I've realized since last night that custom events aren't always the best solution. Most of the time, the code only
needs to be run under one condition, so placing it inside an
if (that condition is true) is a better choice.
Custom events don't save performance if they are executed every frame, as the game is still running all of the code 100 times/second. However, most of these tasks can be executed inside an "if" block which checks if the code needs to run. Let's take a look at your examples:
1. The player-checking code could be placed inside an
if (any of the player-moving keys are down AND the player was previously touching the terminal). That way, the code is only run when it needs to, which will save on performance. As the code only needs to run under one condition, it wouldn't necessarily be faster if put inside a custom event.
2. As the enemy would be constantly moving, AI things like this would probably be best in an Always event. If you want to save more on performance, you could consider using "do every 0.1 seconds", or something similar.
3. I don't believe this should happen 100 times/second, but it is a fairly regular task, so it would work best in a "do every X seconds" block. I assume that is what you have set up already.
So basically, the best way to accomplish these tasks depends on the situation. I see now that custom events aren't normally the best way; placing the code inside
if (this code needs to be run) works just as well.