Comunicação de pacotes
Tudo o que você precisa saber para se comunicar entre pacotes diferentes
In nanos world there are 2 main ways which you can interact with other Packages. Nesta página vamos explicar em detalhes como fazer isso.
Eventos Personalizados
Comunicação com pacotes do mesmo lado
Custom Events are the easier way to send or receive generic data or information to other packages:
local meu_parameter1 = 123
local meu_parameter2 = "olá lá"
local meu_parâmetroter3 = { important_stuff = "omg!" }
Eventos.Call("MyAwesomeEvent", meu_parameter1, meu_parâmetroter2, meu_parameter3)
Events.Subscribe("MyAwesomeEvent", function(parameter1, parameter2, parameter3)
Console.Log("Received " .. parameter1) -- Received 123
Console.Log("Received " .. parameter2) -- Received hello there
Console.Log("Received " .. parâmetro3) -- Tabela recebida
fim)
Enviando dados através da rede
Sending information through the network is not different from sending in the same side:
local my_parameter1 = "dados legais da rede"
-- Chame este evento para todos os jogadores
eventos. roadcastRemote("GetThisFromServer", my_parameter1)
player_02 = GetPlayerSomehow()
-- Ou chame este evento para um jogador específico
Eventos.CallRemote("GetThisFromServer", player_02, meu_parâmetroter1)
Events.SubscribeRemote("GetThisFromServer", function(parameter1, parameter2, parameter3)
Console.Log("Received " .. parâmetro 1) -- Dados legais recebidos da rede
fim)
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.
Unlike events, exported functions can return values to the caller. But exported functions are only available to the same side (Server or Client).
Exportando uma função
With that, you can export functions for example, like that:
-- 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)
You can even export a whole table containing functions for example, to work as a library:
-- 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)
Chamando uma função exportada de outro pacote
Now you can access that directly from other packages:
-- Calls the exported function
local cool_weapon = SpawnCoolWeapon(Vector(), Rotator())
-- Calls the exported table
local awesome_result = MyAwesomeLibrary.CoolMethod(123, 456)