Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
String to variable name — Gideros Forum

String to variable name

DikkesnoekDikkesnoek Member
edited May 2013 in General questions
Hi,

I will get a string back from a JSON file like "level01". Now I
want to convert it to a variable name, like:

local level01 = 10.

Any ideas?

Thanks,

Marc

Comments

  • ar2rsawseenar2rsawseen Maintainer
    @Dikkesnoek usually in such cases tables are used, like
    --you have a table with variables
    local vars = {}
    --you retrieve a string value
    local l = "level01"
     
    --you use string value as key and place something in it
    vars[l] = 10
  • Thanks ar2,

    Marc
  • @Dikkesnoek,
    though I would advice against this very strongly, you can also set these are global variables and use them as
     name = "Jayant"
     print(_G["name"])
    or the other way round as
     _G["test"] = "This is a test"
    print(test)
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • Ok thanks. I come to this because the level indexes will be generated
    and added to the level string like "level" + "01", "level" + "02" and
    stored in the JSON file. Later on I need to use these names as variables.

    Regards,

    Marc
Sign In or Register to comment.