How do I use ui inputs to make a player press a button?

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

I am brand new to coding so I have no idea what I’m doing, I’m using GODOT 2.1.
I am trying to create a button the player can press in the game using “ui_accept” by moving over to it with an animated sprite.
This is the script for my player

extends Area2D

var screensize
export var speed = 175
var vel = Vector2()
onready var sprite = get_node(“Sprite”)
var anim = “walkies”

func _ready():

screensize = get_viewport_rect().size

set_fixed_process(true)
set_pos(Vector2 (100,300))

func _fixed_process(delta):

var input = Vector2(0,0)
input.x = Input.is_action_pressed("ui_right") - Input.is_action_pressed("ui_left")
vel = input * speed
set_pos(get_pos() + vel * delta)
if vel.x != 0:
	anim = "walkies"
else:
	anim = "idle"
sprite.play(anim)
if vel.x > 0:
	sprite.set_flip_h(false)
elif vel.x < 0:
	sprite.set_flip_h(true)
var pos = get_pos() * vel * delta

I have a button scene that can detect when the player is within range with this code (albeit the player sprite walks “behind” the button which is not ideal I would like to appear in front of the button) :

extends Area2D
var anim = “default”
var pos = Vector2(500,240)

func _ready():

pass

func _process_input():
pass

func _on_Area2D_area_enter( area ):

print(area)

How can I make it so that the player can walk into the button’s range, press “ui_accept”, and activate the animated sprite in the button scene? Eventually I’d like to have the button do something else but right now I just want to figure out how to get it to animate when the player is WITHIN range and then PRESSES the “ui_accept” key.
Thanks!

Sounds like you have this mostly solved already. Connect a function to the area_exit as well, and you will be able to toggle a boolean when an object enters/exits the area of interest. If you have more than one object that might be triggering the signal, you need to also check the object to see if it’s your player character. When you check if “ui_accept” has been pressed, check if your boolean is set, and if it is, play your animation.

P.S. If you indent all of the code in your question with four spaces, it will make it significantly more readable.

flesk | 2017-12-10 13:14