[READ-ONLY] Mirror of https://github.com/thoda-dev/du-lua-framework. A framework to simplify coding in Dual Universe. Permit to create most of the code in Unit > onStart
dual-universe lua
0

Configure Feed

Select the types of activity you want to include in your feed.

v1.0.0

Thomas (Feb 4, 2023, 8:38 PM +0100) 824db275 d7cee222

+609 -1
+191 -1
README.md
··· 1 1 # du-lua-framework 2 - A framework to simplidy coding in Dual Universe. Permit to add create most of the code in Unit > onStart 2 + A framework to simplidy coding in Dual Universe. 3 + 4 + Permit to add create most of the code in Unit > onStart 5 + 6 + # Guilded Server (better than Discord) 7 + 8 + You can join me on Guilded for help or suggestions or requests by following that link : https://guilded.jericho.dev 9 + 10 + 11 + # Support or donation 12 + 13 + if you like it, [<img src="https://github.com/Jericho1060/DU-Industry-HUD/blob/main/ressources/images/ko-fi.png?raw=true" width="150">](https://ko-fi.com/jericho1060) or you can sponsor me directly on [Github](https://github.com/sponsors/Jericho1060) 14 + 15 + # Documentation and examples 16 + 17 + ## How to use it 18 + 19 + Copy the content of the file [`config.json`](https://raw.githubusercontent.com/Jericho1060/du-lua-framework/main/config.json) and paste it on any control unit by right clicking on it and selecting "Advanced" and then "Paste Lua Configuration from clipboard". 20 + 21 + *Warning 1: It can happens that the paste is not working in DU, it'f it's your case, please, restart the game and try again.* 22 + 23 + *Warning 2: Paste may not work in GeForce Now, please install it from a real PC or ask someone else to install it for you.* 24 + 25 + ## Major Changes compared to the game logic 26 + 27 + System onUpdate and onFlush events are now builded 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. 28 + 29 + ## Code Examples 30 + 31 + ```lua 32 + --Functions to load as coroutines that will be runned in system > onUpdate (based on FPS) 33 + local system_update = {} 34 + system_update.co1 = function () 35 + for i=0, 10 do 36 + system.print("coroutine 1 --- update --- "..i) 37 + coroutine.yield() -- pause the coroutine 1, it will wait till the next onUpdate event to be resumed 38 + end 39 + end 40 + system_update.co2 = function () 41 + for i=0, 10 do 42 + system.print("coroutine 2 --- update --- "..i) 43 + coroutine.yield() -- pause the coroutine 2, it will wait till the next onUpdate event to be resumed 44 + end 45 + end 46 + 47 + --Functions to load as coroutines that will be runned in system > onFlush (60 times / s) 48 + local system_flush = {} 49 + system_flush.co1 = function () 50 + for i=0, 10 do 51 + system.print("coroutine 1 --- flush --- "..i) 52 + coroutine.yield() -- pause the coroutine 1, it will wait till the next onFlush event to be resumed 53 + end 54 + end 55 + system_flush.co2 = function () 56 + for i=0, 10 do 57 + system.print("coroutine 2 --- flush --- "..i) 58 + coroutine.yield() -- pause the coroutine 2, it will wait till the next onFlush event to be resumed 59 + end 60 + end 61 + 62 + --Function to run on actions 63 + local system_action_start = {} 64 + system_action_start[Script.system.ACTIONS.BRAKE] = function() 65 + system.print("I'm braking"); 66 + end 67 + local system_action_stop = {} 68 + system_action_stop[Script.system.ACTIONS.BRAKE] = function() 69 + system.print("I stopped braking"); 70 + end 71 + local system_action_loop = {} 72 + system_action_loop[Script.system.ACTIONS.BRAKE] = function() 73 + system.print("I'm still braking"); 74 + end 75 + 76 + --Function to run when input text to the lua chat 77 + local system_inputText = function (text) 78 + system.print("Input: " .. text) 79 + end 80 + 81 + --Function to run when the program is stopping 82 + local unit_onstop = function () 83 + system.print("Program is stopping") 84 + end 85 + 86 + --Function to run when the player change parent 87 + local player_onparentchanged = function (oldParent, newParent) 88 + system.print("Player changed parent from ID "..oldParent.." to ID "..newParent) 89 + end 90 + 91 + --Function to run when the construct is docked or undocked 92 + local construct_onDocked = function(id) 93 + system.print("Construct docked on ID "..id) 94 + end 95 + local construct_onUndocked = function(id) 96 + system.print("Construct undocked from ID "..id) 97 + end 98 + 99 + --Function to run when a player board the construct 100 + local construct_onplayerboarded = function(id) 101 + system.print("Player with ID " .. id .. " boarded construct") 102 + end 103 + 104 + --Function to run when a player enter the VR Station 105 + local construct_vrstationentered = function(id) 106 + system.print("Player with ID " .. id .. " entered VR Station") 107 + end 108 + 109 + --Function to run when another construct is docked on this construct 110 + local construct_constructdocked = function(id) 111 + system.print("Construct with ID " .. id .. " docked on this construct") 112 + end 113 + 114 + --Function to run when pvp timer is changing state 115 + local construct_onpvptimer = function(active) 116 + if active then 117 + system.print("PVP is now active") 118 + else 119 + system.print("PVP is now inactive") 120 + end 121 + end 122 + 123 + --[[ 124 + Here how to load the functions in the framework 125 + ]] 126 + Script.system:onUpdate(system_update) --loading coroutines for system > onUpdate 127 + Script.system:onFlush(system_flush) --loading coroutines for system > onFlush 128 + 129 + Script.system:onActionStart(system_action_start) --loading all "actionStart" functions 130 + Script.system:onActionStop(system_action_stop) --loading all "actionStop" functions 131 + Script.system:onActionLoop(system_action_loop) --loading all "actionLoop" functions 132 + 133 + Script.system:onInputText(system_inputText) --loading the function to trigger when input text in lua chat 134 + 135 + --[[ 136 + Here how to add a timer 137 + @param name: the name of the timer, used to remove it with Script.unit:stopTimer(name) 138 + @param delay: the delay between each call of the function in seconds 139 + @param func: the function to call 140 + ]] 141 + Script.unit:setTimer("hello", 1, function()system.print("hello")end) --add a timer displaying "hello" every seconds 142 + Script.unit:setTimer("hello5", 5, function()system.print("hello 5")end) --add a timer displaying "hello 5" every 5 seconds 143 + Script.unit:onStop(unit_onstop) --loading the function to trigger when the program is stopping 144 + 145 + Script.player:onParentChanged(player_onparentchanged) --loading the function to trigger when the player change parent 146 + 147 + Script.construct:onDocked(construct_onDocked) --loading the function to trigger when the construct is docked 148 + Script.construct:onUndocked(construct_onUndocked) --loading the function to trigger when the construct is undocked 149 + Script.construct:onPlayerBoarded(construct_onplayerboarded) --loading the function to trigger when a player board the construct 150 + Script.construct:onVRStationEntered(construct_vrstationentered) --loading the function to trigger when a player enter the VR Station 151 + Script.construct:onConstructDocked(construct_constructdocked) --loading the function to trigger when another construct is docked on this construct 152 + Script.construct:onPvPTimer(construct_onpvptimer) --loading the function to trigger when pvp timer is changing state 153 + ``` 154 + 155 + ## System Actions: 156 + 157 + to use with `Script.system.ACTIONS[ActionKey]` 158 + 159 + ```lua 160 + FORWARD = "forward", 161 + BACKWARD = "backward", 162 + YAW_LEFT = "yawleft", 163 + YAW_RIGHT = "yawright", 164 + STRAFE_LEFT = "strafeleft", 165 + STRAFE_RIGHT = "straferight", 166 + LEFT = "left", 167 + RIGHT = "right", 168 + UP = "up", 169 + DOWN = "down", 170 + GROUND_ALTITUDE_UP = "groundaltitudeup", 171 + GROUND_ALTITUDE_DOWN = "groundaltitudedown", 172 + LEFT_ALT = "lalt", 173 + LEFT_SHIFT = "lshift", 174 + GEAR = "gear", 175 + LIGHT = "light", 176 + BRAKE = "brake", 177 + OPTION_1 = "option1", 178 + OPTION_2 = "option2", 179 + OPTION_3 = "option3", 180 + OPTION_4 = "option4", 181 + OPTION_5 = "option5", 182 + OPTION_6 = "option6", 183 + OPTION_7 = "option7", 184 + OPTION_8 = "option8", 185 + OPTION_9 = "option9", 186 + LEFT_MOUSE = "leftmouse", 187 + STOP_ENGINES = "stopengines", 188 + SPEED_UP = "speedup", 189 + SPEED_DOWN = "speeddown", 190 + ANTIGRAVITY = "antigravity", 191 + BOOSTER = "booster" 192 + ```
+1
config.json
··· 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 builded 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":[]}}}}
+1
source/player/onParentChanged.lua
··· 1 + Script.player:parentChanged(oldId, newId)
+1
source/system/onActionLoop.lua
··· 1 + Script.system:actionLoop(action)
+1
source/system/onActionStart.lua
··· 1 + Script.system:actionStart(action)
+1
source/system/onActionStop.lua
··· 1 + Script.system:actionStop(action)
+1
source/system/onFlush.lua
··· 1 + Script.system:flush()
+1
source/system/onInputText.lua
··· 1 + Script.system:inputText(text)
+1
source/system/onUpdate.lua
··· 1 + Script.system:update()
+408
source/unit/onStart.lua
··· 1 + --[[ 2 + DU-Nested-Coroutines by Jericho 3 + Permit to easier avoid CPU Load Errors by using nested coroutines and adapt cycles on the FPS 4 + Source unminified available here: https://github.com/Jericho1060/du-nested-coroutines 5 + ]]-- 6 + 7 + local print = system.print 8 + local n_error = error 9 + local n_pcall = pcall 10 + local n_assert = assert 11 + local cor = coroutine 12 + local co_create = cor.create 13 + local co_status = cor.status 14 + local co_resume = cor.resume 15 + local co_status_dead = "dead" 16 + local co_status_suspended = "suspended" 17 + 18 + function runFunction(fn, errorPrefix, ...) 19 + local s, err = n_pcall(fn, ...) 20 + if not s then 21 + n_error(errorPrefix .. err) 22 + end 23 + end 24 + 25 + local DU_System = { 26 + __index = { 27 + cos = { 28 + update = {}, 29 + flush = {} 30 + }, 31 + fns = { 32 + update = {}, 33 + flush = {}, 34 + action = { 35 + start = {}, 36 + stop = {}, 37 + loop = {} 38 + }, 39 + inputText = nil, 40 + start = nil, 41 + stop = nil 42 + }, 43 + ACTIONS = { 44 + FORWARD = "forward", 45 + BACKWARD = "backward", 46 + YAW_LEFT = "yawleft", 47 + YAW_RIGHT = "yawright", 48 + STRAFE_LEFT = "strafeleft", 49 + STRAFE_RIGHT = "straferight", 50 + LEFT = "left", 51 + RIGHT = "right", 52 + UP = "up", 53 + DOWN = "down", 54 + GROUND_ALTITUDE_UP = "groundaltitudeup", 55 + GROUND_ALTITUDE_DOWN = "groundaltitudedown", 56 + LEFT_ALT = "lalt", 57 + LEFT_SHIFT = "lshift", 58 + GEAR = "gear", 59 + LIGHT = "light", 60 + BRAKE = "brake", 61 + OPTION_1 = "option1", 62 + OPTION_2 = "option2", 63 + OPTION_3 = "option3", 64 + OPTION_4 = "option4", 65 + OPTION_5 = "option5", 66 + OPTION_6 = "option6", 67 + OPTION_7 = "option7", 68 + OPTION_8 = "option8", 69 + OPTION_9 = "option9", 70 + LEFT_MOUSE = "leftmouse", 71 + STOP_ENGINES = "stopengines", 72 + SPEED_UP = "speedup", 73 + SPEED_DOWN = "speeddown", 74 + ANTIGRAVITY = "antigravity", 75 + BOOSTER = "booster" 76 + }, 77 + main = { 78 + update = co_create(function() end), 79 + flush = co_create(function() end), 80 + }, 81 + update = function(self) 82 + local status = co_status(self.main.update) 83 + if status == co_status_dead then 84 + self.main.update = co_create(function() self:runUpdate() end) 85 + elseif status == co_status_suspended then 86 + n_assert(co_resume(self.main.update)) 87 + end 88 + end, 89 + flush = function(self) 90 + local status = co_status(self.main.flush) 91 + if status == co_status_dead then 92 + self.main.flush = co_create(function() self:runFlush() end) 93 + elseif status == co_status_suspended then 94 + n_assert(co_resume(self.main.flush)) 95 + end 96 + end, 97 + action = function(self, event, action) 98 + if self.fns.action[event][action] then 99 + runFunction(self.fns.action[event][action], "System Action " .. event .. " Error: ") 100 + end 101 + end, 102 + actionStart = function(self, action) 103 + self:action('start', action) 104 + end, 105 + actionStop = function(self, action) 106 + self:action('stop', action) 107 + end, 108 + actionLoop = function(self, action) 109 + self:action('loop', action) 110 + end, 111 + inputText = function(self, text) 112 + if self.fns.inputText then 113 + runFunction(self.fns.inputText, "System Input Text Error: ", text) 114 + end 115 + end, 116 + runUpdate = function(self) 117 + for k,co in pairs(self.cos.update) do 118 + local s = co_status(co) 119 + if s == co_status_dead then 120 + self.cos.update[k] = co_create(self.fns.update[k]) 121 + elseif s == co_status_suspended then 122 + n_assert(co_resume(co)) 123 + end 124 + end 125 + end, 126 + runFlush = function(self) 127 + for k,co in pairs(self.cos.flush) do 128 + local s = co_status(co) 129 + if s == co_status_dead then 130 + self.cos.flush[k] = co_create(self.fns.flush[k]) 131 + elseif s == co_status_suspended then 132 + n_assert(co_resume(co)) 133 + end 134 + end 135 + end, 136 + onUpdate = function(self, fns) 137 + for k,f in pairs(fns) do 138 + self.fns.update[k] = f 139 + self.cos.update[k] = co_create(f) 140 + end 141 + end, 142 + onFlush = function(self, fns) 143 + for k,f in pairs(fns) do 144 + self.fns.flush[k] = f 145 + self.cos.flush[k] = co_create(f) 146 + end 147 + end, 148 + onAction = function(self, event, fns) 149 + for k,f in pairs(fns) do 150 + self.fns.action[event][k] = f 151 + end 152 + end, 153 + onActionStart = function(self, fns) 154 + self:onAction("start", fns) 155 + end, 156 + onActionStop = function(self, fns) 157 + self:onAction("stop", fns) 158 + end, 159 + onActionLoop = function(self, fns) 160 + self:onAction("loop", fns) 161 + end, 162 + onInputText = function(self, fn) 163 + self.fns.inputText = fn 164 + end 165 + } 166 + } 167 + 168 + local DU_Unit = { 169 + __index = { 170 + timers = {}, 171 + stopFn = function() end, 172 + timer = function (self, tag) 173 + if self.timers[tag] then 174 + runFunction(self.timers[tag], "Unit Timer " .. tag .. " Error: ") 175 + end 176 + end, 177 + setTimer = function(self, tag, seconds, fn) 178 + self.timers[tag] = fn 179 + unit.setTimer(tag, seconds) 180 + end, 181 + stopTimer = function(self, tag) 182 + unit.stopTimer(tag) 183 + self.timers[tag]=nil 184 + end, 185 + onStop = function (self, fn) 186 + self.stopFn = fn 187 + end, 188 + stop = function(self) 189 + if self.stopFn then 190 + runFunction(self.stopFn, "Unit Stop Error: ") 191 + end 192 + end 193 + } 194 + } 195 + 196 + local DU_Player = { 197 + __index = { 198 + parentChangedFn = function(oldId, newId) end, 199 + onParentChange = function (self, fn) 200 + self.parentChangedFn = fn 201 + end, 202 + parentChanged = function (self, oldId, newId) 203 + if self.parentChangedFn then 204 + runFunction(self.parentChangedFn, "Player Parent Changed Error: ", oldId, newId) 205 + end 206 + end, 207 + } 208 + } 209 + 210 + local DU_Construct = { 211 + __index = { 212 + dockedFn = function(id) end, 213 + onDocked = function (self, fn) 214 + self.dockedFn = fn 215 + end, 216 + docked = function (self, id) 217 + if self.dockedFn then 218 + runFunction(self.dockedFn, "Construct Docked Error: ", id) 219 + end 220 + end, 221 + undockedFn = function(id) end, 222 + onUndocked = function (self, fn) 223 + self.undockedFn = fn 224 + end, 225 + undocked = function (self, id) 226 + if self.undockedFn then 227 + runFunction(self.undockedFn, "Construct Undocked Error: ", id) 228 + end 229 + end, 230 + playerBoardedFn = function(id) end, 231 + onPlayerBoarded = function (self, fn) 232 + self.playerBoardedFn = fn 233 + end, 234 + playerBoarded = function (self, id) 235 + if self.playerBoardedFn then 236 + runFunction(self.playerBoardedFn, "Construct Player Boarded Error: ", id) 237 + end 238 + end, 239 + VRStationEnteredFn = function(id) end, 240 + onVRStationEntered = function (self, fn) 241 + self.VRStationEnteredFn = fn 242 + end, 243 + VRStationEntered = function (self, id) 244 + if self.VRStationEnteredFn then 245 + runFunction(self.VRStationEnteredFn, "Construct VR Station Entered Error: ", id) 246 + end 247 + end, 248 + constructDockedFn = function(id) end, 249 + onConstructDocked = function (self, fn) 250 + self.constructDockedFn = fn 251 + end, 252 + constructDocked = function (self, id) 253 + if self.constructDockedFn then 254 + runFunction(self.constructDockedFn, "Construct Construct Docked Error: ", id) 255 + end 256 + end, 257 + PvPTimerFn = function(active) end, 258 + onPvPTimer = function (self, fn) 259 + self.PvPTimerFn = fn 260 + end, 261 + PvPTimer = function (self, active) 262 + if self.PvPTimerFn then 263 + runFunction(self.PvPTimerFn, "Construct PvP Timer Error: ", active) 264 + end 265 + end, 266 + } 267 + } 268 + 269 + DU_Framework = { 270 + __index = { 271 + system = setmetatable({}, DU_System), 272 + unit = setmetatable({}, DU_Unit), 273 + player = setmetatable({}, DU_Player), 274 + construct = setmetatable({}, DU_Construct), 275 + } 276 + } 277 + Script = {} 278 + setmetatable(Script, DU_Framework) 279 + 280 + --[[ 281 + You can declare here all the functions you want to call in the diffent events from system, unit, player and construct. 282 + Major changes compared to the base game concept: 283 + - System onUpdate and onFlush events are now builded 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. 284 + 285 + ]] 286 + 287 + --Functions to load as coroutines that will be runned in system > onUpdate (based on FPS) 288 + local system_update = {} 289 + system_update.co1 = function () 290 + for i=0, 10 do 291 + system.print("coroutine 1 --- update --- "..i) 292 + coroutine.yield() -- pause the coroutine 1, it will wait till the next onUpdate event to be resumed 293 + end 294 + end 295 + system_update.co2 = function () 296 + for i=0, 10 do 297 + system.print("coroutine 2 --- update --- "..i) 298 + coroutine.yield() -- pause the coroutine 2, it will wait till the next onUpdate event to be resumed 299 + end 300 + end 301 + 302 + --Functions to load as coroutines that will be runned in system > onFlush (60 times / s) 303 + local system_flush = {} 304 + system_flush.co1 = function () 305 + for i=0, 10 do 306 + system.print("coroutine 1 --- flush --- "..i) 307 + coroutine.yield() -- pause the coroutine 1, it will wait till the next onFlush event to be resumed 308 + end 309 + end 310 + system_flush.co2 = function () 311 + for i=0, 10 do 312 + system.print("coroutine 2 --- flush --- "..i) 313 + coroutine.yield() -- pause the coroutine 2, it will wait till the next onFlush event to be resumed 314 + end 315 + end 316 + 317 + --Function to run on actions 318 + local system_action_start = {} 319 + system_action_start[Script.system.ACTIONS.BRAKE] = function() 320 + system.print("I'm braking"); 321 + end 322 + local system_action_stop = {} 323 + system_action_stop[Script.system.ACTIONS.BRAKE] = function() 324 + system.print("I stopped braking"); 325 + end 326 + local system_action_loop = {} 327 + system_action_loop[Script.system.ACTIONS.BRAKE] = function() 328 + system.print("I'm still braking"); 329 + end 330 + 331 + --Function to run when input text to the lua chat 332 + local system_inputText = function (text) 333 + system.print("Input: " .. text) 334 + end 335 + 336 + --Function to run when the program is stopping 337 + local unit_onstop = function () 338 + system.print("Program is stopping") 339 + end 340 + 341 + --Function to run when the player change parent 342 + local player_onparentchanged = function (oldParent, newParent) 343 + system.print("Player changed parent from ID "..oldParent.." to ID "..newParent) 344 + end 345 + 346 + --Function to run when the construct is docked or undocked 347 + local construct_onDocked = function(id) 348 + system.print("Construct docked on ID "..id) 349 + end 350 + local construct_onUndocked = function(id) 351 + system.print("Construct undocked from ID "..id) 352 + end 353 + 354 + --Function to run when a player board the construct 355 + local construct_onplayerboarded = function(id) 356 + system.print("Player with ID " .. id .. " boarded construct") 357 + end 358 + 359 + --Function to run when a player enter the VR Station 360 + local construct_vrstationentered = function(id) 361 + system.print("Player with ID " .. id .. " entered VR Station") 362 + end 363 + 364 + --Function to run when another construct is docked on this construct 365 + local construct_constructdocked = function(id) 366 + system.print("Construct with ID " .. id .. " docked on this construct") 367 + end 368 + 369 + --Function to run when pvp timer is changing state 370 + local construct_onpvptimer = function(active) 371 + if active then 372 + system.print("PVP is now active") 373 + else 374 + system.print("PVP is now inactive") 375 + end 376 + end 377 + 378 + --[[ 379 + Here how to load the functions in the framework 380 + ]] 381 + Script.system:onUpdate(system_update) --loading coroutines for system > onUpdate 382 + Script.system:onFlush(system_flush) --loading coroutines for system > onFlush 383 + 384 + Script.system:onActionStart(system_action_start) --loading all "actionStart" functions 385 + Script.system:onActionStop(system_action_stop) --loading all "actionStop" functions 386 + Script.system:onActionLoop(system_action_loop) --loading all "actionLoop" functions 387 + 388 + Script.system:onInputText(system_inputText) --loading the function to trigger when input text in lua chat 389 + 390 + --[[ 391 + Here how to add a timer 392 + @param name: the name of the timer, used to remove it with Script.unit:stopTimer(name) 393 + @param delay: the delay between each call of the function in seconds 394 + @param func: the function to call 395 + ]] 396 + Script.unit:setTimer("hello", 1, function()system.print("hello")end) --add a timer displaying "hello" every seconds 397 + Script.unit:setTimer("hello5", 5, function()system.print("hello 5")end) --add a timer displaying "hello 5" every 5 seconds 398 + 399 + Script.unit:onStop(unit_onstop) --loading the function to trigger when the program is stopping 400 + 401 + Script.player:onParentChanged(player_onparentchanged) --loading the function to trigger when the player change parent 402 + 403 + Script.construct:onDocked(construct_onDocked) --loading the function to trigger when the construct is docked 404 + Script.construct:onUndocked(construct_onUndocked) --loading the function to trigger when the construct is undocked 405 + Script.construct:onPlayerBoarded(construct_onplayerboarded) --loading the function to trigger when a player board the construct 406 + Script.construct:onVRStationEntered(construct_vrstationentered) --loading the function to trigger when a player enter the VR Station 407 + Script.construct:onConstructDocked(construct_constructdocked) --loading the function to trigger when another construct is docked on this construct 408 + Script.construct:onPvPTimer(construct_onpvptimer) --loading the function to trigger when pvp timer is changing state
+1
source/unit/onStop.lua
··· 1 + Script.unit:stop()
+1
source/unit/onTimer.lua
··· 1 + Script.unit:timer(tag)