[792] Real time script editing

The Lua scripting language allows you to give your plugin more advanced features.

Moderator: Plugin Moderators

User avatar
Lobby
Developer
Posts: 3717
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

[792] Real time script editing

#1

Post 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 545 times

User avatar
Mg3094066
Inhabitant of a Supercluster
Posts: 3223
Joined: 28 May 2017, 01:50
Location: Venezuelan Republic of Mexico
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: [792] Real time script editing

#2

Post by Mg3094066 »

Not understanding rnything but ill say... its so great

User avatar
Lobby
Developer
Posts: 3717
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: [792] Real time script editing

#3

Post by Lobby »

:lol:

Added a video for clarity.

User avatar
KINGTUT10101
1,000,000 inhabitants
Posts: 2226
Joined: 07 Jul 2016, 22:50
Location: 'Merica
Plugins: Showcase Store
Version: Beta
Contact:

Plugin Creator

Platform

Re: [792] Real time script editing

#4

Post by KINGTUT10101 »

Does the script have to be named "myscript.lua"?

User avatar
Lobby
Developer
Posts: 3717
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: [792] Real time script editing

#5

Post by Lobby »

No, it just have to match with what you specified as script for the LuaWrapper :)

User avatar
KINGTUT10101
1,000,000 inhabitants
Posts: 2226
Joined: 07 Jul 2016, 22:50
Location: 'Merica
Plugins: Showcase Store
Version: Beta
Contact:

Plugin Creator

Platform

Re: [792] Real time script editing

#6

Post 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"
	}
]

User avatar
Lobby
Developer
Posts: 3717
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: [792] Real time script editing

#7

Post by Lobby »

Have you tried a lowercase name? In this specific instance it might make a difference.

User avatar
KINGTUT10101
1,000,000 inhabitants
Posts: 2226
Joined: 07 Jul 2016, 22:50
Location: 'Merica
Plugins: Showcase Store
Version: Beta
Contact:

Plugin Creator

Platform

Re: [792] Real time script editing

#8

Post by KINGTUT10101 »

That's odd. I guess I'll try it though.

User avatar
KINGTUT10101
1,000,000 inhabitants
Posts: 2226
Joined: 07 Jul 2016, 22:50
Location: 'Merica
Plugins: Showcase Store
Version: Beta
Contact:

Plugin Creator

Platform

Re: [792] Real time script editing

#9

Post by KINGTUT10101 »

Thanks for the help, that fixed the issue.

User avatar
erksmit
Villager
Posts: 23
Joined: 02 Mar 2018, 12:39
Plugins: Showcase Store
Version: Beta

Plugin Creator

Re: [792] Real time script editing

#10

Post 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

Post Reply Previous topicNext topic

Return to “Lua Scripting”