Einfach ein paar Funktionen, die man als Basis zum objektorientierten Programmieren verwenden kann.
Wer gut ist kann sogar die Fähigkeit anwenden, es als Client-Server Syncronisation zu verwenden
Dummerweise habe ich eine Krankheit, die man "akute Erklärunlust" nennt. Das bedeutet, dass ihr das Script zwar verwenden dürft, wenn ihr aber nicht versteht was es macht, dann könnt ihr es ausdrucken, einen Papierflieger daraus basteln und halt so euren Spaß haben.
Ich wusste leider nicht wohin damit, da man das Codesnippet wohl kaum als "Spielmodus" bezeichnen kann.
Lua
--Allgemeine Frage: Brauche ich eine Objektliste?
--Objektorientiertes Modell sagt: NEIN!
--Nur eine Klassenliste wird gebraucht!
--eigentlich sollte eine Klasse von alleine ein Objekt erstellen können.
local aClassList = {};
local iObjectCount = 0;
function isObject(object)
return ((type(object) == "table") and (object.identifier));
end
function oocClass(sName,aFunctionList,constructor,destructor)
sName = tostring(sName);
if aClassList[sName] then
return;
end
aClassList[sName] = {};
local class = aClassList[sName];
for sFunctionName,fFunction in pairs(aFunctionList) do
if type(sFunctionName) == "string" and type(fFunction) == "function" then
class["f"..sFunctionName] = fFunction;
end
end
if not class.fDestruct then
class.fDestruct = fGeneralDestructor;
end
class.name = sName;
class.getVar = function(elm,sName) return elm["v"..tostring(sName)]; end
class.setVar = function(elm,sName,value) elm["v"..tostring(sName)] = value; end
class.constructor = constructor or (function(elm) end);
class.destructor = destructor or (function (elm) elm = nil; end);
return class;
end
function ooClassInherited(sName,sParental,aFunctionList,constructor,destructor)
local aParent = aClassList[sParental];
sName = tostring(sName);
if aClassList[sName] then
return;
end
aClassList[sName] = {};
local class = aClassList[sName];
for sFunctionName,fFunction in pairs(aParent) do
if type(sFunctionName) == "string" and type(fFunction) == "function" then
class[sFunctionName] = fFunction;
end
end
for sFunctionName,fFunction in pairs(aFunctionList) do
if type(sFunctionName) == "string" and type(fFunction) == "function" then
class["f"..sFunctionName] = fFunction;
end
end
if not class.fDestruct then
class.fDestruct = fGeneralDestructor;
end
class.name = sName;
class.constructor = (constructor or (class.constructor)) or (function(elm) end);
class.destructor = (destructor or (class.destructor)) or (function (elm) elm = nil; end);
return class;
end
function oocObject(sClass,aVariableList,sName)
if not aClassList[sClass] then
return;
end
iObjectCount = iObjectCount + 1;
-- aObjectList[iObjectCount] = {};
-- local object = aObjectList[iObjectCount];
local object = {};
for sString,fFunction in pairs(aClassList[sClass]) do
object[sString] = fFunction;
end
for sVarName,vVariable in pairs(aVariableList) do
if type(sVarName) == "string" and type(vVariable) ~= "function" then
object["v"..sVarName] = vVariable;
end
end
object.identifier = sName or iObjectCount;
if object.constructor then
object.constructor(object);
end
return object;
end
--SyncObjects: Syncronisierte Objekte, Client Server. Kann auch Server server syncronisieren.
oocClass("SYNCOBJ",{
CallFunction= function(sObjectName,sFunctionName,aVarList)
local object = OOC_SYNCOBJ_MAIN.vObj[sObjectName];
if object and isObject(object) then
object[sFunctionName](object,unpack(aVarList));
end
end,
Call= function (object,sFunctionName,aVarList)
if isObject(object) then
local sObjectName = object.identifier;
if getLocalPlayer and triggerServerEvent then
if not object.noserver then
triggerServerEvent("OOC_FuncSync",getLocalPlayer(),sObjectName,sFunctionName,aVarList)
else
object[sFunctionName](object,unpack(aVarList));
end
else
if not object.noclient then
if not object.ownerlist then
triggerClientEvent(getRootElement(),"OOC_FuncSync",getResourceRootElement(),sObjectName,sFunctionName,aVarList)
else
for i,elm in pairs(object.ownerlist) do
triggerClientEvent(elm,"OOC_FuncSync",getResourceRootElement(),sObjectName,sFunctionName,aVarList)
end
end
end
object[sFunctionName](object,unpack(aVarList));
end
end
end,
AddObject= function (object)
if object and isObject(object) then
OOC_SYNCOBJ_MAIN.vObj[object.identifier] = object;
end
end,
RegisterOwner=function (object)
if getLocalPlayer then
local sObjectName = object.identifier;
if object.fullsync then
--spezieller Trigger für eine Variablenliste
local aVariables = {};
for i,elm in pairs(object) do
if type(elm) ~= "function" then
aVariables[i] = elm;
end
end
triggerServerEvent("OOC_ClientObjCreated",getLocalPlayer(),sObjectName,aVariables);
end
end
end,
AddOwner= function (object,owner)
if object.ownerlist then
local sObjectName = object.identifier;
for i,elm in pairs(object.ownerlist) do
if object.ownerlist == owner then
return;
end
end
table.insert(object.ownerlist,owner);
if object.fullsync then
--spezieller Trigger für eine Variablenliste
local aVariables = {};
for i,elm in pairs(object) do
if type(elm) ~= "function" then
aVariables[i] = elm;
end
end
triggerClientEvent(player,"OOC_ClientObjCreated",getResourceRootElement(),sObjectName,aVariables);
end
end
end,
RemoveOwner= function (object,owner)
if object.ownerlist then
for i,elm in pairs(object.ownerlist) do
if object.ownerlist == owner then
object.ownerlist[i] = nil;
end
end
end
end
});
------------------------------------------------------------------------
------------------------------------------------------------------------
OOC_SYNCOBJ_MAIN = oocObject("SYNCOBJ",{Obj = {}});
------------------------------------------------------------------------
------------------------------------------------------------------------
------------------------------------------------------------------------
------------------------------------------------------------------------
function syncFunctionCall(sObjectName,sFunctionName,aVarList)
local object = OOC_SYNCOBJ_MAIN.vObj[sObjectName];
if object and object[sFunctionName] and object.fCallFunction then
if (getLocalPlayer and triggerServerEvent) then
OOC_SYNCOBJ_MAIN.fCallFunction(sObjectName,sFunctionName,aVarList);
else
object.fCall(object,sFunctionName,aVarList)
end
end
end
function syncObjectCreation(sObjectName,aVarList)
local owner = source;
local object = OOC_SYNCOBJ_MAIN.vObj[sObjectName];
if object and object[sFunctionName] and object.fCallFunction then
if (getLocalPlayer and triggerServerEvent) then
for i,elm in pairs(aVarList) do
object[i] = elm;
end
else
object.AddOwner(object,owner)
end
end
end
addEvent("OOC_FuncSync",true);
addEventHandler("OOC_FuncSync",getRootElement(),syncFunctionCall);
addEvent("OOC_ClientObjCreated",true);
addEventHandler("OOC_ClientObjCreated",getRootElement(),syncObjectCreation);
ooClassInherited("MASTERSO","SYNCOBJ",{},
function (object)
OOC_SYNCOBJ_MAIN.vObj[object.identifier] = object;
end,
function (object)
OOC_SYNCOBJ_MAIN.vObj[object.identifier] = nil;
object = nil;
end);
Alles anzeigen