Variables within variable names?

Description: Archive discussion
============================
Original description:
Official subforum for LunaLua - an extension framework for SMBX Engine Version 1.3 (which a core of SMBX 2.0). Share your creations that require the use of LunaLUA. You may also share info/tutorials on how to use LunaLUA.

Sambo M
Topic author, Count
Count
Avatar
Sambo M
Topic author, Count
Count
Age: 25
Reputation: 15
Posts: 264
Joined: 27 Jun 2014

Post #1by Sambo » 23 Apr 2015, 3:50

That probably sounds confusing. What I mean is, is there a way to make a variable that can have a different name depending on the value of another variable? I have variables in all the levels I've made that do the same things, but they need to be independently set in each level. To reduce the amount of coding that is required, I need to have a way to change variable names. For example, if I have this variable:

Code: Select all

saveData:get("myvar_xxx")
(saveData is being used with the new Data class)
Is there a way to make "xxx" a variable?
Image
Current Project:
Image

Kevsoft M
Angry developer
Angry developer
Avatar
Kevsoft M
Angry developer
Angry developer
Age: 24
Reputation: 93
Posts: 379
Joined: 26 Apr 2014
Location: Austria
Website

Post #2by Kevsoft » 23 Apr 2015, 15:35

Yes you can, however there are some restirictions and recommendations:
1.) You can only make global variables out of them, not local variables.
2.) To prevent an overwrite of an already exsisting variable you should put them in a table!

Code: Select all

for key,value in pairs(saveData:get()) do
_G[key] = value
end


or with tables:

Code: Select all

local myVars = {}
...
for key,value in pairs(saveData:get()) do
myVars[key] = value
end


if you want to to check for the "myvar_" prefix you have to use string library.
▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬ Signature.start() ▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬
▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬ Signature.end() ▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬

Sambo M
Topic author, Count
Count
Avatar
Sambo M
Topic author, Count
Count
Age: 25
Reputation: 15
Posts: 264
Joined: 27 Jun 2014

Post #3by Sambo » 25 Apr 2015, 21:10

I found an even simpler way to do it. I just put this code in lunadll.lua:

Code: Select all

_G["level_code"] = "xxx"

And this in lunaworld.lua:

Code: Select all

saveData:set(level_code .. "-dc-1", "collected")

dc-1 is a variable that indicates if a dragon coin in the level was collected. Since there are 5 dragon coins in each level, it seemed a waste to do all that coding that I had to do before I implemented this.
Image
Current Project:
Image


Return to “LunaLua - Extension Framework”

Who is online (over the past 5 minutes)

Users browsing this forum: 1 guest