Paketkommunikation
Alles, was Sie wissen müssen, um zwischen verschiedenen Paketen zu kommunizieren
In nanos world there are 2 main ways which you can interact with other Packages. Auf dieser Seite werden wir Ihnen detailliert erklären, wie Sie dies tun können.
Eigene Ereignisse
Kommunikation mit Paketen auf der gleichen Seite
Custom Events are the easier way to send or receive generic data or information to other packages:
local my_parameter1 = 123
local my_parameter2 = "halllo"
local my_parameter3 = { important_stuff = "omg!" }
Events.Call("MyAwesomeEvent", my_parameter1, my_parameter2, my_parameter3)
Events.Subscribe("MyAwesomeEvent", function(parameter1, parameter2, parameter3)
Console.Log("Received " .. parameter1) -- Received 123
Console.Log("Received " .. parameter2) -- Received hello there
Console.Log("Received " .. parameter3) -- Empfangene Tabelle
end)
Daten werden über das Netzwerk gesendet
Sending information through the network is not different from sending in the same side:
local my_parameter1 = "coole Daten aus dem Netzwerk"
-- Rufe dieses Ereignis an alle Spieler
Events. roadcastRemote("GetThisFromServer", my_parameter1)
local player_02 = GetPlayerSomehow()
-- Oder rufen Sie dieses Ereignis an einen bestimmten Spieler
Events.CallRemote("GetThisFromServer", player_02, my_parameter1)
Events.SubscribeRemote("GetThisFromServer", function(parameter1, parameter2, parameter3)
Console.Log("Received " .. parameter1) -- Erhaltene coole Daten vom Netzwerk
Ende)
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).
Exportieren einer Funktion
With that, you can export functions for example, like that:
-- Definiert eine Funktion, die Sie exportieren wollen
Funktion SpawnCoolWeapon(Position, Rotation)
lokale cool_weapon = Waff(Position oder Vector(), Rotation oder 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)
Eine Exportierte Funktion aus einem anderen Paket aufrufen
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)