Noir
  • πŸšΆβ€β™‚οΈIntro
  • πŸ“Tutorials
    • πŸŽ“Starting Noir
    • πŸ“–Libraries
    • πŸ–₯️Services
    • ❔Libraries VS Services
    • πŸ’¬Callbacks
    • πŸ’₯Classes
    • πŸ”ŽDebugging
  • πŸ“šExamples
  • β˜„οΈAPI Reference
    • Bootstrapper
    • Built-Ins
      • Classes
        • AITarget
        • Body
        • Command
        • Connection
        • Event
        • HTTPRequest
        • HTTPResponse
        • Library
        • Message
        • Object
        • Player
        • Service
        • Task
        • TickIterationProcess
        • Tracker
        • Vehicle
        • Widgets
          • MapLabelWidget
          • MapLineWidget
          • MapObjectWidget
          • PopupWidget
          • ScreenPopupWidget
          • Widget
      • Libraries
        • Base64
        • Dataclasses
        • Deprecation
        • Events
        • HTTP
        • JSON
        • Logging
        • Matrix
        • Number
        • String
        • Table
      • Services
        • CommandService
        • GameSettingsService
        • HTTPService
        • MessageService
        • NotificationService
        • ObjectService
        • PlayerService
        • TaskService
        • TPSService
        • UIService
        • VehicleService
    • Callbacks
    • Class
    • Classes
    • Debugging
    • Definition
    • Libraries
    • Noir
    • Services
    • TypeChecking
Powered by GitBook
On this page
  • What Are Callbacks?
  • Connecting To A Callback
  • Removing A Callback Connection
Edit on GitHub
  1. Tutorials

Callbacks

This page will go over Noir's callbacks system.

What Are Callbacks?

Callbacks are simply functions defined in your addon like onPlayerJoin that get called by Stormworks.

In Addon Lua, we can listen for game callbacks by creating a function representing the name of the callback like so:

script.lua
function onPlayerJoin(...)
    ...
end

In Addon Lua, this is the only way. It's quite problematic though since we can only define a function with a specific name once, meaning we can only create one function per game callback.

Noir solves this by creating an event that wraps around a game callback. This event will then hold a bunch of functions, referred to as connections, that get called whenever the game callback is called.

Connecting To A Callback

It's super easy to connect to a game callback, simply use Noir.Callbacks:Connect(callbackName, function) or Noir.Callbacks:Once(callbackName, function)

Noir.Callbacks:Connect("onVehicleSpawn", function(vehicle_id, peer_id)
    server.announce("Server", "A vehicle was spawned by "..(server.getPlayerName(peer_id)))
end)

Behind the scenes, this automatically creates an onVehicleSpawn function in _ENV for Stormworks to use when onVehicleSpawn is triggered. Noir also retrieves or creates an event for this callback. This event is stored in Noir.Callbacks.Events. The created function essentially fires the created event with all the arguments provided by Stormworks, and the function passed to Noir.Callbacks:Connect() is automatically connected to the new event.

Removing A Callback Connection

Noir.Callbacks:Connect()and Noir.Callbacks:Once() both return a NoirConnection which comes with a method called :Disconnect().

This method essentially disconnects the connection from the event, meaning the function in the connection won't get triggered again if the event is called.

PreviousLibraries VS ServicesNextClasses

Last updated 1 year ago

See for an example on event creation, event connections, and disconnecting connections from events.

πŸ“
πŸ’¬
Events
Page cover image