Exported array variables getting default values on _ready()

Godot Version 4.2.1

I’m following the tutorial “Making an FPS Weapon Manager in Godot 4” (around 13:00 in https://youtu.be/dTW9jIIdbPU?t=765) and i’m having problems with empty “start_weapons” and “_weapon_resources”. I did filled these variables in the editor as the first image shows and the second one shows the error. The code bellow is from weapons_manager.gd (linked with weapons_manager node)


image3

image

How are they declared in the code and what code is in _ready and Initialize

Sorry for the delay and thanks for the interest

@export var _weapon_resources: Array[Weapon_Resources]
@export var start_weapons: Array[String]
var weapon_list = {} # all game weapons
var weapon_stack = [] # current held weapons
var current_weapon = null

func _ready():
	Initialize(start_weapons)

func Initialize(_start_weapons: Array):
	for weapon in _weapon_resources:
		weapon_list[weapon.weapon_name] = weapon
	
	# add starting weapons
	for i in _start_weapons:
		weapon_stack.push_back(i)
	
	current_weapon = weapon_list[weapon_stack[0]]```

Hmm well from the debug window it seems like your resources were not saved as they are all zero.

That’s the main problem :confused:

My guess is that those resources you have added only pertain to the scene they are added to. (The player scene?)

If you didn’t save them in the base manager scene its code will fail like it did for any other scene, or if the weapons_manager is dynamically instantiated elsewhere.

So you should probably add safety checks before indexing the arrays.

Exported values should be set after _init() is called so I don’t know what else it could be, other than it’s another copy of the weapon_manager that you forgot about.

2 Likes

Thanks for the support!
It was indeed running 2 times. I totally forgot i’ve set the script as a singleton.

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