• 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] Attempting to create a global table

victorafael

New Member
Joined
Jun 5, 2011
Messages
3
Reaction score
0
I'm developing a code and I need to populate a global table wich will be accessed by npcs, talkactions, actions, etc.

so.. i've created a file "table.lua" and placed it on the Data folder.

Code:
table = {}

function insertInTable(key, content)
    table[key] = table
    debugPrint("table size is now: " .. #table) -- will output 'table size is now: 1'
end

function getTableValue(key)
    debugPrint("now table size is: " .. #table) -- will output 'table size is now: 0' :(
    return table[key] -- a nil value
end

then in the global.lua i call the table.lua via dofile('.data/table.lua').

so.. my code is not working..
when i try something like this:

Code:
-- file 1
    local flagType = 1 --item.actionid
    local playerName = getCreatureName(cid)
    local playerPos = getCreaturePosition(cid)
    local eventType = CEVT_FLAGPLACED
    local _time = os.clock()
    local _team = getPlayerStorageValue(cid, 2200)
    local tbl = {name = playerName, pos = playerPos, event = eventType, time = _time, team = _team}

    insertInTable(flagType,tbl) -- 'table size is now: 1'

-- file 2
    -- checking the table for events...
    local check = getTableValue(1) 'table size is now: 0'
    -- (check is nil)

(there will be many values on the table.. so i think that i cannot store it on the global storage)

Well... I've placed a debugPrint on the start of table.lua.. and i've noticed that the file is executed many times when the server is loading...

so.. i guess that i'm missing some reference here...


There's any way to execute the file just one time.. (any kind of requireonce() instead of dofile()) ?

if is not.. there's some way to make a really global variable?



thanks for your time..

----
I dont know how that happened but i'm sure that i've started the thread on Resources/Requests (i even dont know if there's the right place anyway)
 
Last edited:
I need to do this aswell; one thing to check is that u are not including a file that includes the file you are using.
ie, file1 one has dofile "file2.lua" and file2 has dofile "file1.lua", this will lead to some nasty looping go from file to file.

The problem have is when i change a variable with a function in talkactions, it is not change for actions. is there a way to make truly global variables?
 
You want this to save contend(strings) to a file?
I think you want to use IO functions, look in here for them lua-users wiki: Io Library Tutorial

no, what im wanting is to be able to build a table with a talk actions and then use it in actions. i have the table initialized as a global variable, but it wont work. it will work with all other talkactions and if i build it in an actions script, then all actions can use it; however. i want them both to be able to use it as well as spells.

btw, i use OTServ Version 0.6.3_SVN
 
Code:
table = {}

function insertInTable(key, content)
    table[key] = content
    debugPrint("table size is now: " .. #table) -- will output 'table size is now: 1'
end

function getTableValue(key)
    debugPrint("now table size is: " .. #table) -- will output 'table size is now: 1' :DD
    return table[key]
end
Just a small error p:
 
Dear victor... will not work. The lua states are not the same.
You have to understand, all states load ONCE the global.lua If you change something in global and the load a module will be there when you use. but if they load and then you change in file will not happen nothing.

You can use a function called saveTable
LUA:
function tableToString(tb) -- by Mock
	if type(tb) ~= "table" then
		return nil, error("bad argument #1 to 'saveTable' (table expected, got "..type(tb)..")", 2)
	end
	local str = "{"
	for k,d in pairs(tb) do
		if type(k) == 'string' then
			if type(d) == 'string' then
				str = str..""..k.."='"..d.."',"
			elseif type(d) == 'number' or type(d) == 'boolean' then
				str = str..""..k.."="..tostring(d)..","
			elseif type(d) == 'table' then
				str = str..'{'
				for e,f in pairs(d) do
					if type(e) == 'string' then
						if type(f) == 'string' then
							str = str..""..e.."='"..f.."',"
						elseif type(f) == 'number' or type(e) == 'boolean' then
							str = str..""..e.."="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str..""..e.."="..tableToString(f)..","
						end
					elseif type(e) == 'number' then
						if type(f) == 'string' then
							str = str.."["..e.."]='"..f.."',"
						elseif type(f) == 'number' or type(f) == 'boolean' then
							str = str.."["..e.."]="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str.."["..e.."]="..tableToString(f)..","
						end
					end
				end
				str = str..'},'
			end
		elseif type(k) == 'number' then
			if type(d) == 'string' then
				str = str.."["..k.."]='"..d.."',"
			elseif type(d) == 'number' or type(d) == 'boolean' then
				str = str.."["..k.."]="..tostring(d)..","
			elseif type(d) == 'table' then
				str = str..'{'
				for e,f in pairs(d) do
					if type(e) == 'string' then
						if type(f) == 'string' then
							str = str..""..e.."='"..f.."',"
						elseif type(f) == 'number' or type(e) == 'boolean' then
							str = str..""..e.."="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str..""..e.."="..tableToString(f)..","
						end
					elseif type(e) == 'number' then
						if type(f) == 'string' then
							str = str.."["..e.."]='"..f.."',"
						elseif type(f) == 'number' or type(f) == 'boolean' then
							str = str.."["..e.."]="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str.."["..e.."]="..tableToString(f)..","
						end
					end
				end
				str = str..'},'
			end
		end
	end
	local str = str.."}"
	if string.sub(str,string.len(str)-2,string.len(str)-2) == "," then
		str = string.sub(str,0,string.len(str)-3).."}"
	end
	return str
end
But you cannot save lua function.
use the iolib
LUA:
file = io.open('table_file','wb')
if file then
    file:write(tableToString(tb))
    file:close()
end


If you are using open tibia go to luascript.cpp and find //luaL_openlibs and remove the slashes //

in here we use TFS not otserv, try asking in OTfans

Here YOU use tfs. This forum team develop the TFS but nothing says to here we cannot give support to open tibia.
The same on otfans, the people there give a lil' support on TFS.
If you dont know tfs is open tibia based and 70% of his source still open tibia.
 
@Up
Good function, can be simplified a bit, but works right.
But, i think he just want a table for some simple uses...

@Topic
If you want to use for just one state, your function (corrected) will work,
else use mock's one.
 
Back
Top