How do I add the total value of instance variable

  • 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

  • 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.

  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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)

  • 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.

  • SwatHound don't worry everyone make mistakes. I am 67 and pro but still make plenty of mistake lol

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)