+1 vote

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)
Godot version 3.3
in Engine by (942 points)
edited by

1 Answer

0 votes
Best answer

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.

by (8,528 points)
selected by

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

Yes it will be something like.

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

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?

Looks like it.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.