• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Simple Global Variable Problem

Arn

Member
Joined
Mar 8, 2010
Messages
282
Reaction score
18
Hi, I'm trying to get a global variable in a strange way. Please take a look and see if you can tell what I need to do. (will +rep)

This is the recipe for the weapon.
Code:
--8000
RECIPE_CLERICAL_MACE = 	{
						itemid = 2423,
						mats = {5880},
						count =  {10},
						cost = 300
						}

This is the table/array that I'm using to access the different recipes.

Code:
craftingList = 
{
[0] = {id = 8000, name = 'clerical mace', label = 'RECIPE_CLERICAL_MACE'},
[1] = {id = 8001, name = 'jagged sword', label = 'RECIPE_JAGGED_SWORD'},
[2] = {id = 8002, name = 'orcish axe', label = 'RECIPE_ORCISH_AXE'},
[3] = {id = 8003, name = 'spike sword', label = 'RECIPE_SPIKE_SWORD'}
}

Now heres what I'm trying to do.

Code:
local var = craftingList[0].label.itemid

It should find that the itemid is 2423. Instead it says the value is nil. I tried stringToVariant assuming that that function converts a string to a variable, but that didn't work. Any suggestions?

-Arn
 
Lua:
{
	[1] = {id = 8000, name = 'clerical mace', label = 'RECIPE_CLERICAL_MACE'},
	[2] = {id = 8001, name = 'jagged sword', label = 'RECIPE_JAGGED_SWORD'},
	[3] = {id = 8002, name = 'orcish axe', label = 'RECIPE_ORCISH_AXE'},
	[4] = {id = 8003, name = 'spike sword', label = 'RECIPE_SPIKE_SWORD'}
}


for i = 1, 4 do
	local varLabel, varName, varId = craftingList[i].label, craftingList[i].name, craftingList[i].id
end
Not tested.
 
Right, but varLabel would just be a variable holding a string that says 'RECIPE_CLERICAL_MACE'. So when I do varLable.itemid then it doesn't know what I'm talking about.

It think I found my solution anyway though. I'm going to restructure the way recipes are stored to make it one global array. See below.

Code:
RECIPES = 
{
[0] = {itemid= 5555, mats = {444,3355,4563}, cost = 500}
....
...
}
 
I guess you've solved it, but in case you'd be interested in what might have been causing the problem(note that this is just my guess, could be wrong):

I believe the reason it return NIL is because craftingList[0].label would, as you said, return the string 'RECIPE_CLERICAL_MACE', which would not point to the variable with the same name. If this is the case; Does anyone know if(how) it's possible to get the string to point to that variable? I would like to know :p
 
Either just replace the string with the variable name
Lua:
[0] = {id = 8000, name = 'clerical mace', label = RECIPE_CLERICAL_MACE}


OR this:

Lua:
local var = loadstring("return " .. craftingList[0].label)()  

var -- this is the table
var.itemid -- itemid etc

Explanation:
loadstring("return " .. craftingList[0].label) is equal to:
Lua:
function ()
return RECIPE_CLERICAL_MACE
end
and you add () to call the function and get the return value

Rep :D
 
Either just replace the string with the variable name
Lua:
[0] = {id = 8000, name = 'clerical mace', label = RECIPE_CLERICAL_MACE}


OR this:

Lua:
local var = loadstring("return " .. craftingList[0].label)()  

var -- this is the table
var.itemid -- itemid etc

Explanation:
loadstring("return " .. craftingList[0].label) is equal to:
Lua:
function ()
return RECIPE_CLERICAL_MACE
end
and you add () to call the function and get the return value

Rep :D

I love you.

The first part is so obvious (just putting in the variable name). Big derp on my part.

Second part is very interesting and good to know, thank you!
 
Back
Top