From 8787b447232cbd144853db603f0f485e13c1a158 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 5 Feb 2023 11:47:52 +0100 Subject: [PATCH] v1.0.2 - improved documentation and examples --- README.md | 124 ++++++++++++++++++++++++---------------- config.json | 2 +- source/unit/onStart.lua | 112 ++++++++++++++++++++---------------- 3 files changed, 137 insertions(+), 101 deletions(-) diff --git a/README.md b/README.md index 06c1da4..9f5a92a 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,9 @@ System onUpdate and onFlush events are now built with coroutines, in the framewo ## Code Examples +### System > onUpdate and onFlush + + ```lua --Functions to load as coroutines that will be runned in system > onUpdate (based on FPS) local system_update = {} @@ -48,6 +51,8 @@ system_update.co2 = function () end end +Script.system:onUpdate(system_update) --loading coroutines for system > onUpdate + --Functions to load as coroutines that will be runned in system > onFlush (60 times / s) local system_flush = {} system_flush.co1 = function () @@ -63,100 +68,119 @@ system_flush.co2 = function () end end +Script.system:onFlush(system_flush) --loading coroutines for system > onFlush + +``` + +### System > onActionStart, onActionStop and onActionLoop + +```lua + --Function to run on actions local system_action_start = {} system_action_start[Script.system.ACTIONS.BRAKE] = function() system.print("I'm braking"); end +Script.system:onActionStart(system_action_start) --loading all "actionStart" functions + local system_action_stop = {} system_action_stop[Script.system.ACTIONS.BRAKE] = function() system.print("I stopped braking"); end +Script.system:onActionStop(system_action_stop) --loading all "actionStop" functions + local system_action_loop = {} system_action_loop[Script.system.ACTIONS.BRAKE] = function() system.print("I'm still braking"); end +Script.system:onActionLoop(system_action_loop) --loading all "actionLoop" functions + +``` +### System > onInputText + +```lua --Function to run when input text to the lua chat -local system_inputText = function (text) +Script.system:onInputText(function (text) system.print("Input: " .. text) -end +end) +``` + +### unit > onStop +```lua --Function to run when the program is stopping -local unit_onstop = function () +Script.unit:onStop(function() system.print("Program is stopping") -end +end) + +``` + +### unit > Timers + +```lua +--[[ + Here how to add a timer + @param name: the name of the timer, used to remove it with Script.unit:stopTimer(name) + @param delay: the delay between each call of the function in seconds + @param func: the function to call +]] +Script.unit:setTimer("hello", 1, function()system.print("hello")end) --add a timer displaying "hello" every seconds +Script.unit:setTimer("hello5", 5, function()system.print("hello 5")end) --add a timer displaying "hello 5" every 5 seconds + +Script.unit:stopTimer("hello") --stop the timer "hello" + +``` + +### player > onParentChanged + +```lua --Function to run when the player change parent -local player_onparentchanged = function (oldParent, newParent) +Script.player:onParentChanged(function (oldParent, newParent) system.print("Player changed parent from ID "..oldParent.." to ID "..newParent) -end +end) + +``` + +### construct events + +```lua --Function to run when the construct is docked or undocked -local construct_onDocked = function(id) +Script.construct:onDocked(function(id) system.print("Construct docked on ID "..id) -end -local construct_onUndocked = function(id) +end) +Script.construct:onUndocked(function(id) system.print("Construct undocked from ID "..id) -end +end) --Function to run when a player board the construct -local construct_onplayerboarded = function(id) +Script.construct:onPlayerBoarded(function(id) system.print("Player with ID " .. id .. " boarded construct") -end +end) --Function to run when a player enter the VR Station -local construct_vrstationentered = function(id) +Script.construct:onVRStationEntered(function(id) system.print("Player with ID " .. id .. " entered VR Station") -end +end) --Function to run when another construct is docked on this construct -local construct_constructdocked = function(id) +Script.construct:onConstructDocked(function(id) system.print("Construct with ID " .. id .. " docked on this construct") -end +end) --Function to run when pvp timer is changing state -local construct_onpvptimer = function(active) +Script.construct:onPvPTimer(function(active) if active then system.print("PVP is now active") else system.print("PVP is now inactive") end -end - ---[[ - Here how to load the functions in the framework -]] -Script.system:onUpdate(system_update) --loading coroutines for system > onUpdate -Script.system:onFlush(system_flush) --loading coroutines for system > onFlush - -Script.system:onActionStart(system_action_start) --loading all "actionStart" functions -Script.system:onActionStop(system_action_stop) --loading all "actionStop" functions -Script.system:onActionLoop(system_action_loop) --loading all "actionLoop" functions - -Script.system:onInputText(system_inputText) --loading the function to trigger when input text in lua chat - ---[[ - Here how to add a timer - @param name: the name of the timer, used to remove it with Script.unit:stopTimer(name) - @param delay: the delay between each call of the function in seconds - @param func: the function to call -]] -Script.unit:setTimer("hello", 1, function()system.print("hello")end) --add a timer displaying "hello" every seconds -Script.unit:setTimer("hello5", 5, function()system.print("hello 5")end) --add a timer displaying "hello 5" every 5 seconds -Script.unit:onStop(unit_onstop) --loading the function to trigger when the program is stopping - -Script.player:onParentChanged(player_onparentchanged) --loading the function to trigger when the player change parent - -Script.construct:onDocked(construct_onDocked) --loading the function to trigger when the construct is docked -Script.construct:onUndocked(construct_onUndocked) --loading the function to trigger when the construct is undocked -Script.construct:onPlayerBoarded(construct_onplayerboarded) --loading the function to trigger when a player board the construct -Script.construct:onVRStationEntered(construct_vrstationentered) --loading the function to trigger when a player enter the VR Station -Script.construct:onConstructDocked(construct_constructdocked) --loading the function to trigger when another construct is docked on this construct -Script.construct:onPvPTimer(construct_onpvptimer) --loading the function to trigger when pvp timer is changing state +end) ``` -## System Actions: +### System Actions: to use with `Script.system.ACTIONS[ActionKey]` diff --git a/config.json b/config.json index 9d8d7c2..174ddc8 100644 --- a/config.json +++ b/config.json @@ -1 +1 @@ -{"events":[],"handlers":[{"code":"Script.system:update()","filter":{"args":[],"signature":"onUpdate()","slotKey":"-4"},"key":"10"},{"code":"Script.system:inputText(text)","filter":{"args":[{"variable":"*"}],"signature":"onInputText(text)","slotKey":"-4"},"key":"11"},{"code":"Script.system:flush()","filter":{"args":[],"signature":"onFlush()","slotKey":"-4"},"key":"12"},{"code":"Script.system:actionStop(action)","filter":{"args":[{"variable":"*"}],"signature":"onActionStop(action)","slotKey":"-4"},"key":"13"},{"code":"Script.system:actionStart(action)","filter":{"args":[{"variable":"*"}],"signature":"onActionStart(action)","slotKey":"-4"},"key":"14"},{"code":"Script.system:actionLoop(action)","filter":{"args":[{"variable":"*"}],"signature":"onActionLoop(action)","slotKey":"-4"},"key":"15"},{"code":"Script.player:parentChanged(oldId, newId)","filter":{"args":[{"variable":"*"},{"variable":"*"}],"signature":"onParentChanged(oldId,newId)","slotKey":"-3"},"key":"3"},{"code":"Script.construct:VRStationEntered(id)","filter":{"args":[{"variable":"*"}],"signature":"onVRStationEntered(id)","slotKey":"-2"},"key":"6"},{"code":"Script.construct:undocked(id)","filter":{"args":[{"variable":"*"}],"signature":"onUndocked(id)","slotKey":"-2"},"key":"8"},{"code":"Script.construct:PvPTimer(active)","filter":{"args":[{"variable":"*"}],"signature":"onPvPTimer(active)","slotKey":"-2"},"key":"4"},{"code":"Script.construct:playerBoarded(id)","filter":{"args":[{"variable":"*"}],"signature":"onPlayerBoarded(id)","slotKey":"-2"},"key":"7"},{"code":"","filter":{"args":[{"variable":"*"}],"signature":"onDocked(id)","slotKey":"-2"},"key":"9"},{"code":"Script.construct:constructDocked(id)","filter":{"args":[{"variable":"*"}],"signature":"onConstructDocked(id)","slotKey":"-2"},"key":"5"},{"code":"Script.unit:timer(tag)","filter":{"args":[{"variable":"*"}],"signature":"onTimer(tag)","slotKey":"-1"},"key":"1"},{"code":"Script.unit:stop()","filter":{"args":[],"signature":"onStop()","slotKey":"-1"},"key":"2"},{"code":"--[[\n DU-LUA-Framework by Jericho\n Permit to code easier by grouping most of the code in a single event Unit > Start\n Unminified Source available here: https://github.com/Jericho1060/du-lua-framework\n]]--\nlocal a=system.print;local b=error;local c=pcall;local d=assert;local e=coroutine;local f=e.create;local g=e.status;local h=e.resume;local i=\"dead\"local j=\"suspended\"function runFunction(k,l,...)local m,n=c(k,...)if not m then b(l..n)end end;local o={__index={cos={update={},flush={}},fns={update={},flush={},action={start={},stop={},loop={}},inputText=nil,start=nil,stop=nil},ACTIONS={FORWARD=\"forward\",BACKWARD=\"backward\",YAW_LEFT=\"yawleft\",YAW_RIGHT=\"yawright\",STRAFE_LEFT=\"strafeleft\",STRAFE_RIGHT=\"straferight\",LEFT=\"left\",RIGHT=\"right\",UP=\"up\",DOWN=\"down\",GROUND_ALTITUDE_UP=\"groundaltitudeup\",GROUND_ALTITUDE_DOWN=\"groundaltitudedown\",LEFT_ALT=\"lalt\",LEFT_SHIFT=\"lshift\",GEAR=\"gear\",LIGHT=\"light\",BRAKE=\"brake\",OPTION_1=\"option1\",OPTION_2=\"option2\",OPTION_3=\"option3\",OPTION_4=\"option4\",OPTION_5=\"option5\",OPTION_6=\"option6\",OPTION_7=\"option7\",OPTION_8=\"option8\",OPTION_9=\"option9\",LEFT_MOUSE=\"leftmouse\",STOP_ENGINES=\"stopengines\",SPEED_UP=\"speedup\",SPEED_DOWN=\"speeddown\",ANTIGRAVITY=\"antigravity\",BOOSTER=\"booster\"},main={update=f(function()end),flush=f(function()end)},update=function(self)local p=g(self.main.update)if p==i then self.main.update=f(function()self:runUpdate()end)elseif p==j then d(h(self.main.update))end end,flush=function(self)local p=g(self.main.flush)if p==i then self.main.flush=f(function()self:runFlush()end)elseif p==j then d(h(self.main.flush))end end,action=function(self,q,r)if self.fns.action[q][r]then runFunction(self.fns.action[q][r],\"System Action \"..q..\" Error: \")end end,actionStart=function(self,r)self:action('start',r)end,actionStop=function(self,r)self:action('stop',r)end,actionLoop=function(self,r)self:action('loop',r)end,inputText=function(self,s)if self.fns.inputText then runFunction(self.fns.inputText,\"System Input Text Error: \",s)end end,runUpdate=function(self)for t,u in pairs(self.cos.update)do local m=g(u)if m==i then self.cos.update[t]=f(self.fns.update[t])elseif m==j then d(h(u))end end end,runFlush=function(self)for t,u in pairs(self.cos.flush)do local m=g(u)if m==i then self.cos.flush[t]=f(self.fns.flush[t])elseif m==j then d(h(u))end end end,onUpdate=function(self,v)for t,w in pairs(v)do self.fns.update[t]=w;self.cos.update[t]=f(w)end end,onFlush=function(self,v)for t,w in pairs(v)do self.fns.flush[t]=w;self.cos.flush[t]=f(w)end end,onAction=function(self,q,v)for t,w in pairs(v)do self.fns.action[q][t]=w end end,onActionStart=function(self,v)self:onAction(\"start\",v)end,onActionStop=function(self,v)self:onAction(\"stop\",v)end,onActionLoop=function(self,v)self:onAction(\"loop\",v)end,onInputText=function(self,k)self.fns.inputText=k end}}local x={__index={timers={},stopFn=function()end,timer=function(self,y)if self.timers[y]then runFunction(self.timers[y],\"Unit Timer \"..y..\" Error: \")end end,setTimer=function(self,y,z,k)self.timers[y]=k;unit.setTimer(y,z)end,stopTimer=function(self,y)unit.stopTimer(y)self.timers[y]=nil end,onStop=function(self,k)self.stopFn=k end,stop=function(self)if self.stopFn then runFunction(self.stopFn,\"Unit Stop Error: \")end end}}local A={__index={parentChangedFn=function(B,C)end,onParentChange=function(self,k)self.parentChangedFn=k end,parentChanged=function(self,B,C)if self.parentChangedFn then runFunction(self.parentChangedFn,\"Player Parent Changed Error: \",B,C)end end}}local D={__index={dockedFn=function(E)end,onDocked=function(self,k)self.dockedFn=k end,docked=function(self,E)if self.dockedFn then runFunction(self.dockedFn,\"Construct Docked Error: \",E)end end,undockedFn=function(E)end,onUndocked=function(self,k)self.undockedFn=k end,undocked=function(self,E)if self.undockedFn then runFunction(self.undockedFn,\"Construct Undocked Error: \",E)end end,playerBoardedFn=function(E)end,onPlayerBoarded=function(self,k)self.playerBoardedFn=k end,playerBoarded=function(self,E)if self.playerBoardedFn then runFunction(self.playerBoardedFn,\"Construct Player Boarded Error: \",E)end end,VRStationEnteredFn=function(E)end,onVRStationEntered=function(self,k)self.VRStationEnteredFn=k end,VRStationEntered=function(self,E)if self.VRStationEnteredFn then runFunction(self.VRStationEnteredFn,\"Construct VR Station Entered Error: \",E)end end,constructDockedFn=function(E)end,onConstructDocked=function(self,k)self.constructDockedFn=k end,constructDocked=function(self,E)if self.constructDockedFn then runFunction(self.constructDockedFn,\"Construct Construct Docked Error: \",E)end end,PvPTimerFn=function(F)end,onPvPTimer=function(self,k)self.PvPTimerFn=k end,PvPTimer=function(self,F)if self.PvPTimerFn then runFunction(self.PvPTimerFn,\"Construct PvP Timer Error: \",F)end end}}DU_Framework={__index={system=setmetatable({},o),unit=setmetatable({},x),player=setmetatable({},A),construct=setmetatable({},D)}}\n\nScript = {}\nsetmetatable(Script, DU_Framework)\n\n\n--[[\n You can declare here all the functions you want to call in the diffent events from system, unit, player and construct.\n Major changes compared to the base game concept:\n - System onUpdate and onFlush events are now built with coroutines, in the framework, onUpdate and onFlush are loading a table of functions that will be runned as coroutines. This way you can have multiple functions running at the same time, and you use the yield function to pause the coroutine and let the other coroutines run. Each coroutine will be resume or restarted if dead each time the system onUpdate or onFlush event is called.\n\n]]\n\n--Functions to load as coroutines that will be runned in system > onUpdate (based on FPS)\nlocal system_update = {}\nsystem_update.co1 = function ()\n for i=0, 10 do\n system.print(\"coroutine 1 --- update --- \"..i)\n coroutine.yield() -- pause the coroutine 1, it will wait till the next onUpdate event to be resumed\n end\nend\nsystem_update.co2 = function ()\n for i=0, 10 do\n system.print(\"coroutine 2 --- update --- \"..i)\n coroutine.yield() -- pause the coroutine 2, it will wait till the next onUpdate event to be resumed\n end\nend\n\n--Functions to load as coroutines that will be runned in system > onFlush (60 times / s)\nlocal system_flush = {}\nsystem_flush.co1 = function ()\n for i=0, 10 do\n system.print(\"coroutine 1 --- flush --- \"..i)\n coroutine.yield() -- pause the coroutine 1, it will wait till the next onFlush event to be resumed\n end\nend\nsystem_flush.co2 = function ()\n for i=0, 10 do\n system.print(\"coroutine 2 --- flush --- \"..i)\n coroutine.yield() -- pause the coroutine 2, it will wait till the next onFlush event to be resumed\n end\nend\n\n--Function to run on actions\nlocal system_action_start = {}\nsystem_action_start[Script.system.ACTIONS.BRAKE] = function()\n system.print(\"I'm braking\");\nend\nlocal system_action_stop = {}\nsystem_action_stop[Script.system.ACTIONS.BRAKE] = function()\n system.print(\"I stopped braking\");\nend\nlocal system_action_loop = {}\nsystem_action_loop[Script.system.ACTIONS.BRAKE] = function()\n system.print(\"I'm still braking\");\nend\n\n--Function to run when input text to the lua chat\nlocal system_inputText = function (text)\n system.print(\"Input: \" .. text)\nend\n\n--Function to run when the program is stopping\nlocal unit_onstop = function ()\n system.print(\"Program is stopping\")\nend\n\n--Function to run when the player change parent\nlocal player_onparentchanged = function (oldParent, newParent)\n system.print(\"Player changed parent from ID \"..oldParent..\" to ID \"..newParent)\nend\n\n--Function to run when the construct is docked or undocked\nlocal construct_onDocked = function(id)\n system.print(\"Construct docked on ID \"..id)\nend\nlocal construct_onUndocked = function(id)\n system.print(\"Construct undocked from ID \"..id)\nend\n\n--Function to run when a player board the construct\nlocal construct_onplayerboarded = function(id)\n system.print(\"Player with ID \" .. id .. \" boarded construct\")\nend\n\n--Function to run when a player enter the VR Station\nlocal construct_vrstationentered = function(id)\n system.print(\"Player with ID \" .. id .. \" entered VR Station\")\nend\n\n--Function to run when another construct is docked on this construct\nlocal construct_constructdocked = function(id)\n system.print(\"Construct with ID \" .. id .. \" docked on this construct\")\nend\n\n--Function to run when pvp timer is changing state\nlocal construct_onpvptimer = function(active)\n if active then\n system.print(\"PVP is now active\")\n else\n system.print(\"PVP is now inactive\")\n end\nend\n\n--[[\n Here how to load the functions in the framework\n]]\nScript.system:onUpdate(system_update) --loading coroutines for system > onUpdate\nScript.system:onFlush(system_flush) --loading coroutines for system > onFlush\n\nScript.system:onActionStart(system_action_start) --loading all \"actionStart\" functions\nScript.system:onActionStop(system_action_stop) --loading all \"actionStop\" functions\nScript.system:onActionLoop(system_action_loop) --loading all \"actionLoop\" functions\n\nScript.system:onInputText(system_inputText) --loading the function to trigger when input text in lua chat\n\nScript.unit:setTimer(\"hello\", 1, function()system.print(\"hello\")end) --add a timer displaying \"hello\" every seconds\nScript.unit:setTimer(\"hello5\", 5, function()system.print(\"hello 5\")end) --add a timer displaying \"hello 5\" every 5 seconds\nScript.unit:onStop(unit_onstop) --loading the function to trigger when the program is stopping\n\nScript.player:onParentChanged(player_onparentchanged) --loading the function to trigger when the player change parent\n\nScript.construct:onDocked(construct_onDocked) --loading the function to trigger when the construct is docked\nScript.construct:onUndocked(construct_onUndocked) --loading the function to trigger when the construct is undocked\nScript.construct:onPlayerBoarded(construct_onplayerboarded) --loading the function to trigger when a player board the construct\nScript.construct:onVRStationEntered(construct_vrstationentered) --loading the function to trigger when a player enter the VR Station\nScript.construct:onConstructDocked(construct_constructdocked) --loading the function to trigger when another construct is docked on this construct\nScript.construct:onPvPTimer(construct_onpvptimer) --loading the function to trigger when pvp timer is changing state","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"0"}],"methods":[],"slots":{"0":{"name":"slot1","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}}}} \ No newline at end of file +{"events":[],"handlers":[{"code":"Script.system:update()","filter":{"args":[],"signature":"onUpdate()","slotKey":"-4"},"key":"10"},{"code":"Script.system:inputText(text)","filter":{"args":[{"variable":"*"}],"signature":"onInputText(text)","slotKey":"-4"},"key":"11"},{"code":"Script.system:flush()","filter":{"args":[],"signature":"onFlush()","slotKey":"-4"},"key":"12"},{"code":"Script.system:actionStop(action)","filter":{"args":[{"variable":"*"}],"signature":"onActionStop(action)","slotKey":"-4"},"key":"13"},{"code":"Script.system:actionStart(action)","filter":{"args":[{"variable":"*"}],"signature":"onActionStart(action)","slotKey":"-4"},"key":"14"},{"code":"Script.system:actionLoop(action)","filter":{"args":[{"variable":"*"}],"signature":"onActionLoop(action)","slotKey":"-4"},"key":"15"},{"code":"Script.player:parentChanged(oldId, newId)","filter":{"args":[{"variable":"*"},{"variable":"*"}],"signature":"onParentChanged(oldId,newId)","slotKey":"-3"},"key":"3"},{"code":"Script.construct:VRStationEntered(id)","filter":{"args":[{"variable":"*"}],"signature":"onVRStationEntered(id)","slotKey":"-2"},"key":"6"},{"code":"Script.construct:undocked(id)","filter":{"args":[{"variable":"*"}],"signature":"onUndocked(id)","slotKey":"-2"},"key":"8"},{"code":"Script.construct:PvPTimer(active)","filter":{"args":[{"variable":"*"}],"signature":"onPvPTimer(active)","slotKey":"-2"},"key":"4"},{"code":"Script.construct:playerBoarded(id)","filter":{"args":[{"variable":"*"}],"signature":"onPlayerBoarded(id)","slotKey":"-2"},"key":"7"},{"code":"","filter":{"args":[{"variable":"*"}],"signature":"onDocked(id)","slotKey":"-2"},"key":"9"},{"code":"Script.construct:constructDocked(id)","filter":{"args":[{"variable":"*"}],"signature":"onConstructDocked(id)","slotKey":"-2"},"key":"5"},{"code":"Script.unit:timer(tag)","filter":{"args":[{"variable":"*"}],"signature":"onTimer(tag)","slotKey":"-1"},"key":"1"},{"code":"Script.unit:stop()","filter":{"args":[],"signature":"onStop()","slotKey":"-1"},"key":"2"},{"code":"--[[\r\n DU-LUA-Framework by Jericho\r\n Permit to code easier by grouping most of the code in a single event Unit > Start\r\n Unminified Source available here: https://github.com/Jericho1060/du-lua-framework\r\n]]--\r\nlocal a=system.print;local b=error;local c=pcall;local d=assert;local e=coroutine;local f=e.create;local g=e.status;local h=e.resume;local i=\"dead\"local j=\"suspended\"function runFunction(k,l,...)local m,n=c(k,...)if not m then b(l..n)end end;local o={__index={cos={update={},flush={}},fns={update={},flush={},action={start={},stop={},loop={}},inputText=nil,start=nil,stop=nil},ACTIONS={FORWARD=\"forward\",BACKWARD=\"backward\",YAW_LEFT=\"yawleft\",YAW_RIGHT=\"yawright\",STRAFE_LEFT=\"strafeleft\",STRAFE_RIGHT=\"straferight\",LEFT=\"left\",RIGHT=\"right\",UP=\"up\",DOWN=\"down\",GROUND_ALTITUDE_UP=\"groundaltitudeup\",GROUND_ALTITUDE_DOWN=\"groundaltitudedown\",LEFT_ALT=\"lalt\",LEFT_SHIFT=\"lshift\",GEAR=\"gear\",LIGHT=\"light\",BRAKE=\"brake\",OPTION_1=\"option1\",OPTION_2=\"option2\",OPTION_3=\"option3\",OPTION_4=\"option4\",OPTION_5=\"option5\",OPTION_6=\"option6\",OPTION_7=\"option7\",OPTION_8=\"option8\",OPTION_9=\"option9\",LEFT_MOUSE=\"leftmouse\",STOP_ENGINES=\"stopengines\",SPEED_UP=\"speedup\",SPEED_DOWN=\"speeddown\",ANTIGRAVITY=\"antigravity\",BOOSTER=\"booster\"},main={update=f(function()end),flush=f(function()end)},update=function(self)local p=g(self.main.update)if p==i then self.main.update=f(function()self:runUpdate()end)elseif p==j then d(h(self.main.update))end end,flush=function(self)local p=g(self.main.flush)if p==i then self.main.flush=f(function()self:runFlush()end)elseif p==j then d(h(self.main.flush))end end,action=function(self,q,r)if self.fns.action[q][r]then runFunction(self.fns.action[q][r],\"System Action \"..q..\" Error: \")end end,actionStart=function(self,r)self:action('start',r)end,actionStop=function(self,r)self:action('stop',r)end,actionLoop=function(self,r)self:action('loop',r)end,inputText=function(self,s)if self.fns.inputText then runFunction(self.fns.inputText,\"System Input Text Error: \",s)end end,runUpdate=function(self)for t,u in pairs(self.cos.update)do local m=g(u)if m==i then self.cos.update[t]=f(self.fns.update[t])elseif m==j then d(h(u))end end end,runFlush=function(self)for t,u in pairs(self.cos.flush)do local m=g(u)if m==i then self.cos.flush[t]=f(self.fns.flush[t])elseif m==j then d(h(u))end end end,onUpdate=function(self,v)for t,w in pairs(v)do self.fns.update[t]=w;self.cos.update[t]=f(w)end end,onFlush=function(self,v)for t,w in pairs(v)do self.fns.flush[t]=w;self.cos.flush[t]=f(w)end end,onAction=function(self,q,v)for t,w in pairs(v)do self.fns.action[q][t]=w end end,onActionStart=function(self,v)self:onAction(\"start\",v)end,onActionStop=function(self,v)self:onAction(\"stop\",v)end,onActionLoop=function(self,v)self:onAction(\"loop\",v)end,onInputText=function(self,k)self.fns.inputText=k end}}local x={__index={timers={},stopFn=function()end,timer=function(self,y)if self.timers[y]then runFunction(self.timers[y],\"Unit Timer \"..y..\" Error: \")end end,setTimer=function(self,y,z,k)self.timers[y]=k;unit.setTimer(y,z)end,stopTimer=function(self,y)unit.stopTimer(y)self.timers[y]=nil end,onStop=function(self,k)self.stopFn=k end,stop=function(self)if self.stopFn then runFunction(self.stopFn,\"Unit Stop Error: \")end end}}local A={__index={parentChangedFn=function(B,C)end,onParentChange=function(self,k)self.parentChangedFn=k end,parentChanged=function(self,B,C)if self.parentChangedFn then runFunction(self.parentChangedFn,\"Player Parent Changed Error: \",B,C)end end}}local D={__index={dockedFn=function(E)end,onDocked=function(self,k)self.dockedFn=k end,docked=function(self,E)if self.dockedFn then runFunction(self.dockedFn,\"Construct Docked Error: \",E)end end,undockedFn=function(E)end,onUndocked=function(self,k)self.undockedFn=k end,undocked=function(self,E)if self.undockedFn then runFunction(self.undockedFn,\"Construct Undocked Error: \",E)end end,playerBoardedFn=function(E)end,onPlayerBoarded=function(self,k)self.playerBoardedFn=k end,playerBoarded=function(self,E)if self.playerBoardedFn then runFunction(self.playerBoardedFn,\"Construct Player Boarded Error: \",E)end end,VRStationEnteredFn=function(E)end,onVRStationEntered=function(self,k)self.VRStationEnteredFn=k end,VRStationEntered=function(self,E)if self.VRStationEnteredFn then runFunction(self.VRStationEnteredFn,\"Construct VR Station Entered Error: \",E)end end,constructDockedFn=function(E)end,onConstructDocked=function(self,k)self.constructDockedFn=k end,constructDocked=function(self,E)if self.constructDockedFn then runFunction(self.constructDockedFn,\"Construct Construct Docked Error: \",E)end end,PvPTimerFn=function(F)end,onPvPTimer=function(self,k)self.PvPTimerFn=k end,PvPTimer=function(self,F)if self.PvPTimerFn then runFunction(self.PvPTimerFn,\"Construct PvP Timer Error: \",F)end end}}DU_Framework={__index={system=setmetatable({},o),unit=setmetatable({},x),player=setmetatable({},A),construct=setmetatable({},D)}}\r\n\r\nScript = {}\r\nsetmetatable(Script, DU_Framework)\r\n\r\n\r\n--[[\r\n You can declare here all the functions you want to call in the diffent events from system, unit, player and construct.\r\n Major changes compared to the base game concept:\r\n - System onUpdate and onFlush events are now built with coroutines, in the framework, onUpdate and onFlush are loading a table of functions that will be runned as coroutines. This way you can have multiple functions running at the same time, and you use the yield function to pause the coroutine and let the other coroutines run. Each coroutine will be resume or restarted if dead each time the system onUpdate or onFlush event is called.\r\n\r\n]]\r\n\r\n-- System > onUpdate and onFlush\r\n\r\n--Functions to load as coroutines that will be runned in system > onUpdate (based on FPS)\r\nlocal system_update = {}\r\nsystem_update.co1 = function ()\r\n for i=0, 10 do\r\n system.print(\"coroutine 1 --- update --- \"..i)\r\n coroutine.yield() -- pause the coroutine 1, it will wait till the next onUpdate event to be resumed\r\n end\r\nend\r\nsystem_update.co2 = function ()\r\n for i=0, 10 do\r\n system.print(\"coroutine 2 --- update --- \"..i)\r\n coroutine.yield() -- pause the coroutine 2, it will wait till the next onUpdate event to be resumed\r\n end\r\nend\r\n\r\nScript.system:onUpdate(system_update) --loading coroutines for system > onUpdate\r\n\r\n--Functions to load as coroutines that will be runned in system > onFlush (60 times / s)\r\nlocal system_flush = {}\r\nsystem_flush.co1 = function ()\r\n for i=0, 10 do\r\n system.print(\"coroutine 1 --- flush --- \"..i)\r\n coroutine.yield() -- pause the coroutine 1, it will wait till the next onFlush event to be resumed\r\n end\r\nend\r\nsystem_flush.co2 = function ()\r\n for i=0, 10 do\r\n system.print(\"coroutine 2 --- flush --- \"..i)\r\n coroutine.yield() -- pause the coroutine 2, it will wait till the next onFlush event to be resumed\r\n end\r\nend\r\n\r\nScript.system:onFlush(system_flush) --loading coroutines for system > onFlush\r\n\r\n\r\n\r\n-- System > onActionStart, onActionStop and onActionLoop\r\n\r\n--Function to run on actions\r\nlocal system_action_start = {}\r\nsystem_action_start[Script.system.ACTIONS.BRAKE] = function()\r\n system.print(\"I'm braking\");\r\nend\r\nScript.system:onActionStart(system_action_start) --loading all \"actionStart\" functions\r\n\r\nlocal system_action_stop = {}\r\nsystem_action_stop[Script.system.ACTIONS.BRAKE] = function()\r\n system.print(\"I stopped braking\");\r\nend\r\nScript.system:onActionStop(system_action_stop) --loading all \"actionStop\" functions\r\n\r\nlocal system_action_loop = {}\r\nsystem_action_loop[Script.system.ACTIONS.BRAKE] = function()\r\n system.print(\"I'm still braking\");\r\nend\r\nScript.system:onActionLoop(system_action_loop) --loading all \"actionLoop\" functions\r\n\r\n\r\n-- System > onInputText\r\n\r\n--Function to run when input text to the lua chat\r\nScript.system:onInputText(function (text)\r\n system.print(\"Input: \" .. text)\r\nend)\r\n\r\n\r\n-- unit > onStop\r\n\r\n--Function to run when the program is stopping\r\nScript.unit:onStop(function()\r\n system.print(\"Program is stopping\")\r\nend)\r\n\r\n\r\n\r\n-- unit > Timers\r\n\r\n--[[\r\n Here how to add a timer\r\n @param name: the name of the timer, used to remove it with Script.unit:stopTimer(name)\r\n @param delay: the delay between each call of the function in seconds\r\n @param func: the function to call\r\n]]\r\nScript.unit:setTimer(\"hello\", 1, function()system.print(\"hello\")end) --add a timer displaying \"hello\" every seconds\r\nScript.unit:setTimer(\"hello5\", 5, function()system.print(\"hello 5\")end) --add a timer displaying \"hello 5\" every 5 seconds\r\n\r\nScript.unit:stopTimer(\"hello\") --stop the timer \"hello\"\r\n\r\n\r\n\r\n-- player > onParentChanged\r\n\r\n--Function to run when the player change parent\r\nScript.player:onParentChanged(function (oldParent, newParent)\r\n system.print(\"Player changed parent from ID \"..oldParent..\" to ID \"..newParent)\r\nend)\r\n\r\n\r\n\r\n-- construct events\r\n\r\n--Function to run when the construct is docked or undocked\r\nScript.construct:onDocked(function(id)\r\n system.print(\"Construct docked on ID \"..id)\r\nend)\r\nScript.construct:onUndocked(function(id)\r\n system.print(\"Construct undocked from ID \"..id)\r\nend)\r\n\r\n--Function to run when a player board the construct\r\nScript.construct:onPlayerBoarded(function(id)\r\n system.print(\"Player with ID \" .. id .. \" boarded construct\")\r\nend)\r\n\r\n--Function to run when a player enter the VR Station\r\nScript.construct:onVRStationEntered(function(id)\r\n system.print(\"Player with ID \" .. id .. \" entered VR Station\")\r\nend)\r\n\r\n--Function to run when another construct is docked on this construct\r\nScript.construct:onConstructDocked(function(id)\r\n system.print(\"Construct with ID \" .. id .. \" docked on this construct\")\r\nend)\r\n\r\n--Function to run when pvp timer is changing state\r\nScript.construct:onPvPTimer(function(active)\r\n if active then\r\n system.print(\"PVP is now active\")\r\n else\r\n system.print(\"PVP is now inactive\")\r\n end\r\nend)","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"0"}],"methods":[],"slots":{"0":{"name":"slot1","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}}}} \ No newline at end of file diff --git a/source/unit/onStart.lua b/source/unit/onStart.lua index e6783ac..bc4df00 100644 --- a/source/unit/onStart.lua +++ b/source/unit/onStart.lua @@ -284,6 +284,8 @@ setmetatable(Script, DU_Framework) ]] +-- System > onUpdate and onFlush + --Functions to load as coroutines that will be runned in system > onUpdate (based on FPS) local system_update = {} system_update.co1 = function () @@ -299,6 +301,8 @@ system_update.co2 = function () end end +Script.system:onUpdate(system_update) --loading coroutines for system > onUpdate + --Functions to load as coroutines that will be runned in system > onFlush (60 times / s) local system_flush = {} system_flush.co1 = function () @@ -314,95 +318,103 @@ system_flush.co2 = function () end end +Script.system:onFlush(system_flush) --loading coroutines for system > onFlush + + + +-- System > onActionStart, onActionStop and onActionLoop + --Function to run on actions local system_action_start = {} system_action_start[Script.system.ACTIONS.BRAKE] = function() system.print("I'm braking"); end +Script.system:onActionStart(system_action_start) --loading all "actionStart" functions + local system_action_stop = {} system_action_stop[Script.system.ACTIONS.BRAKE] = function() system.print("I stopped braking"); end +Script.system:onActionStop(system_action_stop) --loading all "actionStop" functions + local system_action_loop = {} system_action_loop[Script.system.ACTIONS.BRAKE] = function() system.print("I'm still braking"); end +Script.system:onActionLoop(system_action_loop) --loading all "actionLoop" functions + + +-- System > onInputText --Function to run when input text to the lua chat -local system_inputText = function (text) +Script.system:onInputText(function (text) system.print("Input: " .. text) -end +end) + + +-- unit > onStop --Function to run when the program is stopping -local unit_onstop = function () +Script.unit:onStop(function() system.print("Program is stopping") -end +end) + + + +-- unit > Timers + +--[[ + Here how to add a timer + @param name: the name of the timer, used to remove it with Script.unit:stopTimer(name) + @param delay: the delay between each call of the function in seconds + @param func: the function to call +]] +Script.unit:setTimer("hello", 1, function()system.print("hello")end) --add a timer displaying "hello" every seconds +Script.unit:setTimer("hello5", 5, function()system.print("hello 5")end) --add a timer displaying "hello 5" every 5 seconds + +Script.unit:stopTimer("hello") --stop the timer "hello" + + + +-- player > onParentChanged --Function to run when the player change parent -local player_onparentchanged = function (oldParent, newParent) +Script.player:onParentChanged(function (oldParent, newParent) system.print("Player changed parent from ID "..oldParent.." to ID "..newParent) -end +end) + + + +-- construct events --Function to run when the construct is docked or undocked -local construct_onDocked = function(id) +Script.construct:onDocked(function(id) system.print("Construct docked on ID "..id) -end -local construct_onUndocked = function(id) +end) +Script.construct:onUndocked(function(id) system.print("Construct undocked from ID "..id) -end +end) --Function to run when a player board the construct -local construct_onplayerboarded = function(id) +Script.construct:onPlayerBoarded(function(id) system.print("Player with ID " .. id .. " boarded construct") -end +end) --Function to run when a player enter the VR Station -local construct_vrstationentered = function(id) +Script.construct:onVRStationEntered(function(id) system.print("Player with ID " .. id .. " entered VR Station") -end +end) --Function to run when another construct is docked on this construct -local construct_constructdocked = function(id) +Script.construct:onConstructDocked(function(id) system.print("Construct with ID " .. id .. " docked on this construct") -end +end) --Function to run when pvp timer is changing state -local construct_onpvptimer = function(active) +Script.construct:onPvPTimer(function(active) if active then system.print("PVP is now active") else system.print("PVP is now inactive") end -end - ---[[ - Here how to load the functions in the framework -]] -Script.system:onUpdate(system_update) --loading coroutines for system > onUpdate -Script.system:onFlush(system_flush) --loading coroutines for system > onFlush - -Script.system:onActionStart(system_action_start) --loading all "actionStart" functions -Script.system:onActionStop(system_action_stop) --loading all "actionStop" functions -Script.system:onActionLoop(system_action_loop) --loading all "actionLoop" functions - -Script.system:onInputText(system_inputText) --loading the function to trigger when input text in lua chat - ---[[ - Here how to add a timer - @param name: the name of the timer, used to remove it with Script.unit:stopTimer(name) - @param delay: the delay between each call of the function in seconds - @param func: the function to call -]] -Script.unit:setTimer("hello", 1, function()system.print("hello")end) --add a timer displaying "hello" every seconds -Script.unit:setTimer("hello5", 5, function()system.print("hello 5")end) --add a timer displaying "hello 5" every 5 seconds - -Script.unit:onStop(unit_onstop) --loading the function to trigger when the program is stopping - -Script.player:onParentChanged(player_onparentchanged) --loading the function to trigger when the player change parent - -Script.construct:onDocked(construct_onDocked) --loading the function to trigger when the construct is docked -Script.construct:onUndocked(construct_onUndocked) --loading the function to trigger when the construct is undocked -Script.construct:onPlayerBoarded(construct_onplayerboarded) --loading the function to trigger when a player board the construct -Script.construct:onVRStationEntered(construct_vrstationentered) --loading the function to trigger when a player enter the VR Station -Script.construct:onConstructDocked(construct_constructdocked) --loading the function to trigger when another construct is docked on this construct -Script.construct:onPvPTimer(construct_onpvptimer) --loading the function to trigger when pvp timer is changing state \ No newline at end of file +end) \ No newline at end of file -- 2.51.2