[Solved] How to add all number variables in a structure and making the sum of that number go to another number variable

Hello, I’m making a collectathon where you collect a gem. When you complete a stage there is a structure variable that has number variables for each level, 0 for not having that gem, while 1 for having it. :face_with_raised_eyebrow:

I’m trying to see if there is a way just to add all number variables in that structure variable to a total gem number variable. :thinking:

I found a way to do this, but it seems tedious.
Just making an action that sets the total gem number variable by putting each and every number variable for the value. :smiling_face_with_tear:

I just didn’t know if anybody has a better, and more efficient solution. :thinking:

If there is anything that can just add all child number variables in a structure to another number variable that would be great! :smile:

How is your structure setup? Is it a variable with a structure with the gem types or is it a structure or array of structures with the gems separated by levels? A screenshot of the variable would help.

You could keep a running total or use for each child to add everything up.

1 Like

Here is the variable

Menu_Gems is the structure variable containing all level gem variables. I’m trying to find a way to add all the number variables up, then put the sum into the Gems number variable

Menu_Guy_Data has the Gems number variable to put the sum of all of the total gems collected in.

I plan to repeat this process with other collectables like Keys, Time_Medals, and Special_Gems.

You could use a for each child. This would add all of the children.

Variables.

Events

Result
Screenshot_20240912_152101_Chrome

I added the items to a text object just to demonstrate the process.

That would be the basic setup. A more automatic setup would use a structure of structures.

Like

Items
–gems (as children of items)
------list of gems and quantities as children of gems
–wood
------list of wood

You could use nested for each child events to go through the entire list and create another structure with the totals or each catagory could have a child named total among the other items.

So it could be
.
Items.Wood.Total
Items.Wood.Ash
Items.Wood.Spruce

Items.Gems.Total
Items.Gems.Ruby
Items.Gems.Diamonds

The first for each child. Would get the children names of each Item and the nested For Each child would get the counts of each child.

I don’t have the time right now to create another example of that.

There might be a simpler way. Maybe using functions within the array tools extension.

If this is what you’re looking for, I can try to create an example later. It might sound tougher than it is. It would organize everything into 1 structure. It won’t require a lot of events. Just planning.

Edit: this would use nested for each child events. Note the events don’t use the same variables. I added Category to the first one to hold the child name.

I realize that you might only be interest in whether there’s a particular gem not the total. Meaning there’s 3 types of gems not a total of say 30 gems. This is just to showcase a method.

Variables. Click to open.

Events

Result
Screenshot_20240912_193548_Chrome

These are all the same

Set Category variable to “Wood”

Items[Category].Total
Items[“Wood”].Total
Items.Wood.Total

To just count how many unique items of each type then you could use the following.
This just has a slight change. Instead of adding the number of each item, it adds 1 to the total if the number isn’t zero. I think that better reflects your need.

Edit: I realize I had a at the beginning event. That’s a habit. In this example that’s not needed.

2 Likes

Thanks for the information about child variables, very helpful :grin:.

For the text I use an icon sprite for it, so do I need ChildName? :face_with_raised_eyebrow: I would only use the text for the number of gems collected. (Not an = also)

Plus, the only collectables are going to be in are the Gem, Key, Special Gem, and the Time Medal. I don’t mind repeating the process of the first example just 4 times, sorry if I miscommunicated when I said “like” Keys, Time_Medals, and Special_Gems, on my second post.

Also, I tried it without the ChildName text variable and when I got the 2nd gem, after I completed it and went to level 3, It said I had 3 gems and not 2. I think I broke it because of not including ChildName to it. :sweat:

I’m new to the whole Child variable thing, so I don’t know that much about them :sweat_smile:. But thanks for telling me about them still. If you can find a way to do it without the including extra text, that would be great, still thanks for the help. :smile:

It takes some getting used to but like for each object, it’s a great time saver. You can start at any level of a variable either from the top of the variable or from a child. You could use another structure or another array to pick the children.

With the nested approach, you could use an array with the items you want a count of. And then use it with the larger structure.

Here’s simplified example the just displays the children.

Varaibles.

The result is just the values for the children in the List array.
image

You can use for each child with an array or a structure.

The 2nd and 3rd boxes are optional. Although, it wouln’t be useful without a variable. It would basically just be a repeat. You just need to make sure the variables are declared.

It could be that you didn’t set the counter variable back to zero. IDK. You’d have to post a screenshot of the events if you’re still having a problem.

1 Like

This is another example. It shows how you can use a child value or it’s name as a condition.

Modified variables.

If this was a real world project, I wouldn’t check the boolean, you could make a structure with a structure and an array. Yeah, it gets confusing but it’s all about the structure.

image

You can see in this setup, you wouldn’t need to check the boolean a 2nd time.

Events

Results
image

Variables

You can see how the variables are named in the autocomplete
image

In the real world, you want to keep the number of children realitivly small. It gets confusing if there are too many levels.

1 Like

Thank you for taking the time to explain it to me! I fixed it :smile:. I forgot to set the counter variable back to zero. :laughing: Thank again! :grin:

1 Like