TimeDelta working too fast?

Greetings,

In my game, I have a special key door, that when touched with the right key, will unlock other blocks touching it, creating a chain reaction

Here’s a demonstration:

That’s all fine and dandy, but here’s the issue: It’s opening the gate too fast

Here’s how the code works:

Basically, if the door is playing it’s “open” animation, spawn a temporary object called “keyDoorOpener” on all 4 sides, that inherit the values of the door it was spawned from (Color, region lock, etc).

If a door with the matching values touches the “keyDoorOpener,” then open that door too. Then the keyDoorOpener instantly deletes itself on the very next frame

You’ll notice that I use TimeDelta() for the Wait actions here. That’s because if the framerate dips even just a tiny bit, the chain reaction could yield inconsistent results (E.G. Doors of the wrong color/region get opened, the chain reaction stops midway through, etc)

So I use TimeDelta(), and now the results are always consistent. However, the doors are opening WAY too fast now, as if the Wait commands are being ignored entirely

Is there another way to set this up? Or is there a way to force the wait actions to cooperate?

Thanks

The wait action does not pause the game flow. I believe it creates a thread where the actions after the wait are held and then processed once the wait is completed.

This means the Repeat over KeyDoors is run normal time, no delay, and the last event in the repeat is run without being impacted by the wait actions. This means the KeyDoors are being opened without much, or any, delay.

Another possible way to do what you’re after is to add another frame to the open animation (the easiest way is to duplicate the first one) and set the speed to the delay you want between door opening in the chain, and then wait on the animation to finish (this occurs when the last frame has started playing, not when the last frame has finished) before creating the KeyDoorOpener.

So something like:

1 Like

I game it a try, and it seems to work fine, thanks!

I also found out that if you apply TimeDelta() to the Wait action, it records time by frames instead of seconds. So basically, instead of typing 1, you have to type 60

I also added a longer animation, and removed the Trigger Once, as it was causing issues. The keyDoorOpener object only exists for a single frame anyways, so removing Trigger Once won’t really have any noticeable impact on performance

Here’s the new code:

TimeDelta() is the time elapsed since the last game frame. So generally yes, one frame is 1/60th of a second, but it really depends on how much processing has gone on.


And ideally you should filter KeyDoors with the “Animation = "open"” condition and have the “Repeat for each KeyDoor” as a subevent. Then the program is only repeating over a small number of KeyDoor objects, rather than every KeyDoor every frame.

Although it’s only a slight processor improvement in this case, it is a good programming habit to get into. Filter as much as possible to reduce the number of objects you work with in an event.

1 Like

That’s a good point. I updated the code to be more efficient. Is this good?

The KeyDoors part is better.

The conditions of an event will filter objects to a subset of objects. The actions in that event are only performed on that subset. If you want to perform the same action on a subset of objects, just use conditions. There’s no need to use a Repeat for each object event.

So given this, you don’t need a Repeat in this event:


That change animation action is performed on only the KeyDoors that meet the conditions. Adding a Repeat only adds unnecessary processing time.

And again here:
image

If you just have the delete in an unconditional event, it’ll delete all of them. There’s no need to repeat over them, deleting each one individually.


Also, that last action can be merged into the event that sets the animation to “open”, like I have in my previous post:
image


The less processing your game has to do, the smoother it will run.