Passer au contenu principal
Version: latest - a1.156.0 ⚖️

Communication entre Packages

Tout ce que vous avez besoin de savoir pour communiquer entre différents Packages

In nanos world there are 2 main ways which you can interact with other Packages. Dans cette page, nous vous expliquerons en détail comment faire.

Événements personnalisés​

Communiquer avec les Packages du même côté​

Custom Events are the easier way to send or receive generic data or information to other packages:

package-one/Server/Index.lua
local my_parameter1 = 123
local my_parameter2 = "hello there"
local my_parameter3 = { important_stuff = "omg!" }

Events.Call("MyAwesomeEvent", my_parameter1, my_parameter2, my_parameter3)
package-two/Server/Index.lua
Events.Subscribe("MyAwesomeEvent", function(parameter1, parameter2, parameter3)
Console.Log("Received " .. parameter1) -- Received 123
Console.Log("Received " .. parameter2) -- Received hello there
Console.Log("Received " .. parameter3.important_stuff) -- Received omg!
end)

Envoi de données via le réseau​

L'envoi d'informations via le réseau n'est pas différent de l'envoi dans le même côté:

Server/Index.lua
local my_parameter1 = "cool data from network"

-- Call this event to all Players
Events.BroadcastRemote("GetThisFromServer", Reliability.Reliable, my_parameter1)

local player_02 = GetPlayerSomehow()

-- Or call this event to a specific player
Events.CallRemote("GetThisFromServer", player_02, Reliability.Reliable, my_parameter1)
Client/Index.lua
Events.SubscribeRemote("GetThisFromServer", function(parameter1, parameter2, parameter3)
Console.Log("Received " .. parameter1) -- Received cool data from network
end)
tip

For more information and examples about using Custom Events, please refer to Events.

Exporting Variables Globally​

Another way of communicating is using Package.Export() method, it allows exposing variables (tables, functions, etc) globally so all other Packages can access it directly.

info

Contrairement aux événements, les fonctions exportées peuvent renvoyer des valeurs où elles sont appelées. But exported functions are only available to the same side (Server or Client).

Exportation d'une Fonction​

With that, you can export functions for example, like that:

package-one/Server/Index.lua
-- Defines a function which you want to export
function SpawnCoolWeapon(location, rotation)
local cool_weapon = Weapon(location or Vector(), rotation or Rotator(), "nanos-world::SK_AK47")
return cool_weapon
end

-- Exports the function to be called by other Packages
Package.Export("SpawnCoolWeapon", SpawnCoolWeapon)

You can even export a whole table containing functions for example, to work as a library:

package-one/Server/Index.lua
-- Defines a table with functions which you want to export
MyAwesomeLibrary = {
CoolMethod = function(a, b)
return a + b
end,
AnotherAwesomeImportantMethod = function(c, d)
return c * d
end
}

-- Exports the table to be accessed by other Packages
Package.Export("MyAwesomeLibrary", MyAwesomeLibrary)

Appel d'une Fonction Exportée depuis un autre Package​

Now you can access that directly from other packages:

attention

An exported variable only exists after the Package which exports it has loaded. If your Package depends on it, add the exporting Package to the packages_requirements of your Package.toml, so it's always loaded before yours. See Package Loading & Lua Environment.

package-two/Server/Index.lua
-- Calls the exported function
local cool_weapon = SpawnCoolWeapon(Vector(), Rotator())
package-two/Server/Index.lua
-- Calls the exported table
local awesome_result = MyAwesomeLibrary.CoolMethod(123, 456)