Page 1 of 1

[792] Real time script editing

Posted: 07 Jan 2020, 18:28
by Lobby
Hi,

if you tried to use Lua for your plugins in the past you may have noticed that restarting the game every time you made a change to the script can be really time consuming. Wouldn't it be nice - and way more productive - to be able to edit a script with the game applying the changed automatically in real time? That's exactly what can be done by a newly introduced helper script called #LuaWrapper.

Right now your json to include a script may look like that:

Code: Select all

    "script":"myscript.lua"


For real time scripting use this form:

Code: Select all

    "script":"#LuaWrapper",        // Use the wrapper as primary script
    "meta":{
      "luawrapper":{
        "script":"myscript.lua",   // This is your script file
        "dev":true                 // Actually enable real time editing
      }
    }
This looks complicated, so how does it work? The #LuaWrapper script (that is predefined in the game) handles loading and reloading the given script file "myscript.lua" once it detects changes. The "dev":true is there as an easy switch to disable real time scripting, e.g. for release of the plugin.

Caveats
Of course there are some caveats because of which real time scripting is not used by default for scripts. Especially, it should not be used in published plugins:
  • Performance
    The game makes static assumptions about when a script has to be executed. For example, if a script doesn't implement the script:update function then it won't even be considered once the update event triggers. Scripts that may change at any point in time cannot be optimized in this regard as they have to be considered all the time. Another aspect is that the #LuaWrapper redirects calls on the real time script which adds some overhead.
  • Lua lifecycle
    The lifecycle for Lua scripts provides a well defined order of when script functions will be called. See the documentation about that. Real time scripts break this lifecycle by being loaded at a later point in time (usually after all other scripts have been initialized). Although the #LuaWrapper ensures that the init functions of the real time script will be called, dependencies to other scripts might not work as expected.
  • Hidden script object
    For normal scripts you can use Draft:getScripts() to access the script objects that are attached to a draft. However, since the real time script is wrapped by the #LuaWrapper you can only get access to that one. The real time script doesn't exist as an usual script object, instead it uses a table that uses the #LuaWrapper as a prototype. This is done so that from the point of view of the real time script, it is a full fledged script object.

See here for a video of how real time editing looks like:
http://www.youtube.com/watch?v=EE9qoKTCbZ0

The example can be downloaded from here:
realtimelua.zip
(6.86 KiB) Downloaded 297 times

Re: [792] Real time script editing

Posted: 08 Jan 2020, 08:37
by Mg3094066
Not understanding rnything but ill say... its so great

Re: [792] Real time script editing

Posted: 08 Jan 2020, 12:52
by Lobby
:lol:

Added a video for clarity.

Re: [792] Real time script editing

Posted: 27 Jul 2020, 00:17
by KINGTUT10101
Does the script have to be named "myscript.lua"?

Re: [792] Real time script editing

Posted: 27 Jul 2020, 09:28
by Lobby
No, it just have to match with what you specified as script for the LuaWrapper :)

Re: [792] Real time script editing

Posted: 27 Jul 2020, 16:34
by KINGTUT10101
I asked because whenever I change the name of the script, I get this error:
Screenshot_20200727-093329.png

Code: Select all

[
	{
		"frames":[],
		"id":"TheoThoughts.kt101",
		"meta":{
			"luawrapper":{
				"dev":true,
				"script":"TT.lua"
			}
		},
		"script":"#LuaWrapper",
		"strictlua":true,
		"type":"script"
	}
]

Re: [792] Real time script editing

Posted: 27 Jul 2020, 23:39
by Lobby
Have you tried a lowercase name? In this specific instance it might make a difference.

Re: [792] Real time script editing

Posted: 28 Jul 2020, 04:48
by KINGTUT10101
That's odd. I guess I'll try it though.

Re: [792] Real time script editing

Posted: 28 Jul 2020, 04:50
by KINGTUT10101
Thanks for the help, that fixed the issue.

Re: [792] Real time script editing

Posted: 15 Feb 2022, 18:27
by erksmit
is there an event function for when a script is reloaded? reloading the script breaks its connection to a gui button i have made. If i can detect when it reloads i can use GUI.get to get it again