'Cannot call method "translate" on a null value. error

Godot Version

Godot 4.4.1 stable

Question

Hello,
I am following the HeartBeast Space shooter tutorial.

The goal is to create a more complex enemy behavior cycling components.
Move to on the y axis for 2s, then move on the x axis, then spawn projectile, then pause, then start again.

However, when the code projectile_spawner_component.spawn(pink_enemy.global_position) is added, I am met with a ‘Cannot call method “translate” on a null value.’ pointing me to the move_component.

Please note that the projectile is spawning is executed, it is when the inline function is finished that the error pops up. Also note that commenting out the projectile_spawner_component.spawn(pink_enemy.global_position) gets rid of the error.

Also note that the original tutorial is 2 years old and was developed on Godot 4.1.3, that is why I believe that the error might be due to some engine version differences.

Please see the script of the enemy as well as the script that the error points towards.

pink_enemy

class_name PinkEnemy
extends Enemy

@onready var states: Node = $States

@onready var move_down_state: TimedStateComponent = %MoveDownState
@onready var move_side_state: TimedStateComponent = %MoveSideState
@onready var pause_state: TimedStateComponent = %PauseState

@onready var move_side_component: MoveComponent = %MoveSideComponent

@onready var fire_state: StateComponent = %FireState
@onready var projectile_spawner_component: SpawnerComponent = %ProjectileSpawnerComponent

@onready var pink_enemy: PinkEnemy = $“.”

func _ready() → void:
super()

for state in states.get_children():
	state = state as StateComponent
	state.disable()

move_side_component.velocity.x = [-20, 20].pick_random()

move_down_state.state_finished.connect(move_side_state.enable)
move_side_state.state_finished.connect(func():
	fire_state.enable()
	scale_component.tween_scale()
	projectile_spawner_component.spawn(pink_enemy.global_position)
	fire_state.disable()
	fire_state.state_finished.emit()
)
move_side_state.state_finished.connect(pause_state.enable)
fire_state.state_finished.connect(pause_state.enable)
pause_state.state_finished.connect(move_down_state.enable)

move_down_state.enable()

func _process(delta: float) → void:
pass

move_component.gd

class_name MoveComponent
extends Node

@export var actor: Node2D
@export var velocity: Vector2

func _process(delta: float) → void:
actor.translate(velocity * delta)

Any help is welcome, I have fried my brains on this one.

It looks like the issue is in move_component.gd. the variable actor holds null instead of a Node2D like it should. Since it’s an export variable, did you set the value in the inspector?

1 Like

The move component (Node) is the child of another Node that is called MoveDownState, please see picture.
But why is it only when the projectile_spawner_component.spawn(pink_enemy.global_position) piece of code added the error is thrown.
Should i comment it out, and it works ok… :thinking: and i checked, the actor is set the correct Node2D

I am using the exact same thing;

translate(-Vector3(0,0,0.1) * velocity)

I noticed this error is thrown in the case when Godot(4.4.1) does not multiply a Vector3 by the integer value. Because it can’t. Weevel, Vector3 is not an integer value.
Is this a ‘enemy shooting bullet’ question?

print(star_count)
print(start_lock.position)
print(str(home_star.position, " position, home."))
print(map_count)

I use this helpful line for the READY method to compare the results to data I expect. And, since I am interested in locations here. That are the same.
Here, it’s a challenge to figure out if I like this universe, where the location of any star is, and if I want it to be home star. Where I located. Also, the count of stars.

and it actually pays off.
It’s all around the screen.

false
(88456.0, 0.0, 67442.0)
(88456.0, 0.0, 67442.0) position, home.
8451

1 Like

the move_component throws the error, because i missed to assign the actor for the projectile that is spawned when the enemy is shooting… :melting_face: 2 days gone on this…
ty for the help mate :heart:

1 Like

ty, you gave me an idea and it helped me troubleshoot in a different spot :heart: