Object Structure Workaround

I am working on a kind of factory game. (I’ll use psuedo code here because my actual code is pretty tangled at the moment, but the concepts should be easy to understand.) Imagine I have many different factories: Fa, Fb, Fc, etc. Now, each of those factory types has various Resource (Ra, Rb, Rc, Rd, etc.) used for Inputs and Outputs of varying quantities.

Example:

Fa Inputs: 2xRa + 1xRd = Outputs: 2xRb

Fb Inputs: 1xRa + 1xRb + 3xRc = Outputs: 1xRe + 2xRf

And so on.

(Additionally, there can be multiple factories of each type in play.)

I would like to build each factory object with its own Structure variable, such as:

Fa:

Process {Structure}

  • Inputs {Structure}
    • Ra Number 2
    • Rd Number 1
  • Outputs {Structure}
    • Rb Number 2

Fb:

Process {Structure}

  • Inputs {Structure}
    • Ra Number 1
    • Rb Number 1
    • Rc Number 3
  • Outputs {Structure}
    • Re Number 1
    • Rf Number 2

I would love to be able to iterate through each object’s Inputs and Outputs independently, but For Each Child does not work for object structures (only Scene structures). However, if I build these factory structures as Scene structures, I run into the problem of trying to tie the Factory Name into the Scene Structure name.

With the factories in a Scene Structure, I’ve tried to write something like this:

Repeat for each instance of groupFactory:

For every child in Process[groupFactory.ObjectName()].Inputs, store child variable in Process[groupFactory.ObjectName].ChildValue, the child name in Process[groupFactory.ObjectName().ChildName and do:

[…]

But this does not work. There doesn’t seem to be a way to dynamically insert the groupFactory.ObjectName() into the necessary places of the For every child string on the fly.

I have tried setting the groupFactory.ObjectName() as a scene string (like: scnTempString), but this causes each factory to constantly reset this scene variable to it’s own ObjectName.

This would be so easily solved if I could iterate through each Object’s structure variables, but I’m stumped as to how to make a scalable solution to this factory problem. Please let me know if there are any workarounds dealing with the children of multiple object structures.

Thanks,
Tom

Hey Tom, welcome!

Although both methods can achieve the same result, I think using a scene structure variable is the better and easier method, and you’re actually on the right track.

Scene variable:

Processes {main structure}
    ○ Fa
        • Inputs
             - Ra: 2
             - Rd: 1
        • Outputs
             - Rb: 2
    ○ Fb
        • Inputs
             - Ra: 1
             - Rb: 1
             - Rc: 3
        • Outputs
             - Re: 1
             - Rf: 2

Event:

For every child in Processes[groupFactory.ObjectName()].Inputs , store child in tempChild, store child name in tempChildName.

I suggest not messing with the main structure (Processes) and store the children values or names in their own separate variables.

Thank you for responding. While I understand your restructure, I again must point out that Processes[groupFactory.ObjectName()].Inputs does NOT work as written. I wish it did. Assume groupFactory.ObjectName() = “Factory A”. GDevelop won’t substitue “Factory A” in place of groupFactory.ObjectName() within a For Every Child command. If there were a way to do this on-the-fly substituion, I could likely work with that. But that’s one of the walls I’ve hit trying to work around the lack of For Every Child not working for Object structures.

I agree with insein. If your objects are instances though, you might need to use an ID object variable or a modified object name as a variable name with a number. Meaning either use an object instance variable with objects using ID of 1, 2, 3 or an object variable like name with object instance values of factory1, factory2, factory3 etc. You would then either build the object name with group.ObjectName() + group.ID or just use group name variable

As for copying an object variable to a scene variable, you can covert the object variable to a JSON string and then the string to a scene variable.

To test this, I had an object structure named Output with Gold, Silver and Bronze as children. The scene variable Output has the same structure setup.

This displayed the values in a text object

The main thing would be the object would have 1 variable structure of like Data and could have child structures of output and input.

Object variable objectName.Data.Output.Gold

Would become sceneVariableName.Output.Gold

1 Like

Ok now I understand what you meant. I actually had this problem before and I forgot that this doesn’t work. Sorry about that.

The workaround I used is simply storing the object’s name in a scene variable string and using this variable sting in the for each child event.

The event would be something like this:

For each object

  • → set string variable “objectName” to object.ObjectName()

  • For every child in Processes[VariableString(objectName)].Inputs , store child in tempChild , store child name in tempChildName .

Hey guys, thanks for all your input! I rebuilt my code using the structure variables attached to each factory object (rather than using scene structure variables), converting each object structure to JSON in order to use For every child. This seems to be working!

It’s possible using the scene structure variables would work too, but I think my previous code got too tangled somewhere along the way and it was easier to rebuild from scratch using the JSON conversion technique. This proved very fruitful, and now the factories operate as expected. Thanks again!

-Tom