• 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 Anyway to call functions in 050-functions.lua (so it calls it at start)

dudeim

Member
Joined
Oct 5, 2007
Messages
121
Reaction score
9
Hey,

I have a little problem currently,

I have this very big table that requires alot of data set that needs to be accessed globaly (so I need to set it befor hand and not in some creature/action/globalscript or whatever)

So I wrote this function that sets that data for me so I have to write alot less like this:

Lua:
CurrentNumber = 1
Data = {

            life={},
            mana={},
            stuff={},
     create = function()
             CurrentNumber = CurrentNumber+1
             return Data[CurrentNumber]
     end,

     addData = function(life,mana,stuff) --this function adds alot of data to the table
                     table.insert(life,{val=life,max=life*2})
                     table.insert(mana,{val=mana,max=mana*2})
                     table.insert(stuff,{val=stuff,max=stuff*2})
                     --alot more here but just for simplification;)
     end

}
--now I want to be able to call somewhere in 050-functions or somewhere else this:

Data1 = Data.create()
Data1.addData(150,50,13)
Data1.addData(250,0,5)
Data1.addData(50,200,50)
Data1.addData(125,100,25)

Data2 = Data.create()
Data2.addData(1150,150,113)
Data2.addData(1250,10,15)
Data2.addData(150,1200,150)
Data2.addData(1125,1100,125)

--etc....
Note: I'm trying to create something instance-like I have no idea if that even works (if you know if it works or if I'm doing it wrong or anything would be great to hear how I could fix it:p), I don't think it's the problem but it might, who knows;)
Also this is handwritten here in the editor so there might be some small errors:p
Thanks!
 
Lua:
DataObj = {}

function DataObj:new(_life, _mana, stuff)
	return setmetatable({
		life = _life
		mana = _mana
		stuff = _stuff},
		{__index = self}
	)
end


Data = {}

function addData(data, value)
	table.insert(data, value)
end

object = DataObj:new(150, 50, 13)
addData(Data, object)

Like this?
You should search for some Lua OOP tutorials.
 
Can I also do something mmore like this:

Lua:
object = {

function = new()
--do the stuff that needs to be done here, setting metatable etc...
end,

function = addData(life,mana,stuff)
table.insert(self,{life=life,mana=mana,stuff=stuff})
end,

function = doSomethingWithData()
self.life = self.life*2
end
}

Data1 = object:new()
Data1:addData(50,15,10)
Data1:doSomethingWithData()
Would something like that also work?
 
Bump
Also can i just create the global var Data1 in this example somewhere in 050-functions and add the data to it using the functions (so like I'm calling this without being it in a function)
 
That's not a problem for me I have a way around it.
Also this would work I think as these functions would be called at the start of the map (through 050-...) which would kinda be the same as setting the table itself wouldn't it?
 
Back
Top