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.
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?
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!!