How do I "trigger once" or "one shot" in Gdscript?

alextroalextro Posts: 6Member

So in Construct there is a condition system called "trigger once". Is there something similar to invoke that "one shot" command?

Answers

  • raccoonraccoon Posts: 8Member
    Answer ✓

    Yes. You'll use CONNECT_ONESHOT in the signal you're using to trigger some action.

    For example, if you're waiting for a signal called call_action from a node that you defined in a variable called emitter in the node who'll see that signal, you'll do:

    emitter.connect("call_action", self, "_function_to_call", [], CONNECT_ONESHOT)
    

    That'll call the _function_to_call when the emitter sends the call_action signal, and then the CONNECT_ONESHOT will automaticaly disconnect that signal from the node.

    If you need more information about signals, take a look at this:
    http://docs.godotengine.org/en/latest/reference/gdscript.html#signals

    Hope it helps :)

  • alextroalextro Posts: 6Member

    Another question; Is that the only way to execute the command? What if I only need to press a key then force it executed command only once until the key released.

  • CalinouCalinou Posts: 161Admin Godot Developer
    Answer ✓

    @alextro said:
    Another question; Is that the only way to execute the command? What if I only need to press a key then force it executed command only once until the key released.

    Godot 3.0 onwards will have Input.is_action_just_pressed() and Input.is_action_just_released(), which will allow triggering code just once after a key is pressed or released.

  • alextroalextro Posts: 6Member

    Cool. That would be more handy and straight forward. I heard a lot of good things for upcoming Godot 3.

  • RayekRayek Posts: 21Member

    Andreas explains how to setup a simple key check script in his third and fourth tutorial:

  • alextroalextro Posts: 6Member
    edited March 2017

    @Rayek said:
    Andreas explains how to setup a simple key check script in his third and fourth tutorial:

    I understand the keypress method and boolean check,but that will only suite certain scenarios (ground checking for the video case). Say I make fixed grid movement for a character and has a boolean "move". After a key pressed then the character moves only to one grid and the boolean was in true state now. How do I know when the key was released, so the "move" boolean will set back to false state?

  • RayekRayek Posts: 21Member

    You'd check for the input_states class that would return the output_state "1" for "just pressed", set the move boolean to true, move one block, and after movement is finished set the move boolean to false. In your move function you would check whether the move boolean is set to false before allowing movement with the output_state "1".

    Or am I missing something here?

  • alextroalextro Posts: 6Member

    That would be make a loop endlessly while the key still not released since there is no way to make output_state to 0.

  • raccoonraccoon Posts: 8Member

    You can also use two signals on the button. The pressed and the released and use some variable to trigger true or false and control your actions when these events happen. I do not know if I completely understood the problem you're trying to solve, sorry if I misunderstood.

  • alextroalextro Posts: 6Member
    Answer ✓

    Well I aimed for keyboard input and might be different from button purpose. At least I got Space-Run demo to study the case for now. Thanks to the contributor & author.

Sign In or Register to comment.