Dec: One VM to Rule Them All!
One Lua Virtual Machine to Rule Them All!
One Lua Virtual Machineโ
After much analysis, study and refactoring. I am happily introducing the new way the Packages will work! This was heavily motivated by the lack of possibility of creating extensions or customized items in an easy way.
In addition to the last new Entity System update, this change will heavily add to the possibilities of modding & scripting capabilities to nanos world!
Those changes are meant to have as little as possible impact and breaking changes, all tests I did so far assured everything is still backward compatible!
So far, we had one VM (Virtual Machine - or lua_State
) created per Package. This caused each Package to be completely isolated from each other. Therefore it was necessary to create mechanisms for communication between the Packages, such as Events and Package.Export/Package.Call methods, which allowed (in a limited way) Packages to communicate with each other.
But as mentioned, this brought many limitations, such as it was not possible for Packages to access variables or call functions directly from other Packages, or even worse (which become more evident from the last update of Entity System): It was not possible to share custom classes between Packages!
However now all of this will be solved! I reworked the entire internal scripting system, and now instead of having a new virtual machine per Package, we will have a virtualized sandboxed environment (using Lua environment _ENV
feature), separating the scope of each Package but allowing them to access a global shared environment!
This will allow the Packages to define classes, expose functions and share libraries globally! So that everyone else can use without the need for workaround mechanisms to do so!
Now, for example you can just load additional scripts together the sandbox
game-mode which loads custom Classes of tools or weapons, and those will appear automatically in the Spawn Menu, no need for manually adding them anymore!
Each Package will still have it's own sub-global environment, so defining global variables or functions will not make them available to other packages immediately.
Call & Exportโ
Package.Call
and Package.Export
are not necessary in the traditional way anymore. As now we can share the global environment, we can define methods and expose them to be called directly, without the need to Package.Call
them.
The Package.Export
will still exist and work similarly. But now it will be possible to "expose" any value to the global scope! Example:
local my_table = { 123, 456 }
function DoSomething(var1)
return var1 + 123
end
Package.Export("MyTable", my_table)
Package.Export("DoSomething", DoSomething)
Console.Log(MyTable[1])
-- 123
Console.Log(DoSomething(456))
-- 579
Inherited Classesโ
Now all Inherited Classes will be available to all Packages, as they will be created and exposed to the Global scope automatically.
The first parameter of it will now be used as the variable name to set the class in the global scope. Also, it just got a new optional 2nd parameter to define a custom table contained custom values to be set in the class table before it's created:
-- Note we are assigning the return of Inherit to a variable called
-- MyPropClassLocal, but also MyPropClass will be defined globally
-- as being our class, both variables points to the same Class table
MyPropClassLocal = Prop.Inherit("MyPropClass", {
"my_custom_value" = 123,
"my_another_value" = "hello"
})
local val = MyPropClass.my_custom_value
-- val == 123
We've also added a new static event for getting when a new Inherited Class is registered: ClassRegister
:
Prop.Subscribe("ClassRegister", function(class)
-- Do something with the new class
end)
Library Package Typeโ
With this new way Package work, library
types are no longer needed. As now we can communicate directly with other packages with exposed global methods, we can just define the methods to be exported with Package.Export
and use them whenever we want.
In this way we will only have two types of "lua script" Packages: game-mode
and script
. All existing library
packages will be automatically converted to script
when loaded.
RequirePackageโ
Therefore, Package.RequirePackage
will becomes unnecessary as well. As we can now reuse the same script globally, this method will be depreciated in favor of adding your package in the package_dependencies
list of your Package's Package.toml.
Package requiring the default nanos-world-vehicles
or nanos-world-weapons
will load them as a standalone Package, as now they export the NanosWorldWeapons
and NanosWorldVehicles
tables globally.
GameMode Settingsโ
We are introducing the concept of custom settings for game-modes! These settings can be defined in Package.toml like that:
# game-mode custom settings configurations
[custom_settings]
my_toggle = { label = "enable PVP", type = "boolean", description = "whether to enable PVP or not", default = true }
my_text_input = { label = "type anything", type = "text", description = "custom text!", default = "hello world!" }
These settings can be configured through server arguments --custom_settings "my_toggle = true, my_text_input = 'awesome text!', ..."
, or through the New Game screen, which parses and displays the configuration dynamically when creating a new game:
The values defined can be accessed through the new method Server.GetCustomSettings()
.
Please refer to the updated Documentation Page for more information.
Configuration Filesโ
As announced on previous blogs, the format of the configuration files are changing in the next update!
All Package.toml and Assets.toml in the old format will automatically self-convert to the new format as soon as they load for the first time.
Meta Blockโ
Now we have a new common header among all Assets.toml and Package.toml: [meta]
.
It will have the following format, which will be used to define general configurations mainly used by the store:
loading...
Packageโ
For the Packages, we will have a new format to differentiate their Types. Each Package type will have an independent and different block of configurations. Now the Package type will be determined by defining a block [game_mode]
, [script]
or [loading-screen]
.
This is how a game-mode Package.toml will look like:
loading...
Configuration Imagesโ
As you may have noticed, there is no more setting image
on Package.toml, the same applies to Assets.toml and for the server image in the Config.toml.
Now, the images should be physically placed next to the .toml file with the name Package.jpg
, Assets.jpg
or Server.jpg
.
This is a standardization to solve the image inconsistency between the store and the config file, as well as to avoid external and malicious links to be placed there. But this brings the drawback that only dedicated servers can display an image on the Server List from now on.
New Static Classesโ
Finally we are releasing the update with all the Static Class refactoring! This will give us a great new organization to find things at the docs, as now specific methods will be grouped into classes with names that make sense!
List of all new Static Classes: