Reference a variable by a string

0 favourites
  • 7 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Say I have a string called strVar that contains the text "Example == 5"

    How would I be able to run a check that a variable called "Example" is equal to 5?

    Additionally, if the string was changed to "Example <= 5" how would I be able to check that it is equal or less than 5?

    I've been unable to figure out how to reference a variable by a string.

    I tried putting strVar in an evaluate expression, and hoping that would just work, but sadly it doesn't.

  • Hi.

    Can you tell me what problem you are trying to solve.

  • Hi.

    Can you tell me what problem you are trying to solve.

    If I gave you a text file with "varName == 5" in it, how would you code Construct 3 so that it checked if varName is equal to 5.

    Additionally, how would you code it if "varName <=2"

    Additionally, what if "varName == XYZ". How would you code it so you can check if the variable varName is XYZ?

  • A simple example of how I can parse a string via tokenat and compare the right part as a number or as a string.

    Using this method you can pull out the right parts of the string.

    But I would rather do everything through arrays, it's better than parsing files.

  • As far as I know you can't set/read a string value with an exported string, you need to reference the string by name directly in your events. It might be possible with scripting, though I haven't tried that.

    I would recommend you use a dictionary to store all your strings and values, then you can easily set/read them with a string. By splitting the text using tokentat based on spaces.

    tokenat(strVar,0," ") will get you the string name

    tokenat(strVar,1," ") will get you the valuation

    tokenat(strVar,2," ") will get you the result

    Then you can use dictionary.get(tokenat(strVar,0," ")) = tokenat(strVar,2," ")

    Since you can't also inject a valuation string and need to directly write it in your event, you would probably need to do a separated check for each type of evaluation.

    if tokenat(strVar,1," ") equals ("==") >

    Dictionary.Get(tokenat(strVar,0," ")) = int(tokenat(strVar,2," ")) ? "correct" : "incorrect"

    if tokenat(strVar,1," ") equals (">=") >

    Dictionary.Get(tokenat(strVar,0," ")) >= int(tokenat(strVar,2," ")) ? "correct" : "incorrect"

  • Simplest way would be to use browser.execjs() on that text string. You can access variables with the scripting api but it’s simpler to just provide the values manually.

    Here is an example. It runs the code as as JavaScript and the globals a and b are prepended to that. It’s simple to add more.

    text var result=""
    text var js=""
    text var code="a== 42"
    
    // globals
    number var a=42
    number var b=33
    
    on click
    -- set js to ""
    -- add "let a="&a&";" to js
    -- add "let b="&b&";" to js
    -- add code to js
    -- set result to browser.execjs(js)

    You could do something clever to have it automatically retrieve the variable values. There are also other ways to parse it.

    The pros of doing it this way are you don’t have to deal with any parsing and you can run any JavaScript. The cons are you don’t have a way to really deal with errors and you can run any JavaScript which can be considered a vulnerability to your game.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • A simple example of how I can parse a string via tokenat and compare the right part as a number or as a string.

    Using this method you can pull out the right parts of the string.

    But I would rather do everything through arrays, it's better than parsing files.

    Unfortunately that won't work, as you're only checking if it's equal to 5. What if I need to check if it's less than, or greater than, or a different number entirely?

    Also, you're only checking that the numbers match. I need to check the number against a particular variable which could change at any time.

    I could brute force it and account for every combination, but that wouldn't be remotely efficient.

    In the end, I decided to do it in javascript and convert everything to a string for the comparison.

    let tokens = runtime.globalVars.Example.split(' ');
    let variableName = tokens[0];
    let comparison = tokens[1];
    let comparisonValue = '"' + tokens[2] + '"';
    let value = '"' + runtime.globalVars[variableName].toString() + '"';
    runtime.globalVars.PassFail = eval(`${value} ${comparison} ${comparisonValue}`) ? 'PASS' : 'FAIL';
    

    I can then check the PassFail var to see if it was a match or not.

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