Odd error instance object set variable

Godot Version

4.3 stable

Question

I’m having a odd problem with a instanced object. It was working but now returned a error.

I have a data file that holds my workers (npc) and loads them when the game starts so that i can save the prexisting character data. It looks like this right now.

{
	"worker_01": {
		"id" : "worker_01",
		"name": "Sally",
		"worker_level": 1,
		"attributes": {
			"base_attack": 3,
			"fear_resist" : 2,
			"work_efficency": 3,
			"defence": 2,
			"walk_speed_boost": 5
		},
		"position_name": "WorkerSpawnLocation",
		"status": "idle",
		"machine_assigned": "Machine1",
		"inventory": {},
		"carrying": {}
	}
}

When the game loads it does this to create the characters.

	for nkey in PlayerData.worker_settings.keys():
		var worker_instance = WORKER_NPC.instantiate()
		worker_instance.worker_id = nkey
		add_child(worker_instance)	

The var worker id is defined in the instance code.

var worker_id

Now i’m getting a error when trying to assign the worker id to the instance.

Invalid assignment of property or key ‘worker_id’ with value of type ‘String’ on a base object of type ‘CharacterBody2D’.

I’m a little confused on why this is happening, perhaps someone can see the error that i don’t see?

Thanks

Try putting a breakpoint on this faulty line and investigate what’s the status of variables and references at that time.

1 Like

I couldnt figure out how to get to the instance variables in the debugger so I tried to print the existing value but get an error. I even tried with a differnt variable with the same result.

print(worker_instance.worker_id )

and that is after i set a default value for the id var

Invalid access to property or key ‘worker_id’ on a base object of type ‘CharacterBody2D’.

it is like the variable doesnt exist or something

ok, figured it out.

There was a error in the instance code but it wasnt showing me the error. After fixing that it worked.

1 Like