• 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 Variables Lua

massuco

Brazilian, sorry for my bad english XD
Joined
Feb 17, 2013
Messages
199
Solutions
8
Reaction score
22
Location
Brasil
Hi, I want to define some Local Variables on my npc script
I did this:
Lua:
local DREAM_MATTER = 5145
local CLUSTER_OF_SOLACE = 5144
local CRUDE_UMBRAL_BLADE = 5147
local UMBRAL_BLADE = 5148
local UMBRAL_MASTER_BLADE = 5149

How can I improve this? Im saying about one array, or table, idk. something that I dont need to write local every line.
THX
 
Solution
Like this?
Code:
local items = {
DREAM_MATTER = 5145,
CLUSTER_OF_SOLACE = 5144,
CRUDE_UMBRAL_BLADE = 5147,
UMBRAL_BLADE = 5148,
UMBRAL_MASTER_BLADE = 5149,
CRUDE_UMBRAL_AXE = 5150,
UMBRAL_AXE = 5151
}


If I write the variables like this, I need to call the variable with ".items"
doPlayerAddItem(cid, items.CRUDE_UMBRAL_BLADE, 1)

and I want to call the variables without write ".items" every time, like this.
doPlayerAddItem(cid, CRUDE_UMBRAL_BLADE, 1)
That's correct.

A third option might be to put it all in 1 line.
There's no real benefit between the different ways, beyond how your script aestetically looks though.
Lua:
-- 1)
local DREAM_MATTER = 5145
local CLUSTER_OF_SOLACE = 5144
local CRUDE_UMBRAL_BLADE = 5147
local UMBRAL_BLADE...
Lua:
local variables = {
   5145, -- DREAM_MATTER
   5144, -- CLUSTER_OF_SOLACE
   5147, -- CRUDE_UMBRAL_BLADE
   5148, -- UMBRAL_BLADE
   5149 -- UMBRAL_MASTER_BLADE
}

variables[1] -- means DREAM_MATTER
variables[4] -- means UMBRAL_BLADE
ect.
 
You could add this to ex global.lua, that way you can load it from any script.
Lua:
local npcVariables = {
    DREAM_MATTER = 5145,
    CLUSTER_OF_SOLACE = 5144,
    CRUDE_UMBRAL_BLADE = 5147,
    UMBRAL_BLADE = 5148,
    UMBRAL_MASTER_BLADE = 5149,
}
 
You could add this to ex global.lua, that way you can load it from any script.
Lua:
local npcVariables = {
    DREAM_MATTER = 5145,
    CLUSTER_OF_SOLACE = 5144,
    CRUDE_UMBRAL_BLADE = 5147,
    UMBRAL_BLADE = 5148,
    UMBRAL_MASTER_BLADE = 5149,
}

The Tag "npcVariables" is a random name or is a function that will work with all npcs?

I want to define this variables for only one script. I will define the variables on the script, and this variable will work only on this script.
and for example, if I use:
doPlayerRemoveItem(cid,DREAM_MATTER, 1)

I will use like this, or I need to use:
doPlayerRemoveItem(cid, npcVariables.CRUDE_UMBRAL_AXE, 1)

?
 
The Tag "npcVariables" is a random name or is a function that will work with all npcs?

I want to define this variables for only one script. I will define the variables on the script, and this variable will work only on this script.
and for example, if I use:
doPlayerRemoveItem(cid,DREAM_MATTER, 1)

I will use like this, or I need to use:
doPlayerRemoveItem(cid, npcVariables.CRUDE_UMBRAL_AXE, 1)

?

Random name, you can name a variable w/e you want.
If you want it for one script only you add local before the variable name (local npcVraiables)
If you want it global place it in global.lua and don't use local (npcVariables = { ) etc

In that case place it in the npc Lua file and use local before the variable name.
 
Like this?
Code:
local items = {
DREAM_MATTER = 5145,
CLUSTER_OF_SOLACE = 5144,
CRUDE_UMBRAL_BLADE = 5147,
UMBRAL_BLADE = 5148,
UMBRAL_MASTER_BLADE = 5149,
CRUDE_UMBRAL_AXE = 5150,
UMBRAL_AXE = 5151
}


If I write the variables like this, I need to call the variable with ".items"
doPlayerAddItem(cid, items.CRUDE_UMBRAL_BLADE, 1)

