Javascript, Instance Variable and Pizza?

0 favourites
  • 4 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I created an object and generated an infinity of copies of it. All inherited 3 variables from the main object.

    Now I need to access 1 of these 3 instance variables, from each copy.

    To improve the explanation I will give names to these instance variables: "music", "steps" and "pizza".

    I need to compare the value of the "pizza" instance variable of each copy object.

    To make this distinction between copies I decided to use idd.

    Now follows the failed attempt to solve this problem through javascript:

    if(runtime.objects.position.iid(0).pizza == "Pepperoni")

    {runtime.globalVars.total_Pizza++;}

    This logic didn't work, but what would be the correct logic to access an instance variable from an object copy through javascript?

  • The expression runtime.objects.position.iid(0).pizza confuses both object types and instances, both JavaScript and Construct expressions, and both number types and functions. I would advice following the Learn JavaScript in Construct guide.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I must do a review. But in this guide, it teaches us how to create classes, but not access instance variables of an object. In the example I gave, the object (which is a sprite) is called "position".

    From what I saw, in this tutorial, I must conclude that we can only access the first instance variable of an object, through the code "getFirstInstance", but the others is impossible, it would be necessary to create variables in the script itself instead of accessing the instance variables already created in the object properties in Layout.

    But I will reread the whole article, which is very good for teaching programming logic. I just miss a tutorial that teaches how to manipulate the variables that are present in the properties of objects (the sprites) mainly on the part of instance variables.

  • I mean you can do it like this. Get the array of instances and loop over them.

    var list = runtime.objects.position.getAllInstances();
    var count=0;
    for(var i=0; i<list.length; i++)
     If(list[i].instVars.pizza =="pepperoni")
     count++;

    Any particular reason you’re using JavaScript? It’s much simpler with events.

    Global number count=0
    
    Position: pizza=“pepperoni”
    — set count to position.pickedCount
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)