Godot does not let me access the position

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

Why godot doesn’t let me access the position of the input event(event.position) of the _input() function after I enter this condition: if event is InputEventScreenTouch or if event is InputEventScreenDrag ?

How doesn’t it let You ? What does it say, any errors ?

Inces | 2022-01-11 17:03

I have written it in the question details. When I write: if event is InputEventScreenTouch or if event is InputEventScreenDrag ; after this instruction, the script or the class “TouchScreenButton” doesn’t let me access to the event position(i.e. when I press the left mouse button), that is it doesn’t let me write “event.position”

Peppe | 2022-01-11 18:20

like it physically paralyses your hand so You can’t type “position” ? :slight_smile: I don’t think so.
You must mean that it crashes when You run this code. If so - it will show You error text in console. Post it.
Or could You mean, that compiler doesn’t help You in finishing sentence of Input.position ? This can happen in many situations, when compiler loses track of what class is reference. Ignore it, try to run code, only error will show you the truth.

Inces | 2022-01-11 18:45

So as you say, can it happen that the compiler doesn’t recognize the reference class so that I can access the attributes and methods of that class that I instantiated?

Peppe | 2022-01-11 18:49

Thanks for your explanation about the compiler. Basically even if it doesn’t give me automatic compilation of an attribute or function, the important thing is to see if the console gives errors. Anyway… I tried printing the location in the console. It worked!

Peppe | 2022-01-11 19:01

Exactly :). In your code it propably happened because You used “IS” keyword with OR. This keyword checks for class, so i guess compliller assumed, that input could be none of those 2 classes. But compiller gets lost every now and then. “AS” keyword can be helpful sollution for lot of cases. You can introduce a variable and use AS after it, to “remind” compiller what class it is, and how should it help You finish your sentences. For example

var bomb as RigidBody
get_node("CollisionShape") as CollisionShape2D 

Inces | 2022-01-11 19:47

Thanks for your explanation :slight_smile: . Anyway, it doesn’t read the keyword “as”, but “is” does.

Peppe | 2022-01-11 19:56

nono, I am talking aboiut two different keywords.
IS checks for class, returns true or false :

 if myvar is RigidBody :

AS is added to variable to force compiller to bahave, like this variable is of certain class. If You wrote :

mywar = get_node("boom") as RigidBody

compiller will finish your sentences, just like myvar would be of class rigidbody, even if in reality it is another class.

Inces | 2022-01-11 22:08

So with the last operation you force the “boom” node to be of type or class rigidbody and assign it to the variable “myvar”?

Peppe | 2022-01-13 07:09

You force editor to treat it as it would be class RigidBody, but real class is not changed. This is useful when , for example, node doesn’t have child “boom” at the start of project, it is added in some conditions later on runtime, but You have functions that refer to this child in your code. This way You can let editor know, that when this get_node(“boom”) will exist, it will be of class RigidBody, and it will make referring to this node easier, and “let” you use apply_impulse, add_torque and other rigid body stuff. However if You write RigidBody methods but lie to editor and actually “boom” will be another class, it will crash with error :slight_smile:

Inces | 2022-01-13 10:14

I read in passing. I thank you anyway for the explanation :slight_smile:

Peppe | 2022-01-13 11:13