Cant find nodes / node paths

I just can NOT get the distance to things working, it’s not really about the distance at this point, its about finding the nodes. I just cant find them???
Also i am kinda new to coding so it might just be a dumb mistake

var player_position = %player.global_transform.origin
var item_position = %testitem.global_transform.origin

just doesnt work for some reason :confused:

Godot Version

4.3

(also yes i tried it without the %, idk how it really works…)

In order to get a valid reference of a node, you can make use of a variety of methods:

  • Node.get_node() – requires a relative or absolute path to the node.
  • @export annotation – requires you to set a value for the field in the editor.
  • …and more.

Since you’re making use of the % shorthand, have you made sure to mark the player and testitem nodes as “unique names”?


Let me know if you have any more questions.

yeah i right clicked the nodes and pressed the % symbol, but it’s saying

Invalid access to property or key 'global_transform' on a base object of type 'null instance'.

image

Your error makes it clear that the root cause of your issue is due to an invalid object reference ('null instance').

Are you absolutely sure that your code uses the correct name of the nodes, and that the reference is valid at runtime?

EDIT: I know what your issue is. Since your variables are defined outside of any virtual function (e.g. _ready()) the tree is not yet built so you can’t get a reference to the objects unless you annotate the variables with @onready (see GDScript reference). That should fix it!

2 Likes

oh, thank you so much!

ok, now the variables work, but the distance sensing doesn’t. do you know how do i fix that?
image

EDIT: i forgor to update the variable. oops :confused:

EDIT 2: ok it doesnt really update so uh help?

Hmm… it seems you need to study how and when a program/script is executed.

Your variable definitions (for those defined in the scope of the class) are initialized and updated only when an instance of that class is created. When @onready is added to a variable definition, it’s the same as assigning a value to the variable in _ready() – it’s only done once.

If you wish to continuously update a variable, you have to make use of a consistently executing loop. In the case of Godot, you may use _process() to do this every frame.

oh sorry i forgot to put a screenshot, but i did put it in the _process function, but it still doesn’t work… maybe i’m just dumb?

image

You’re updating the distance but not the values used to compute it.

I would recommend defining player_position, item_position, and distance in the scope of _process() instead. You don’t need to remember the information from one frame to the next, therefore, no need to store it outside the function.

func _process(_delta: float):
  var player_pos = %player.global_position
  var item_pos = %testitem.global_position
  var distance = player_pos.distance_to(item_pos)
  # Your code...

Does that make sense?

oh thanks, ill try it and give you an update in a sec!

ok, it doesn’t give the null instance error, but the distance still doesn’t update for some reason. should I add this:

distance = player_pos.distance_to(item_pos)

or something?

Explain to me what you don’t understand. Did you read what I wrote?

i don’t understand how to make distance to update, so it detects when i’m 1 meter away from it. it just gives the same number every frame.

oh im so dumb i didnt update the positions :confused:
sorry

1 Like

okay, it still doesn’t work and i’m sure that the answer is gonna be simple and i’m just dumb, i’m just kinda new to this coding thing
image

You’re being too fast and rash in your corrections. Slow down and think for a second. Read through what I’ve said and research what you don’t understand.

Where is item_pos and player_pos defined; you don’t seem to be defining them in _process() so where are they?

oh yeah i didnt take the full screenshot of the function
image

so do you know how do i update the things???

I’ll tell you what. Make a new topic so someone else will help you. No one is going to look at this topic since it’s solved already.

You barely have any idea of how to write code and what everything means. There are so many things that I could start to check for you but that becomes really tiring. You need to learn the language of coding. It’s like talking to someone who barely knows English – it’s exhausting.

I hope it works out for you!

bruh‎ ‎ ‎ ‎ ‎

I don’t mean anything by it – I just don’t have the patience right now.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.