How do I set an action inside the input function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By leotreasure
:warning: Old Version Published before Godot 3 was released.

I’m using the Platformer3d example and trying to make it so that when the screen is swiped left or right, the player turns.

I’ve tried:

event.set_as_action("move_right", true))

and

event.action_press ("move_right")

but it’s not moving the character.

:bust_in_silhouette: Reply From: mateusak

You’re using functions that doesn’t even exist. Look into this. The way actions work is; you go to the Input Map under Project Settings and assign a button to a certain action, then you can test if such action is pressed or not. So, you would use:

if event.is_action_pressed("action"):

So, assuming you want to turn the player right when you press “E”. You then go to the Project Settings, create a new action and assign the “E” key to it, let’s call this action “move right”. Then you just need to go to the script and check if that action is pressed and turn the camera:

if event.is_action_pressed("move right"):
   #turn player right

I am trying to trigger the action: “move left” from inside the input function when SCREEN_DRAG’s relative_x hits a certain point (swipe).

func _input(ie):
if ie.type == InputEvent.SCREEN_TOUCH:
	if ie.pressed && !touch_dragging:
		touch_dragging = true
	if !ie.pressed && touch_dragging:
		touch_dragging = false
		Input.action_release("move_left")
		Input.action_release("move_right")
		
if ie.type == InputEvent.SCREEN_DRAG:
	if touch_dragging:
		if ie.relative_x < -10:
			Input.action_press("move_left")
		if ie.relative_x > 10:
			Input.action_press("move_right")

It half works on the first swipe, but release doesn’t work and I get this error in the console:

C Error: Condition '!custom)action_press.has(p_action) ' is true
C source: main/input_default.cpp:454
C function: action_release

leotreasure | 2016-09-23 05:54

Basically you’re trying to make the player turn using action_press() to indirectly change the action state, right? That’s not recommended. Of course, you need to check somewhere if the “move_left” action is pressed, and then execute the code to turn. That said, you should do something like this:

func _input(event):
   if event.is_action_pressed("move_left"):
      turn(angle)
   elif event.is_action_pressed("move_right"):
      turn(angle)

   if event.type == InputEvent.SCREEN_DRAG:
      if touch_dragging:
         if event.relative_x < -10:
            turn(angle)
         if event.relative_x > 10:
            turn(angle)

func turn(angle):
   #turn

mateusak | 2016-09-25 03:26