Return a list of object variables with JS

Hello,

i keep going with seeking a little help on some improvements for my “internal” debugger (like describe in [Solved] List the whole scene list - #9 by arthuro555).

The idea:
Actually, a certain amount of my interactive objects on the scene can display some of his object variables on mouse rollover (in a dynamically created text objects). It’s really usefull, because i can track in realtime the values changes of theses variables at any time.

Furthermore, tracking the values of the variables in realtime is really important, since the GD5 debugger is not displaying it in realtime (while GD4 did).

I use different debugging options to display only some var & values, or whole list of object variable when needed. My objects are standardised a lot so my tool works very well, but problem : 1)it’s hard coded, and 2) it’s painful/long/boring when i create new object less standardised for some special tasks.

How it could work:
So i suppose it’s possible, exactly like for scenes list asked in the other post, to create something in JS that can return a formated string wich could be displayed in text object or stored in text var ? Variables could be displayed line by line, and child variables indented ?

This demand is basically a “trick” i need to polish and extend my actual tool (no one like hard coded stuffs :p), but i think it could probably being part of the bigger “feature request” demand on GD5 debugger usability.

1 Like

Making a string representation of a variable in Js? Interesting. Try with this js snippet. I can also handle automatic text object creation for you but I will do that separately as it is not the direct question of the topic :wink:. For this one, it creates for each object in the scene an object variable VariablesAsString containing a string representation of every other object variables like you said.


let deepness = 0
let varParse = function(variable) {
    if (variable.isStructure()) {
        deepness++;
        let retText = "{\n";
        for (let variab of variable.getAllChildren().keys()){
            retText += variab + ": " + varParse(variable.getChild(variab)) + "\n";
        }
        let indent = "";
        for (let i in Array(deepness)) {
            indent += "    "
        }
        deepness--;
        return indent + retText + "}";
    } else {
        if(variable.isNumber()) {
            return ""+variable.getAsNumber()
        } else {
            return variable.getAsString()
        }
    }
}
        

for (let object of runtimeScene.getObjects()){
    let varString = "";
    object.getVariables().remove("VariablesAsString");
    for (let variable of object.getVariables()._variables.items.keys()){
        varString += variable + ": " + varParse(object.getVariables().get(variable)) + "\n";
    }
    object.getVariables().get("VariablesAsString").setString(varString);
}

This is untested as always so no guarantee it works.

Sorry for use of internal var but didn’t found another way.

3 Likes

Not sure about how to use it.
I think i understand the snippet, and what is doing varParse() and your array, but i failed to use it :frowning:

If i use as an event, it just creates tons of strange things on the scene (a lot of conditions trigger like if they were true without doing anything!). If i try to pass an object to the javascript event to expect an output on it, the scene just don’t load (stuck at 96% on loading).

This is weird. In theory it should just put in every object an additional variable containing a string with all the variables of this specific object. When your game is stuck, can you press Ctrl-Shift-I and look in the console what error appears? I am sorry if it isn’t working I just wrote this directly without testing.

if this can help:
image

I’ve just seen you have updated the script above with indentation, i’ll try it right now.

EDIT : same thing, now the loading reach 100% then black screen.

1 Like

Could you click on code1:numbers right next to the first error to see where exactly the error comes from? The indented one should not soleve the issue just add indentation in the end text.

image

1 Like

This was indeed an error of mine. I fixed the code.

Unfortunately, here is the last code we tried yeasterday:

let deepness = 0
let varParse = function(variable) {
    if (variable.isStructure()) {
        deepness++;
        let retText = "{\n";
        for (let variab of variable.getAllChildren().keys()){
            retText += variab + ": " + varParse(variable.getChild(variab)) + "\n";
        }
        let indent = "";
        for (let i in Array(deepness)) {
            indent += "    "
        }
        deepness--;
        return indent + retText + "}";
    } else {
        if(variable.isNumber()) {
            return ""+variable.getAsNumber()
        } else {
            return variable.getAsString()
        }
    }
}
        

for (let object of runtimeScene.getObjects()){
    let varString = "";
    for (let variable of object.getVariables()._variables.items.keys()){
        if (variable === "VariablesAsString") {
            continue;
        }
        varString += variable + ": " + varParse(object.getVariables().get(variable)) + "\n";
    }
    object.getVariables().get("VariablesAsString").setString(varString);
}

It doesn’t work :frowning:

For precision the part that doesn’t work seems to be the assignment of the variable. The object variable doesn’t appear on the object.

Theories:
– Variable assignment itself doesn’t work
– runtimeScene.getObjects don’t get all objects
– The object for loop was setted up incorrectly

The probably problem doesn’t come from the variable getter as the variable isn’t set in the first place. While accessing it it returns 0, meaning the setString wasn’t called as else it would be "".

@arthuro555, did you tried this snippet on your own ?