Skip to content

ModEvents and ModEventHooks

Thomas edited this page Apr 14, 2022 · 14 revisions

ModEvents and ModEventHooks

ModEvents and ModEventHooks are the backbone of the API. They are special event classes that are used to broadcast events to one or multiple mods at the same time. They are extremely convenient and designed to be as easy to use as possible. This page will contain info about ModEvents and ModEventHooks as they pertain to the No Mans Sky API. If you are interested in learning more about them in general, check out the wiki page dedicated to them, which you can find Here.

ModEvents and ModEventHooks are used wherever possible to provide more possibilities to modders. Most of the time they are linked to actual hooked functions within No Man's Sky. Anything general like OnMainMenu, OnGameJoined, OnProfileSelected, etc will be a regular ModEvent. Since they are just notifying modders of the current state of the game, they don't need the "before and after" options that a ModEventHook provides. For everything else, like OnHealthChanged, OnUnitsChaged, etc, "before and after" options are practically required since we need the ability to run our code before or after the change. For these situations a ModEventHook will always be used to provide the maximum control to mod makers. Lastly, every ModEvent and ModEventHook in the API will be using the "Shared" implementation to separate listeners by Mod. You can learn more about this in the wiki page mentioned above.

Examples

Game.OnMainMenu += () => Logger.WriteLine("We just reached the main menu!");
Game.OnProfileSelected += () => Logger.WriteLine("The player just selected a save file!");
Player.Health.OnValueChanged.Prefix += (newValue) =>
{
    Logger.WriteLine($"The player's health is about to be {newValue}. Changing it to 999 instead.");
    newValue.value = 999;
}
Player.Units.OnValueChanged.Postfix += (newValue) => Logger.WriteLine($"The Player's Units have changed to {newValue}");

The examples above are all using the Lambda syntax for ModEvents and ModEventHooks. I prefer this way since I believe it's more convenient, however there are multiple ways you can use ModEvents. Check the wiki page above to see all of the different ways to use ModEvents.

Current ModEvents

Below is a list of all of the current ModEvents:

// Called when the Game Instance of the API is first initialized. 
// This is called extremely early on and is mainly used by the API
public IModEvent OnInitialized { get; set; }
// Called every time the player reaches the main menu
public IModEvent OnMainMenu { get; set; }
// Called when a player selects a save file to play.
public IModEvent OnProfileSelected { get; set; }
// Called when a player loads into the game after selecting a save file to play.
public IModEvent OnGameJoined { get; set; }
// Called when the Inventories UI screen opens. The one you open by pressing TAB
public IModEvent OnInventoriesOpened { get; set; }
// Called when the Inventories UI is closed.
public IModEvent OnInventoriesClosed { get; set; }
// Called when the address for GcPlayerStateData is acquired.
// Passes the address as an argument.
public IModEvent<long> Player.OnPlayerStateAquired { get; set; } 
// Called when the address of the actual Player instance in the game is acquired.
// Passes the address as an argument.
public IModEvent<long> Player.OnBaseAddressAquired { get; set; } 

ModEventHooks

Below this you can find all of the current ModEventHooks in the API:

// Called once per frame when the game's Game Loop executes.
// Hooked to the actual game loop
public IModEventHook<T> GameLoop.OnUpdate { get; set; }
// Every Stat<T> object, like Player.Health, Player.Units, etc, has this So there's a lot of them
// Called whenever the value of a stat changes.
public IModEventHook<T> Stat<T>.OnValueChanged { get; set; }

In addition to the ModEventHooks mentioned above, every single Game Hook exposes a ModEventHook for more advanced modders to take advantage of. For example, exposing the Update method for InventoriesGUI, MainMenu, Game, InGame, Player, etc is a lot to process for your average modder and makes things cluttered. More advanced modders would have no issue accessing it from a more remote place. Below is an example for the MainMenu.Update function.

// Called once every frame when the MainMenu is updated by the game.
public IModEventHook Hooks.GameHooks.MainMenu_Update.ModEventHook { get; set; }

You can find out more about the hooks by going to their respective class located under the NoMasSky.Api.Hooks namespace. Just a warning now, due to how Hooking works in C# with Reloaded, it doesn't look as pretty as you'd wish it did.

Clone this wiki locally