• 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!

Basic LUA template for state driven scripts

Stellow

C++/C#/PHP/LUA
Joined
Oct 23, 2008
Messages
1,106
Reaction score
214
Location
Germany
GitHub
eubrunomiguel
If you like to store data while executing an event, you may store these data inside a global table which can or most likely will not make it available for different branches of scripting (creature scripts/movements/actions...).

The best way that I have found to accomplish this is to create a metatable and store it on the database.

Follows the template:

Code:
Event = {}
Event.__index = Event

Event.Config = {
    -- config variables/tables/arrays/objects
}

function Event.new(id, morevariables)
  local event =
  {
    id                             = id,
    morevariables       = morevariables or default value,
  }

  setmetatable(event, Event)
  return event
end

function Event.get(id)
  local tmp = table.unserialize(Game.getStorageValue(id))
  return Event.new(tmp.id, tmp.morevariables)
end

function Event:save()
  Game.setStorageValue(self.id, table.serialize({
    id                                = self.id,
    morevariables          = morevariables,
  }))
end

function Event:changeState(foo)
   self.morevariables = foo
end

Now if you want to trigger some function you can for example

Code:
function onKill(creature, target)

    local event = Event.get(storageid)
    event:changeState("i dont know")
    event:save()

end
 
but where is table.serialize and table.unserialize?
 
Hi, thank you for the script. I was just wandering, how can I choose an id that will not conflict with another value on the data base?
 
Back
Top