• 2D
  • Failing to call a function from the root node's script

Hi there, I have a problem since I decided to kinda centralize some data from my scene. As you can see below, I have my root node called GameScreen It has a script attached which for now just contains some important variables for my game and that other nodes should be able to access and eventually modify. For example, here is the code for the Circle node which draws a simple circle in the middle with the desired radius

extends Node2D

var radius = 0
export var segments = 64

func _ready():
	position = $GameScreen.getViewCenter()

func _process(delta):
	radius = $GameScreen.getPlayAreaRadius()

func _draw():
	drawCircle()

func drawCircle():
	var circlePoints = Array()
	var stepAngle
	for i in range(segments + 1):
		stepAngle = (float(i) / segments) * deg2rad(360)
		circlePoints.append(Vector2(cos(stepAngle) * radius, sin(stepAngle) * radius))
	for i in range (segments):
		draw_line(circlePoints[i], circlePoints[i + 1], Color(0.5, 0.5, 0.5), 4, true)

You can see that in the _ready() and the _process() functions, I am trying to get the values of the variables storing the center of the view and the radius of the area where the game takes place through the getViewCenter() and the getPlayAreaRadius() functions defined in the GameScreen's script. I've replaced $Gamescreen with everything I know to get access to its functions (get_node(../GameScreen), get_tree().get_root(), GameScreen) but every time Godot can either not find the node or returns null

The problem it causes is that it launches the game but only a black window shows up and can't be closed unless I kill the process

I'm running out of ideas

Someone please help

If the script are attached to Circle node, you can get root node by calling get_node("../GameScreen") (just as you said, but with quotes) or $"../GameScreen" (again, the quotes).

Apart from that, you need to make sure the functions you are calling on GameScreen node do exist. If that doesn't help, please post a screenshot with the errors on debugger.

I've replaced the $GameScreen with $"../GameScreen" and checked that the functions are correct (and they do) but I still get the following errors

Wait, if you put the $ sign in front there doesn't that = self.? In other words wouldn't godot be looking for 'GameScreen' from amongst the children of 'GameScreen'? :/

Ah the script is attached to the Circle?

maybe try

extends Node2D
export (NodePath) var gs
var radius = 0
export var segments = 64
func _ready():
	position = gs.getViewCenter()
#    position = $GameScreen.getViewCenter()
func _process(delta):
	radius = gs.getPlayAreaRadius()
#    radius = $GameScreen.getPlayAreaRadius()
func _draw():
    drawCircle()
func drawCircle():
    var circlePoints = Array()
    var stepAngle
    for i in range(segments + 1):
        stepAngle = (float(i) / segments) * deg2rad(360)
        circlePoints.append(Vector2(cos(stepAngle) * radius, sin(stepAngle) * radius))
    for i in range (segments):
        draw_line(circlePoints[i], circlePoints[i + 1], Color(0.5, 0.5, 0.5), 4, true)

Then in the inspector for Circle node you should get a property/widget to select/load the node you want.


Alternatively I think you might have to do what you did previously(edit: I mean what @arthur told you to do) but in _onready(): instead of _ready():

So I've been busy those past days so I haven't been able to test your tips. Now I've tried the NodePath way which didn't work so I've come back to the original way but replaced ready by onready. The only thing that it did is that I now have the same error but in the process function since the onready function doesn't seem to be called before the process one (plus I didn't have autocompletion for onready so I don't think this is an existing function). I will provide the whole project so you can eventually find something that comes from a part of the code I didn't suspect

One problem could be that other objects inherited from(in this case gamescreen) have necessary code in _ready() still. I've downloaded the project and will take a closer look later though.

Yeah I'm just remembering wrong and onready was something in version 2 not 3. And I remembered wrong what it actually was. Ok, so using get_parent() instead of everything previously mentioned works as you seem to expect.

snippet from Ball.gd

func _ready():
	viewCenter = get_parent().getViewCenter()
	reset()

Thank you. This solved my problem: well most of it but I think I can figure out the rest by myself