Accessing the y position of my player in another script

Godot Version

4.4.1

Question

Hey everybody! I just started with Godot (and programming in general) very recently and am running into an issue. I followed a tutorial online for a simple 2D platformer which went great and have since been trying to add random personal touches to it to get more familiar with things.

One such idea was changing the time it takes to respawn after death based on how high up the player is when they die (the death animation currently involves removing the collision from the objects underneath the player so that you fall of the screen, and my goal was to make it so that you would respawn relatively around when you fell out of the camera). In order to do this I was planning on making some kind of formula based on the players y position at the time of death to calculate what to set the timer to, but I’m having a hard time accessing the player’s y position outside of the player script.

Currently my killzone node is right below my player node (but not parented to it), and I’ve tried creating a variable of the player node by dropping it into the script with ctrl and various other methods like creating a function in the player script and trying to get the node in the killzone script, but nothing I’ve found seems to be working for what I would think would be a pretty easy task. The closest I’ve done was printing my y position every time I jumped through the player script, but I haven’t found any way to actually get the the information over into my killzone script to try to make it work.

I’m sure this is just something simple I’m missing, but the last couple hours of trying haven’t turned up any results yet so any help would be much appreciated. Thanks!

You need to somehow pass a reference of your Player node to the Killzone script. There are a couple of ways you can do that, e.g. you can create an exported variable in your Killzone script that you later fill in with a reference to the Player node by simply dragging the node into the field in the inspector. Then you can access the position of the player by calling player.position.y inside the script.

I still seem to be having an issue unfortunately. The code runs fine until the collision with the killzone is triggered and then it returns this “Invalid access to property of key “position” on a base object of type ‘Nil’” error. I tried swapping around the extends and the player variables to Node2d instead of the types they are but figured this wasn’t the issue and swapped them back. I also tried going straight to the process function and it returned a similar error but wouldn’t let the game run at all.

I suggested the exported variable solution as one of the options, because I didn’t know your current structure of the code.
As you shared your code now - it seems like you already have the reference to the player node, because it’s the body parameter of your _on_body_entered() method. You don’t need the exported variable at all, you can just access the player’s vertical position with body.position.y.
You need to be aware though that after you queue.free() the node, you might not be able to access the parameters of that node anymore.

1 Like

This seems to be working! It gives me coordinates relative to the player’s starting point instead of coordinates relative to the space, but I just subtracted the distance to the y-axis and that seems to work great. Thanks!

Any idea why referencing the player node/exporting the variable wasn’t working though, just for future reference?

Use global_position instead of position then.

Hard to tell without seeing how it was fully set up, but this error you got in general tells you that at the time of calling this line, the player reference was not assigned. So it was either never assigned, or it was removed at runtime somehow, or the node was removed.

Got it, thanks! Still haven’t quite figured out why this is the case here, but this other method works perfectly fine so I’ll look into it another time. I appreciate all the help!!

1 Like