Using formulas + app (or page) variables to access a specific field of a data variable

Hi! I’m working on a rudimentary mockup of a multiplayer game. I have a bunch of data variables set up, each for a different Country, with details about that Country drawn from the database.

When a player starts the game, they are assigned a country (app variable). I would like to display the amount of money their country has by referencing the contents of the “user_country” data variable.

As a static formula, that text would look like:
"You Have: $"+data.countryData_egypt.fields.money.integerValue

However, I want to reference the country dynamically, using the app var “user_country” to find values for “egypt” instead of hardcoding the word “egypt” into the formula:
"You have: $"+data.countryData_{appVars.user_country}.fields,money.integerValue

Obviously this is not the right syntax, since it throws an error. Can someone point me in the right direction on how I would do this? I have been able to use nested LOOKUPs as a workaround in a similar case, when I was replacing an entire field with the app variable, but that doesn’t work here, where I’m trying to replace only part of the field (what comes directly after “countryData_”):
"You have: $"+LOOKUP(LOOKUP(LOOKUP(JOIN(["data.countryData_",appVars.user_country],""),"fields"),"money"),"integerValue")

Thanks in advance!

I tried adding yet another LOOKUP to employ the same trick I’ve used elsewhere, but with “data” as the initial object:
"You have: $"+LOOKUP(LOOKUP(LOOKUP(LOOKUP("data",JOIN(["countryData_",appVars.user_country],"")),"fields"),"money"),"integerValue")

Unfortunately treating “data” as an object doesn’t seem to work, so I’m still stuck.

Edit: I guess this is all very roundabout way of asking: Is there a way to turn a string into an object within my formula?

Hi!

If I understand your issue correctly, in my opinion you’d benefit a lot if you’d change the structure of your data a little. I’ll explain just a little about how I think it would be good to structure your data, and then get to trying to see if I can help you with your current solution.

In my opinion, it would be good to just have a database of countries as data.Countries from which you find the correct country by id. You would then store this id to e.g. Object type app variable with id and name, or just a string/number type app variable of the id. With the id of the country, you could then fetch all related data from the database. The bit of formula to find the country by id would be something like FIND_BY_KEY(data.Countries, "id").

As for the current structure, as you’re right that “data” can’t be accessed as an Object and as such I don’t know how this would work, I would rather recommend making a data/page/app variable for current_user_country which you set where the country for that user is determined, and access this Object in the formula instead.

1 Like

Thanks for the suggestions, Mevi! Instead of re-structuring my data entirely (as there are already a bunch of variables and schemas built around the current structure), I will use your second suggestion instead :grin:

It really does seem like there should be a way to formulaically/dynamically refer to a variable, though – i.e., to build a string using a formula and then indicate via a function or some kind of separator, “I’d like to refer to the object with this designation please.”

Let me know if you have any further questions!

Just to let you know, I asked about your suggestion and after a heated discussion, the conclusion that we don’t want to provide the possibility to choose a variable dynamically (as it would considerably worsen the performance of the apps if it would have to be taken into account). However, the property of a variable can be chosen dynamically, and that’s fine.

1 Like