Add together all instance variables?

0 favourites
  • 3 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hey there!

    I was wondering what the simplest way to for loop through all the instances of a certain object and add together all of their values for the instance variable "amount"

    For example:

    Object1:

    (Instance Variable) Amount: 2

    Object2:

    (Instance Variable) Amount:7

    Total Amount: 9

    Thanks for any help you can provide!

  • You can do this with scripting:

    var obj = runtime.objects.Sprite.getFirstPickedInstance();
    var result=0;
    
    for (var key in obj.instVars) {
     result +=obj.instVars[key];
    }
    
    console.log("Result=" + result);
    

    But it may be more convenient in this case to use a dictionary or an array instead of a bunch of instance variables. Add the dictionary and the object to a container, and each instance of the object will have its own copy of the dictionary.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • dop2000 thanks for the help, this is convenient timing as I've been experimenting with the scripting in construct today as well.

    Appreciate it!

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