and I want to call the variables without write ".items" every time, like this.
doPlayerAddItem(cid, CRUDE_UMBRAL_BLADE, 1)
 
Like this?
Code:
local items = {
DREAM_MATTER = 5145,
CLUSTER_OF_SOLACE = 5144,
CRUDE_UMBRAL_BLADE = 5147,
UMBRAL_BLADE = 5148,
UMBRAL_MASTER_BLADE = 5149,
CRUDE_UMBRAL_AXE = 5150,
UMBRAL_AXE = 5151
}


If I write the variables like this, I need to call the variable with ".items"
doPlayerAddItem(cid, items.CRUDE_UMBRAL_BLADE, 1)

and I want to call the variables without write ".items" every time, like this.
doPlayerAddItem(cid, CRUDE_UMBRAL_BLADE, 1)
That's correct.

A third option might be to put it all in 1 line.
There's no real benefit between the different ways, beyond how your script aestetically looks though.
Lua:
-- 1)
local DREAM_MATTER = 5145
local CLUSTER_OF_SOLACE = 5144
local CRUDE_UMBRAL_BLADE = 5147
local UMBRAL_BLADE = 5148
local UMBRAL_MASTER_BLADE = 5149

-- doPlayerAddItem(cid, CRUDE_UMBRAL_BLADE, 1)
-- 1)

-- 2)
local DREAM_MATTER, CLUSTER_OF_SOLACE, CRUDE_UMBRAL_BLADE, UMBRAL_BLADE, UMBRAL_MASTER_BLADE = 5145, 5144, 5147, 5148, 5149

-- doPlayerAddItem(cid, CRUDE_UMBRAL_BLADE, 1)
-- 2)

-- 3)
local items = {
   5145, -- DREAM_MATTER
   5144, -- CLUSTER_OF_SOLACE
   5147, -- CRUDE_UMBRAL_BLADE
   5148, -- UMBRAL_BLADE
   5149 -- UMBRAL_MASTER_BLADE
}

-- doPlayerAddItem(cid, items[3], 1)
-- 3)

-- 4)
local items = {
   DREAM_MATTER = 5145,
   CLUSTER_OF_SOLACE = 5144,
   CRUDE_UMBRAL_BLADE = 5147,
   UMBRAL_BLADE = 5148,
   UMBRAL_MASTER_BLADE = 5149
}

-- doPlayerAddItem(cid, items.CRUDE_UMBRAL_BLADE, 1)
-- 4)
 
Solution
That's correct.

A third option might be to put it all in 1 line.
There's no real benefit between the different ways, beyond how your script aestetically looks though.
Lua:
-- 1)
local DREAM_MATTER = 5145
local CLUSTER_OF_SOLACE = 5144
local CRUDE_UMBRAL_BLADE = 5147
local UMBRAL_BLADE = 5148
local UMBRAL_MASTER_BLADE = 5149

-- doPlayerAddItem(cid, CRUDE_UMBRAL_BLADE, 1)
-- 1)

-- 2)
local DREAM_MATTER, CLUSTER_OF_SOLACE, CRUDE_UMBRAL_BLADE, UMBRAL_BLADE, UMBRAL_MASTER_BLADE = 5145, 5144, 5147, 5148, 5149

-- doPlayerAddItem(cid, CRUDE_UMBRAL_BLADE, 1)
-- 2)

-- 3)
local items = {
   5145, -- DREAM_MATTER
   5144, -- CLUSTER_OF_SOLACE
   5147, -- CRUDE_UMBRAL_BLADE
   5148, -- UMBRAL_BLADE
   5149 -- UMBRAL_MASTER_BLADE
}

-- doPlayerAddItem(cid, items[3], 1)
-- 3)

-- 4)
local items = {
   DREAM_MATTER = 5145,
   CLUSTER_OF_SOLACE = 5144,
   CRUDE_UMBRAL_BLADE = 5147,
   UMBRAL_BLADE = 5148,
   UMBRAL_MASTER_BLADE = 5149
}

-- doPlayerAddItem(cid, items.CRUDE_UMBRAL_BLADE, 1)
-- 4)

Niicee! Thanks!
But in this case, for me, i have added this variables to global.lua, because its item ids, and I might use it again.
 
Back
Top