How do I treat a string from json as a construct interpretable expression?

0 favourites
  • 5 posts
From the Asset Store
Casino? money? who knows? but the target is the same!
  • i am working on a powerful and versatile dialogue system. i want it to be able to check for any aspect of the game state (such as inventory, skills, quest status, equipment, time of day, day of the year, etc.)

    tldr:

    i want to have text in json that is, for instance,

    {requires:"InventoryDic.Get(\"Banana\")"}
    

    and then i want to pass that text into construct 3 and have it return the value for the key "Banana" in the dictionary InventoryDic, rather than the string InventoryDic.Get("Banana"). is there an expression that attempts to turn a string from JSON into an ...expression...(?) like i would type into the expression dialogue? i've been looking at the documentation all day and it's entirely possible i'm just not seeing it.

    more information:

    my ideal would actually be to just have something like this in my json for each dialogue option:

    "requires":"InventoryDic.Get(\"Banana\")>=2"

    and then just find a way to evaluate it as an expression, even if it needs slightly different formatting. even if that is possible, i still want to be able to evaluate expressions from json. for instance, in the character's nameplate, one might want to put for the name a code, like CharNames.Princess, that finds the value for the key Princess in a dictionary CharNames and puts that into the nameplate. or, if i wanted someone to say how many bananas you had, or your favorite food you've selected earlier, or what you told them your name is, inline in normal text.

    the above example in the tldr is from breaking that expression into three chunks like this:

    "requires":{
    	"variable":"InventoryDic.Get(\"Banana\")","operator":">","value":1
    }
    

    which seems like it might be easier, since i could evaluate either side of the equation separately, and use additional conditions for operator. this seems obviously more prone to potential typos and such, but then i only need to know how to evaluate one simple thing. i have considered using a format function like in Laura D's dialogue tutorials, which i am using for things like newline, but as far as i can tell that would still require evaluating the text in construct to replace it with the associated values...

    i'm really stumped by this, i've looked (repeatedly) at the documentation for json and ajax and the expression finder and if it's there i really am not seeing it. but, i also missed the "reset platformer doublejump" action for like 4 hours when i first started working in construct last month, so it seems possible i'm just missing something obvious? like if it's possible i'm expecting it to be something really simple i just overlooked, but i have looked over and over now

  • You can access variables by their string name using scripting, but I don't think the same is possible with any expressions.

    You will need to invent some way of encoding these expressions into JSON, and create a parser for them.

    For example,

    {requires: "InventoryDic,Banana,3"}
    

    or

    {requires: {type: "dictionary", name: "InventoryDic", key: "Banana", val: "3"}}
    

    Then you can have a function which reads and processes this data.

    Say, if the type="dictionary", then you pick an instance from the AllDictionariesFamily with the object name="InventoryDic", and read the key "Banana" from it.

  • i see, thank you! so after testing i find i can evaluate, for instance:

    InventoryDic.Get(JSON.Get(.Variable))

    i have also got it to call a DictionaryCheck function that correctly picks and pulls the right values from the right dictionary!! incredible. didn't know about the "pick" system condition, which also fixes another issue i was having problems with (highlighting the speaking portrait with its name from json)

    however, i still don't seem to be able to find variables with their string names from json. i suppose i could solve this by making every global variable instead be stored in a globalVar dictionary, which is all the same to me even if it is more typing, but i wanted to check if there might be an easier way?

  • Like I said, the only way to access variables (global, local or instance) by string name is using scripting:

    Alternatively, you can create a bunch of conditions - if varName="hp" then return hp, else if varName="shield" then return shield etc.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • i see, i didn't understand you meant "only" before. thanks!

    for me scripting is kind of impenetrable, but after working with a friend, we came up with this function that i wanted to share if anyone was interested (i know how it sucks finding a forum post from 5 years ago where they don't say how they ultimately solved their problem):

    this lets you call the function in an expression with Functions.GetVarsFromString(JSON.Get()) or any string variable really and it will return the value of the global variable being called. you can see me using it in the conditions for my VariableCheck function (and calling that in the action at the top)

    anyway thank you again for your help! i am planning on releasing the dialogue system when i'm done with it so i will try to remember to post about it here as well for the future people who may find this thread in seven years

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