Packages Communication
All you need to know to communicate between different Packages
In nanos world there are 3 main ways which you can interact with other Packages. In this page we will explain in details how to do so.
Custom Eventsβ
tip
For more information and examples about using Custom Events, please refer to Events.
Communicating with Packages in the same Sideβ
Custom Events are the easier way to send data or information to other packages:
local my_parameter1 = 123
local my_parameter2 = "hello there"
local my_parameter3 = { important_stuff = "omg!" }
Events.Call("MyAwesomeEvent", my_parameter1, my_parameter2, my_parameter3)
Events.Subscribe("MyAwesomeEvent", function(parameter1, parameter2, parameter3)
Package.Log("Received " .. parameter1) -- Received 123
Package.Log("Received " .. parameter2) -- Received hello there
Package.Log("Received " .. parameter3) -- Received table
end)
info
Note: Calling events will trigger the event in all Packages.
Sending data through the networkβ
Sending information through the network is not different from sending in the same side:
local my_parameter1 = "cool data from network"
-- Call this event to all Players
Events.BroadcastRemote("GetThisFromServer", my_parameter1)
local player_02 = GetPlayerSomehow()
-- Or call this event to a specific player
Events.CallRemote("GetThisFromServer", player_02, my_parameter1)
Events.Subscribe("GetThisFromServer", function(parameter1, parameter2, parameter3)
Package.Log("Received " .. parameter1) -- Received cool data from network
end)
info
Note: Calling network events will trigger the event in all Packages from the other side.
Packages Exported Functionsβ
Another way of communicating is using Exported Functions. With Exported Functions you can define functions in your Package which can be called by any other Package and have a value returned.
info
Unlike events, exported functions can return values to the caller. But you can only call Exported Functions from the same side (Server or Client).
Exporting a Functionβ
For exporting functions, use Package.Export()
method:
-- Defines a function which you want to export
function SpawnCoolWeapon(location, rotation)
local cool_weapon = Weapon(location or Vector(), rotation or Rotator(), ...)
return cool_weapon
end
-- Exports the function to be called by other Packages
Package.Export("SpawnCoolWeapon", SpawnCoolWeapon)
Calling an Exported Function from another Packageβ
For calling a function exported by another Package, use Package.Call()
method:
-- Calls the exported function
local cool_weapon = Package.Call("PackageOne","SpawnCoolWeapon", Vector(), Rotator())
Using Package's RequirePackageβ
The last way of communicating between Packages is by loading other Packages inside the current Package:
Package.Log("Hello from Package One!")
Package.RequirePackage("PackageOne")
Package.Log("Hello from Package Two!")
Outputβ
Hello from Package One!
Hello from Package Two!
This will load the file Index.lua from PackageOne into the VM of PackageOne where you called Package.RequirePackage()
.
Also the Required Package will be sent to the clients, then you can RequirePackage
in the client side too.
note
To be able to RequirePackage
in the Client, you must first RequirePackage
it in the Server, so it can be loaded and sent to Clients.