Correct way to GetInstanceVariableValue from name?

0 favourites
  • 4 posts
From the Asset Store
Flags
$5 USD
An educational memory game about countries’ names and their flags!
  • Ashley

    In the manual I can find how to get an instance variable value by its index (GetInstanceVariableValue(index)). But I can not find references on how to get the value of a variable starting from the name.

    I can do it with some undocumented features:

    GetInstanceVariableIndexFromName(string) {
    	//return this.GetInstance()._GetAllInstanceVariableNames().indexOf(string);
    	return this.GetInstance()._instVarNames.indexOf(string);
    },
    GetInstanceVariableValueFromName(string){
    	const pos = this.GetInstanceVariableIndexFromName(string)
    	return pos < 0 || pos > this.GetInstance().GetInstanceVariableCount() ? '' : this.GetInstance().GetInstanceVariableValue(pos );
    }
    

    What is the correct way to do this using the documented features?

    I need it because I'm experimenting with one of my plugins. My idea is to be able to directly access the variables via '{{ variable }}'.

    I can get the same result by writing {{0}} {{1}}. But it's much clearer and simpler if I can write {{name}} {{surname}}.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's not supported. Instance variable names are deliberately stripped out to impede reverse-engineering (a common concern amongst users). They are sometimes available in preview mode for the debugger, but it will stop working when the project is exported.

    This seems to be a half-way point to allowing arbitrary expressions anyway, so it seems you might as well just make it fire a trigger which can set a return value, and then you can use any expression at all in a placeholder, including instance variables.

    I'd also point out that you'd better implement that carefully or it will be a playground for injection attacks!

  • Thank you for your answer.

    I can create an optional property (Model) where you can insert the variable's name. I didn't think about injection attacks. Maybe I can put a function to sanitize external input.

  • Glad you asked this because I´m doing the same thing. If doing it like this is a concern I should probably also change this!

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