derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
734 stars 35 forks source link

Change Delay Seconds from transition programmatically #41

Closed AlexTemina closed 10 months ago

AlexTemina commented 10 months ago

Hi!

I have a very simple chart with 3 states, and I just want to move from one state to another (randomly) after some delay. Which means I should just use the "Delay Seconds" and a guard expression that checks if a random number is the correct one, which will tell the next state.

The problem I have is that I want that delay seconds to change randomly also, so every time I change states, I want this delay to be different to give some organic feeling.

Is there a way to do this? or should I use an external timer and then call the event to change state? Thanks!

BadBoyGodot commented 10 months ago

Hi Alex, you may dynamically set the delay_seconds property for a transition whenever a state is entered. This will allow you to have a varying delay time for each state transition, adding the "organic feeling" you're aiming for. connect the on_state_entered signal on your logic handling node, and use this

func _on_A_state_entered():
    # Replace this with the actual path to your Transition node within the State node
    var transition_node = $PathToYourTransitionNode
    # Generate a random delay between 0 and 5 seconds
    transition_node.delay_seconds = randf() * 5.0
AlexTemina commented 10 months ago

Ah nice! I was expecting to also get the transition programmatically, but I guess I can do it this way. Thanks!

derkork commented 10 months ago

You can also use a scene unique node the access the transition so your code will not break when you move the node around (https://docs.godotengine.org/en/stable/tutorials/scripting/scene_unique_nodes.html).