How do I add the total value of instance variable

Get help using Construct 2

Post » Wed Jul 08, 2015 7:38 am

My question : How do I add all instance variables together (If I have 2 Units on screen, and their variables are 10, how do I show that the total is 20?)

I tried using TotalCount=10*Unit.Count, but this will be a problem since the buildings will eventually not have the exact number of units inside of them (If one gets attacked and loses men).

I'm not sure if this has something to do with the TYPE of instance variable its set to (Text, number, Boolean)

I'm trying to create a swarm defense type game, where I send my units into their buildings, the person with more units wins and claims the building.

Said building creates more units and the cycle continues.

I have some buildings that contain "units" inside of them.

Is it possible to add the sum of all of the buildings to give me the total number of units on the map? I'm using a family variable, trying to modify a global variable, is this possible?

I have a feeling I should be using Arrays but I don't know exactly how.

The capx is a mess, sorry. The more I tinkered with it the worse it got.

I'm starting over with a clean event sheet, but I have no idea where to even begin to have the system add up the value of the individual instance variables.

https://drive.google.com/file/d/0B-APu8 ... sp=sharing
B
6
S
2
Posts: 60
Reputation: 1,242

Post » Wed Jul 08, 2015 8:03 am

If anyone could link me to a tutorial that does SOMETHING along these lines I would be very greatful.

I tried searching the manual for anything relating to instance variable, total sum, adding, checked the forums, I just can't seem to find anything relating to

"Adding instance variable to a global variable" using the total number with every unit on screen.
B
6
S
2
Posts: 60
Reputation: 1,242

Post » Wed Jul 08, 2015 8:20 am

create a new variable CombinedValue
If you want the action performed every tick

every tick
> System set value CombinedValue = 0
-- for each unit
> System add to combinedValue = unit.instancevariable
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Wed Jul 08, 2015 8:32 am

B
48
S
16
G
9
Posts: 1,098
Reputation: 11,197

Post » Wed Jul 08, 2015 9:02 am

Thank you for the quick replies guys, but I'm still having some issues.

I wan't the total count to be equal to the total value for each unit.

I have TWO buildings, both with the same value, so lets say 10.

I want the total value to equal 20, or atleast, I wan't to be able to count individual units and add the total up to later implement a unit CAP.

I'm not sure if I asked the initial question funny or I'm doing something very wrong here.

https://drive.google.com/file/d/0B-APu8 ... sp=sharing

In this example, the text matches the numbers of the variable, but it should actually be double that (Since there are 2 blue buildings spawning units)
B
6
S
2
Posts: 60
Reputation: 1,242

Post » Wed Jul 08, 2015 9:27 am

B
48
S
16
G
9
Posts: 1,098
Reputation: 11,197

Post » Wed Jul 08, 2015 9:34 am

I.... Thank you.. I don't know why... lol..

I'm looking at your capx now and I'm kind of upset at why couldn't I get that.

I'm not sure if I missed something in my capx but this seems to be working.

Thanks a lot, appreciate it.
B
6
S
2
Posts: 60
Reputation: 1,242

Post » Wed Jul 08, 2015 10:52 am

SwatHound don't worry everyone make mistakes. I am 67 and pro but still make plenty of mistake lol
B
252
S
39
G
15
Posts: 1,964
Reputation: 40,753

Post » Wed Jul 08, 2015 6:27 pm

Haha thanks, LordShiva. I think I just needed to get some sleep, or I just did not want to use the "Add" expression(or is it function?) , but I guess it works.

I was just hoping for there to be an alternative instead of Adding to it, I'm not sure why but I feel this is going to hurt me later down the line, but I guess I'll have to wait and see.

My fear is that when the units start colliding into each other my event sheet will have a bunch of "Add to" and "Subtract from" and such, where I was hoping there was some way to have the whole value somewhere (And yes now it is in CombinedValuePlayer, thank you again for the help LittleStain and Spongehammer)

But I was hoping more for a way to have the system to update to whatever X is without me having to "Add" or "Subtract" The value manually.

Am I asking to much or is this something that perhaps I should have looked into Arrays for?


Instead of System - Add Sprite.Units to CombinedValuePlayer, is there a way to make the add be... "Set to current value"? The more I talk about this the more I feel maybe this is an issue Arrays could solve.
B
6
S
2
Posts: 60
Reputation: 1,242


Return to How do I....?

Who is online

Users browsing this forum: Artcadev, dop2000 and 37 guests