[Help] Import/link another node on GDscript

Greetings everyone, good evening! It’s great to be back. I finally set aside some time to continue working on my project, but I’ve encountered an issue with the script. Despite having studied it a few times, I still feel more confident following tutorials.

Here’s the first problem I’ve come across. I followed this tutorial for a camera and got stuck at the part where I need to “link” the player node to the script. I’ve tried various methods (such as get_node(“Player”)), but none of them have worked. The only changes I made to the tutorial were to translate some of the variables and annotations into portuguese, my native language. As far as I can tell, the error is not related to the variable names.

However, whenever I run the code, I get the error “cannot call method ‘get_position’ on a null value.” I believe this is because the script is unable to “link” with the other node. All of the nodes are in the same scene, and they function individually (the camera without customization and the character’s movement). So, what should I try now?

Here are some code snippet - I think the issue is there.



The scene
image


Previous post, for some extra context.

P.S: I’m currently focusing on creating a transition within the same scene before moving on to multiple scenes and transitions. Any additional advice is always appreciated, as I’m still a beginner.

Looking at your scene tree, the get_node() should work. Although I would caution against using such code in that way.

Let’s call the scene with the world, camera scene, and player scene etc. the “main” scene.

I suspect this should run fine in the main scene, but if you run the camera scene by itself get_node will fail and return null.

They tutorial does this another way.

You need to export a variable target node

@export var targetNode:Node2D = null

Then in the inspector pane select the player node from the list.

2 Likes

Oh, thank you so much, man! I never would have figured it out on my own.

But I’m a bit curious about this. Are there any other reasons why it’s not recommended?

It makes an independent scene couple with another scene. It is no longer independent, and is the reason that made you have to came here.

Get node should be limited to use if it’s within a scene internally and the node is static.

Or if you have need to, but do the proper null checks.

I think it’s cumbersome to do null checks before interacting with an object, so I try and avoid ever letting that object reference be null by planning my code and organization.

Having an export object is for this design pattern is great as long as you set the property it should work. You can even have an editor warning to remind you.

2 Likes

Thank you so much!

1 Like

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