Create projectile works in one scene but not the other [Solved]

Godot Version

4.2.2

Question

Working code - I have BlueBasicEnemy fires homing projectiles at the player. Works great. Code below:

extends CharacterBody2D

var t: float = 0.0

@onready var BLUE_BASIC_PROJ = preload("res://scenes/blue_basic_proj.tscn")
@onready var main_game = $"../.."
@onready var ship = $"../../ship"
@onready var bb_1 = $"."

# Fire at Player
func _process(delta: float) -> void:
	t += delta
	if t >= 1.0:
		t -= 1.0
		attack_homing()


func attack_homing():
	var projectile = BLUE_BASIC_PROJ.instantiate()
	main_game.add_child(projectile)
	projectile.position = position
	
	if bb_1:
		projectile.launch(ship)

And here’s the projectile code:

extends Area2D

const MOVE_SPEED = 300
const STEER_FORCE = 100

var velocity = Vector2.ZERO
var acceleration = Vector2.ZERO
var target = null

@onready var bb_1 = preload("res://scenes/BlueBasicEnemy.tscn").instantiate()
@onready var shipcall = preload("res://scenes/ship.tscn").instantiate()

func seek():
	var steer = Vector2.UP
	if target:
		var desired = (target.position - position).normalized() * MOVE_SPEED
		steer = (desired - velocity).normalized() * STEER_FORCE
	return steer

func _physics_process(delta):
	acceleration = seek()
	velocity += acceleration * delta
	position += velocity * delta

func launch(target):
	self.target = target

func _on_body_entered(_body):
	Globals.HitBy = "blue"
	Globals.ReflectTarget = bb_1
	queue_free()
	shipcall.impact()
 

^ This all works. I have the ship firing back when hit, this is the part not working and I think(?) it’s a tree issue but I’m too inexperienced at this.

Ship code (not working): (collision calls the reflect func, that’s all good)

extends CharacterBody2D

const SPEED = 400.0

const REFLECT_PROJ = preload("res://scenes/reflect_proj.tscn")

@onready var MAIN_GAME = $".."
@onready var bb_1 = $"../Enemies/BB1"

func reflect():
	var projectile = REFLECT_PROJ.instantiate()

	MAIN_GAME.addchild(projectile)

	projectile.global_position = self.global_position

	projectile.launch(bb_1)

I get “Invalid call. Nonexistent function ‘addchild’ in base ‘Nil’.”
If I @onready the REFLECT_PROJ instead of const, I get a similar error there.
Here’s my tree:
Screenshot from 2024-06-20 12-21-26

My inexperience is kicking my butt. Let me know if I can clarify anything.

function is add_child not addchild

also it appears that MAIN_GAME is nil when the function reflect is called when is it called?

Update:
Okay NONE of my variables are working in this script…
Everything is coming back null.
Screenshot from 2024-06-20 13-57-16

So that’s the issue… dunno what’s causing it though…

Sorry, the “addchild” was just a typo in the post. The code in my script is

MAIN_GAME.add_child(projectile)

Yes MAIN_GAME is coming up Nil is my issue. I’ve tried many things like

const MAIN_GAME = preload("res://scenes/main_game.tscn")
@onready var MAIN_GAME = $MainGame

etc. I’m not really sure how to do it correctly. In the working one:

@onready var main_game = $"../.."

Was achieved by clicking and dragging from the tree with ctrl, and it worked fine, but that doesn’t want to work on the other one.

Sooo I finally figured this out. The func was being called from another script, so it wasn’t taking the node path from the script within that function, I had to provide it from the other script calling it.

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