Push key to do thing

Godot Version 4

Question

First thing first I am an OmegaBeginner so bear with me. As the title says I’m fidgeting with inputs, but I’m stuck atm. I would like to do something along these lines:

  • push key to do thing,
  • wait for next input,
  • push same key to do thing_2
  • wait for next input
    but so far I managed to achieve only the first part. This is the code I’m working with:
extends PathFollow3D


# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass



func _input(_event):
	var tween := create_tween().set_trans(Tween.TRANS_QUINT).set_ease(Tween.EASE_IN_OUT)
	
	var increment = .2
    var increment_2 = .5

	
	
	if Input.is_action_just_pressed('ui_page_up'):
		
		tween.tween_property(self, "progress_ratio", increment, 2).set_delay(.2)
	
	if Input.is_action_just_pressed('ui_page_up'):
		
		tween.tween_property(self, "progress_ratio", increment_2, 2).set_delay(.5)	

If I understand you correctly, you need to look into states. You want to do different things depending on some state.
First you need some kind of state variable (enums might be useful)

var state = 0

Then you can use this in the following way

if Input.is_action_just_pressed('ui_page_up'):
    if state == 0:
        # do your thing
        pass
    if state == 1:
        # do your second thing
        pass
    # increment state, so the next state will be called next time
    state += 1

Hope this helps ^^

At some point I’ve tried something like that, but perhaps I’ve done it wrong. I’ll check it, thanks.

same thing. It gets stuck after the first input.

can you post the code?

extends PathFollow3D


# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass



func _input(event):
	var tween := create_tween().set_trans(Tween.TRANS_QUINT).set_ease(Tween.EASE_IN_OUT)
	
	var state = 0
	var pr_1 = .25
	var pr_2 = .25
	
	
	if Input.is_action_just_pressed('ui_page_up'):
		
		if state == 0:
			tween.tween_property(self, "progress_ratio", pr_1, 2).set_delay(.2)
			state += 1
			print(state)
			pass
		
		if state == 1:
			tween.tween_property(self, "progress_ratio", pr_2, 2).set_delay(.2)	
			print(state)
			pass
		

here
perhaps is something about the func _input(event), but I’m clueless. If I bind the second input to a different key it works smoothly, but I’m not looking for that.

You need to define state as a global variable within your class. By putting var state = 0 within the _input function declares a new state variable each time this function is called, and thus the state is always 0.

Try this:

extends PathFollow3D

var state = 0
var pr_1 = .25
var pr_2 = .5

func _input(event):
	var tween = create_tween().set_trans(Tween.TRANS_QUINT).set_ease(Tween.EASE_IN_OUT)
	
	if Input.is_action_just_pressed('ui_page_up'):
		if state == 0:
			tween.tween_property(self, "progress_ratio", pr_1, 2).set_delay(.2)
			state += 1
			print(state)
		elif state == 1:
			tween.tween_property(self, "progress_ratio", pr_2, 2).set_delay(.2)	
			print(state)
3 Likes

Now it goes on his own, without waiting for the next input

Did you change if to elif for state == 1?

2 Likes

ok now it does it. Thanks. :yellow_heart:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.