[Solved] How do I add all the instance variable value of each instance for a single object?

0 favourites
  • 11 posts
From the Asset Store
Kids Game
$49 USD
New Sounds Added Update: 115 new sound effects added for no additional cost!
  • I have a sprite called "Coins" and it has an instance variable of "CoinValue_IV". There are more than one instances of the same "Coins" sprite presents on the layout. Depending upon the "CoinValue_IV" of each instance, the user will get 100, 500, 1000 coins respectively.

    So, my question is, How do I add all the instance variable value ("CoinValue_IV") of the "Coins" sprite?

    Means, if the user will collect all the coins, total how many he will have on that particular stage.

    I tried

    "Coins.CoinValue_IV*Coins.Count"

    on every tik, but it doest work.

  • Unfortunately, there is no such expression. You need to loop through all instances and sum up all values:

    Global (or local) variable TotalAmount
    
    For each Coins : Add Coins.CoinValue_IV to TotalAmount
    
  • Unfortunately, there is no such expression. You need to loop through all instances and sum up all values:

    > Global (or local) variable TotalAmount
    
    For each Coins : Add Coins.CoinValue_IV to TotalAmount
    

    Hi, thanks for your quick ersponce.

    I tried that but sadly it doesn't work, the TotalAmount_GV does not stop adding numbers, I even added a triger once to it.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Don't add Trigger Once to loops.. If you need to calculate this total once, do it in On Start of the layout event.

  • If I add it on the start of the layout, it randomly selects only one instance of Coins (CoinValue_IV).

  • It kept counting so I tot by putting "tiger once", it may stop but sadly no.

  • there are 10 coins present on the layout, for 8 of them, I made "100" to its CoinValue_IV, and remaining 2 "50" to its CoinValue_IV.

    So the value of TotalAmount_GV should be 900. but its keep increasing.

  • Try this.

  • (Took too long). Here's a variation. You need a local variable, so you start counting at zero each time. (Or make sure to reset your sum-counter to zero).

    CoinSum_619_RM.zip

  • Download C2 Example

    image preview

    dop2000 was right :)

    took longer to place the objects in screen by event sheet than to actually make the calculation work ;)

    have fun... initially who ever said something about placing the loop under the start of layout was right... is just you need to get the order of events properly.

    hope this is what you mean.

    .. if you want to use it for coins in screen in a game... just replace the "on start of layout" to some 1 time trigger. you don't need at that point the creation lines... just the counting "for each coing iid ascending" and do the action. just make sure u trigger it once like... on collision with object, or on destroyed, or on score end game custom boolean trigger 1 time.

  • sizcoz, blackhornet, GeorgeZaharia & dop2000, Thank you, I greatly appreciate the time you spent for answering my question. :) because of you guys, Finally, my query has solved. Much appreciated.

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