on screen control buttons

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Help me please

Hi
I just want to ask that how can I make on screen controls for RPGs
I have four buttons (i.e. up, down, left, right and shoot). I don’t have any problem to shoot but I don’t know how will I make my player moveable by these buttons.
I am using this code to move my player

extends KinematicBody2D

export (int) var speed = 200

var velocity = Vector2()

func get_input():
    velocity = Vector2()
    if Input.is_action_pressed("ui_right"):
        velocity.x += 1
    if Input.is_action_pressed("ui_left"):
        velocity.x -= 1
    if Input.is_action_pressed("ui_down"):
        velocity.y += 1
    if Input.is_action_pressed("ui_up"):
        velocity.y -= 1
    velocity = velocity.normalized() * speed

func _physics_process(delta):
    get_input()
    velocity = move_and_slide(velocity)
:bust_in_silhouette: Reply From: exuin

You need to use signals. You can read the docs I linked or look up a different tutorial. The basic idea is that you connect the pressed signal of the buttons to a function that changes the velocity of the player character.

can I emit such signal that when he clicks the button then “ui_up” is pressed signal is emitted

Help me please | 2021-06-12 15:37

Yes it will be something like.

func _on_up_button_pressed():
    Input.action_press("ui_up")

Wakatta | 2021-06-12 15:43

That’s not quite it. See this: Using InputEvent — Godot Engine (stable) documentation in English

exuin | 2021-06-12 15:49

Means

var ui_right = InputEventAction.new()
# Set as move_right, pressed.
ui_right.action = "ui_right"
ui_right.pressed = true
# Feedback.
Input.parse_input_event(ui_right)

Am i right?

Help me please | 2021-06-12 16:21

Looks like it.

exuin | 2021-06-12 17:53