2022 Nov: Entity System, UDS & UE5.1!
Entity System, UDS & UE5.1!
Refactor Releases
As announced in the last Blog News, we are performing a series of refactoring in the API to standardize it better. We have a long list of changes and I'm doing them gradually.
The main one that allowed us to make more changes without breaking compatibility is the compatibility_version
released in the last update. This configuration on Package.toml will ensure that the package continues to work in future updates in cases of breakage changes.
Also we've already got our first feature to try the compatibility_version system: the new Events.SubscribeRemote
method! This method will subscribe only for remote called methods, splitting completely the behavior of subscribing for Local and Remote events. The change of this method caused a breaking change because all existing Events.Subscribe
will no longer trigger with remote Events. But compatibility_version ensures that they continue to work until you update the value to version 1.22
or more recent.
I'm working on several Static Class refactoring as well and also changing some names of methods (e.g. HTTP.RequestSync
will be standardized into just HTTP.Request
and the async version would become HTTP.RequestAsync
).
I reinforce again that if you have suggestions on other changes such as event or methods names, do not hesitate to say it! Big refactoring opportunities like this are rare!
Entity & Inheriting System
This is a very special update which I've been working in the past months! 🥳
The new Inheriting System is an addition that completely changes the paradigm of creating scripts! This system allows you to inherit existing classes to create your own custom classes and spawn them as if they were native!
-- Creates a new Class called "MyNewClass" inheriting from Prop
MyNewClass = Prop.Inherit("MyNewClass")
-- Spawn it using the default constructor
local ent = MyNewClass(Vector(), Rotator(), "nanos-world::SM_Cube")
Built-in Events
Native events will return your class instead of the native class. E.g.:
-- This will trigger for all Props and it's descendents,
-- and the value of 'prop' will be the children itself
Prop.Subscribe("Destroy", function(prop)
-- You can check if an entity is from a inherited Class type!
-- IsA checks recursively if any parent is from that type
if (NanosUtils.IsA(prop, MyCube)) then
-- prop is of type MyCube
end
end)
Custom Methods
Easily add new methods to your class!
MyNewClass = Prop.Inherit("MyNewClass")
function MyNewClass:MyMethod()
-- Do something
end
Custom Constructors
This system also allows the creation of personalized constructors:
-- Creates a new Class called "MyCube" inheriting from Prop
MyCube = Prop.Inherit("MyCube")
-- Defines my constructor with any parameters you desire
function MyCube:Constructor(loc, rot)
-- Do any kind of logic here
loc = loc + Vector(0, 0, 100)
-- Calls Super Constructor to finalize the construction
self.Super:Constructor(loc, rot, "nanos-world::SM_Cube")
end
-- Spawn it using your custom constructor
local cube = MyCube(Vector(123, 456, 100), Rotator())
Multiple Inheritance
We can inherit from other inherited classes as well!
MyCube = Prop.Inherit("MyCube")
MyMiniCube = MyCube.Inherit("MyMiniCube")
MySuperMiniCube = MyMiniCube.Inherit("MySuperMiniCube")
Custom Entity Events
Now we have the possibility to trigger remote events on custom Classes, to allow easy networking communication:
function MyCube:OnMyAnotherEvent(my_param)
Console.Log(my_param)
end
MyCube.SubscribeRemote("MyAnotherEvent", MyCube.OnMyAnotherEvent)
local my_cube = MyCube()
my_cube:CallRemoteEvent("MyCustomEvent", 123)
function MyCube:OnMyCustomEvent(player, my_param)
self:BroadcastRemoteEvent("MyAnotherCustomEvent", "hello")
end
MyCube.SubscribeRemote("MyCustomEvent", MyCube.OnMyCustomEvent)
Global Registry
Through the parent class, we can get a list of all children classes of that class, having a global registry of all existing classes!
local children_classes = ToolGun.GetInheritedClasses()
for _, class in pairs(children_classes) do
-- 'class' is a custom inherited class! we can spawn it
local p = class()
end