Error global_position

Godot Version

4.2.1

Question

I can’t get rid of the error. Can someone help me?

The error says that your target is null. Assign a target or insert a check before that line to check if that target is not null.

1 Like

target hasn’t been assigned a valid object by the time _process runs.

Can you help me with that?

Set target to something before using add_child() to add your scene to the scene tree.

1 Like

Can you show me how to do it? I’m still a beginner.

I tend to use accessors, so I would add a setTarget(target) method to my class. I would then instantiate the object, call setTarget(target), then use add_child(myObject).

The _process() function won’t get called until after the scene is added to the scene tree via add_child(), so the time between instantiation and adding to the scene tree is your window for setting the target.

Sorry, but even after some research, I can’t fully understand it and can’t manage to do it. :frowning:

Show us what you have tried.


This is my status.
I haven’t managed to set the target to something before it is queried.

Are you running this in isolation from within the IDE (using F6)? If so, you will have to do more work to ensure that target has a valid reference to something that exists.

Create your target in the scene tree under RailCamera, then assign it to target from within the _ready() function.

In _process(), check to see if target is not null before trying to use it.

Most of the time, but it can also start permanently using ‘Run Project’.

I don`t understand this part.
I thought I had to use the node tree…

Is the check correct like this?

Yes, that part is correct.

In your rail camera scene click on the RailCamera node, in the Inspector you will see your @exported variables as properties. Assign the Camera2D to the target, play with lerp speed if you want.

I have selected the target. Now I am getting a new error… :frowning:

They changed it to follower.progress in 4.0, must’ve been an old tutorial

2 Likes

Okay, I understand. Is this correct?

What do I need to put into the lerp()?

So you create a local variable var offset, this is the closest position on the path to your target. You can still use this as the second parameter.

Be sure to paste code instead of taking a screen shot, and use formatting on the forum like so

```
type or paste code here
```

1 Like

Okay, when I’am using ‘offset’ as the second parameter, I am getting a new error:
Invalid get index ‘global_position’ (on base: Camera2D)

extends Path2D

@export var target = Camera2D

@export var lerp_speed = 2.0
@onready var follower: PathFollow2D = $PathFollow2D

func _ready():
	pass                      # I don´t understand that part :(

func _process(delta: float) -> void:
	if target != null:
		var offset: = curve.get_closest_offset(target.global_position - global_position)
		follower.progress_ratio = lerp(follower.progress_ratio, offset, lerp_speed * delta)
	else:
		print("Error")

Ah, the camera’s export is decalred incorrectly.

@export var target: Camera2D = $PathFollow2D/Camera2D

Honestly best to use @onready like you did for the “follower” variable, exports are more useful for changing instanced scenes, but this camera is set in stone as a child.

@onready var target: Camera2D = $PathFollow2D/Camera2D