Problem: Object variables stay 0 even though structure data is correct inside a function

Hi everyone,
I’m working on a small auto-battler project where I generate cats (TokenCat) using a function that receives:

  • in_rol (text) → the role name (like "tank")
  • out_obj (object) → the cat instance to fill
  • roles (structure) → a variable containing all role data
  • nextId (number) → id counter
  • txt_debug (object) → a text object for debugging

Everything works except one thing:
Inside the function, I can correctly read the structure (for example rolesRef.tank.color gives "orange"),
but when I write the variables into the object with ModVarObjetTxt or ModVarObjet, all values appear as 0 in the debug output.

Example of the debug text after creation:

id=c1 | rol=0 | color=0 | V:0 A:0 S:0 | hab=0

However, VariableString(rolesRef.tank.color) prints "orange" correctly inside the same function.

Here is the simplified version of what I’m doing inside the function:

CopyArgumentToVariable2("roles", rolesRef)
CopyArgumentToVariable2("nextId", tmpNextId)

tmpRole = VariableString(in_rol)
vmin = Variable(rolesRef[VariableString(tmpRole)].vida[0])
vmax = Variable(rolesRef[VariableString(tmpRole)].vida[1])
tmpColor = VariableString(rolesRef[VariableString(tmpRole)].color)
tmpHab   = VariableString(rolesRef[VariableString(tmpRole)].hab)

vida = floor(RandomInRange(Variable(vmin), Variable(vmax)))

out_obj id    = "c" + ToString(Variable(tmpNextId))
out_obj rol   = tmpRole
out_obj color = tmpColor
out_obj vida  = vida
out_obj hab   = tmpHab

No errors, but every value except id is shown as 0.

So far I’ve verified:

  • The structure (rolesRef) is correct — I can print any field like rolesRef.tank.color = "orange".
  • The function is called correctly inside a For each TokenCat.
  • The parameters are passed in the right order.

Could it be a typing or scope issue when assigning to out_obj from inside a function?
Do object variables keep their old types (number vs string) if they were initialized before?

Any ideas or clarifications about how GDevelop handles object variables in functions would be super appreciated :pray:

1 Like

The Variable() and VariableString() expressions are no longer needed in GD. I don’t believe that’s the issue but it’s extra work plus it makes the events slightly more difficult to read.

Anyways, the issue might be that you have a create object as a subevent of a delete object. As well as the other events.

Objects don’t get deleted immediately. So, any deleted objects would still seem to be alive.

You can test this by changing the text action in the function to add instead of equal. That way the text object would show the stats for each object instead of just the last object.

This shouldn’t really cause a problem though. Each object should still have their variables changed.

Try putting the delete action as it’s own subevent and then the other events with the same indentation.

Meaning:

Instead of

Trigger once
… Delete object
… … Create object

Try

Trigger once
… Delete object
… Create object

My only theory is that since the deleted object is triggering the function that the nextID value is being increased too many times. IDK. Maybe it’s something else. IDK the purpose of the NextID.

This might not fix things but it’s a start.

Also, since autocomplete doesn’t work for objects inside a function, make sure all of the variables are spelled correctly including their case.