Variables Name for Lua Global Var

Posted by Tkl1129 on Tue 27 Dec 2011 03:13 PM — 6 posts, 20,967 views.

Hong Kong #0
Hi all,

I have a question for this coding method, please kindly advice, thanks.

Case:
Trigger - > Skill: Unarmed - 100 lv
Sword - 120 lv

Pattern %1 = skill name, %2 = lv


local a = tostring("lv_" .. "%1") 
local b = "%2"


SetVariables(a , b)



The Variables set in Mushclient was
Name : "lv_Sword"
Value: "120"

however this only use in Mush Global Var, how can I perform the same effect but only save in Lua Global Var?

what I want is
"lv_unarmed" - "100"
"lv_sword" - "120"
"lv_xxxx" - "xxx"

Normal Global var set in lua is

a = b

, but this will occur error

tostring("lv_" .. "%1") = "%2"


Do there any method can write in this way? I want to write in seperate var, not in Lua table.
USA Global Moderator #1
%x expansion happens before the script engine is run, so you can just do

lv_%1 = "%2"

But I think you should reconsider the decision to not do it inside a table. A table would guarantee that you can easily keep track of all of your dynamically created variables.
Amended on Tue 27 Dec 2011 04:26 PM by Fiendish
Hong Kong #2
Thanks, it's work, but a problem again.

For the skill name

1. Sword
2. Unarmed
3. Axe

This 3 cap in the code below is work,

lv_%1 = "%2"


but for this case won't be work, it occur error base on "=" & "-"

1. Big-sword
2. Big-Axe

I cannot cap in Lua Global Var "lv_Big-Axe"

Error Message:
[String "Trigger: "]:3: '=' expected near '-'

-_-"
USA Global Moderator #3
You're probably going to need to use the dostring function. Again, I think you should reconsider using table members.
Amended on Wed 28 Dec 2011 01:36 AM by Fiendish
Australia Forum Administrator #4
I strongly agree with Fiendish here. You are better off making a table and indexing into it. Then you can do this...


skills = skills or {}  -- make table if necessary

test = "Big-sword"

skills [test] = 42


Or even:


skills ["Big-sword"] = 42


However if you *must* use global variables <sigh> then you can do this:


_G ["lv_%1"] = "%2"


However I warn you that it is easy to serialize (save for tomorrow) a normal table, not the _G table.
Hong Kong #5
okok, I try to rewrite in table format :D, thanks.