Basic LUA template for state driven scripts

Stellow

C++/C#/PHP/LUA
Joined
Oct 23, 2008
Messages
1,010
Best answers
0
Reaction score
132
Location
Germany
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
 

Delusion

Divine Intellect
Support Team
Joined
Feb 14, 2015
Messages
5,049
Best answers
472
Reaction score
2,678
but where is table.serialize and table.unserialize?
 

Fred =D

New Member
Joined
Jun 3, 2019
Messages
5
Best answers
0
Reaction score
1
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?
 
Top