0 votes
extends Area2D
#----------
var move_speed = 200
#----------
const SCREEN_WIDTH = 180
const SCREEN_HEIGHT = 320
#----------
func _input(event):
    if not event is InputEventScreenTouch:
        return
    if event.pressed:
        var touch_pos = event.get_position()

func _ready():
    print(get("touch_pos"))

It doesn't print at func _read(): What am i doing wrong? i need to get the result from the touch_pos.

in Engine by (21 points)

1 Answer

+1 vote
Best answer

So, a few things...

First, the _ready function is fired immediately after the node is added to the scene tree. So, it'll run before the _input even is ever processed. So, even if your code was right (which it isn't), the variable wouldn't be set at the time the _ready function fires.

Regarding, how to access the same variable in multiple functions... The easiest way is to make the variable "global" to the script. That way, you can access it from anywhere in the local script.

To do that, just define the variable outside of any function. Then just reference it as needed from anywhere else in the script. So, for example:

var my_global # define the variable outside of any function

func _ready():
    my_global = 10 # assign a value to the global variable
    my_func1()
    my_func2()

func my_func1():
  print(my_global) # prints "10"

func my_func2():
    my_global += 5 # change the value
    print(my_global) # prints 15
by (21,864 points)
selected by
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.