Get all the sub-object type variables of the family in a for each-loop event (with script)?

0 favourites
  • 9 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • I'm trying to write all the information of the sprites contained in my layouts with JSON.

    Including size, angle and even instance variables.

    But I do this by using a family that contains all the objects.

    Previously I had inserted objects in the layouts that have specific variables, for example:

    (items to collect):

    grenades (variable "ammo" and variable "damage".

    (enemies) :

    ufo ( variable "lifepoints" and variable "attackpoints" .

    These variables are specific to each type object.

    Is there a way to access it using scripting?

    I want JSON to also write all the variables and values ​​for each type object that is part of the family.

  • If you need this to save/restore sprites on the layout, you can use Family.AsJSON expression. It will save all object properties, including the values of all instance variables. However, there will be unique codes instead of variable names.

    Or you can retrieve all instance variables with this script:

    const o = runtime.objects.Family.getFirstInstance();
    
    for (var key in o.instVars) {
     console.log(key + ": " + o.instVars[key]);
    }
    
    
  • If you need this to save/restore sprites on the layout, you can use Family.AsJSON expression. It will save all object properties, including the values of all instance variables. However, there will be unique codes instead of variable names.

    Or you can retrieve all instance variables with this script:

    > const o = runtime.objects.Family.getFirstInstance();
    
    for (var key in o.instVars) {
    console.log(key + ": " + o.instVars[key]);
    }
    
    

    I tried the second method (the script), because I didn't understand how to use the first one.

    That's not what I meant anyway.

    I would like the console.log to also write the object variables not related to the family.

    I put up some screenshots.

    I also modified some variables in the layout view of the objects (related to the family) but in the console the 4 variables are always 0 and "" and false. (probably because it's not correct how I made the events and how I included your script. I don't know how to do.)

    Sorry for the strange names, but I'm Italian and I program half Italian and half English.

  • I didn't understand how to use the first one.

    It's easy. Save Sprite.AsJSON to a text variable. Later, when you need to restore, create a new Sprite instance and use "Sprite Set From JSON" action - it will restore its position, instance variables and all other properties from the saved JSON.

    I would like the console.log to also write the object variables not related to the family.

    You know which variables are related to the family - simply exclude/ignore them when running the "for (var key in o.instVars)" loop.

    for (var key in o.instVars) {
    
     if (key!="FamVar1" && key!="FamVar2") {
     console.log(key + ": " + o.instVars[key]);
     }
    }
    
    
  • > I didn't understand how to use the first one.

    It's easy. Save Sprite.AsJSON to a text variable. Later, when you need to restore, create a new Sprite instance and use "Sprite Set From JSON" action - it will restore its position, instance variables and all other properties from the saved JSON.

    > I would like the console.log to also write the object variables not related to the family.

    You know which variables are related to the family - simply exclude/ignore them when running the "for (var key in o.instVars)" loop.

    > for (var key in o.instVars) {
    
    if (key<>"FamVar1" && key<>"FamVar2") {
    console.log(key + ": " + o.instVars[key]);
    }
    }
    
    

    Sorry, I can't understand, could you give me a practical example?

    I tried to use your code but it gives me an error on key<> and I don't understand why.

    Could you make a sample project and post screenshots?

    Thank you.

  • I tried to use your code but it gives me an error on key<> and I don't understand why.

    Sorry, I don't use JS often, it should be != instead of <>

    I assumed you know Javascript since you asked for a script solution?

    Here is a demo.

    dropbox.com/scl/fi/iw8pscxnqot8t6dnx0965/JS_InstVars.c3p

  • > I tried to use your code but it gives me an error on key<> and I don't understand why.

    Sorry, I don't use JS often, it should be != instead of <>

    I assumed you know Javascript since you asked for a script solution?

    Here is a demo.

    https://www.dropbox.com/scl/fi/iw8pscxnqot8t6dnx0965/JS_InstVars.c3p?rlkey=nrdw9bcn9s4yn0k61jc6ra03d&dl=0

    Yes I'm studying javascript step by step. but I'm still at the beginning.

    Thank you so much!

    I also inserted a new sprite and got the variables of all using getFirstPickedInstance:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • could you also tell me how to insert the variables into JSON format?

    How can I do the same thing with the JSON plugin?

    JSON --> set value --> Set "."&loopindex&".height" to int(FamilyALL.height) what is written above in my screenshot, how can I do it with the script?

    I should get in the console:

    "Level_1": {

    "Sprite": [ { "posx": 20291, "posy": 223, "size": 50609, [{ ---> ObjVar1: 999, <--- ---> ObjVar2: zzz, <--- ],}

    }

    ],

    I'm also fine with doing everything with the script, but for each of my objects in the layout I need to have all the information regarding it

  • I don't know JS that well.

    Google how to build a JSON string in Javascript. Then use setJsonString(str) to set it to JSON object.

    construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/plugin-interfaces/json

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