0 of 0

File information

Last updated

Original upload

Created by

Adam

Uploaded by

AdamMil01

Virus scan

Safe to use

About this mod

Implement mods without overwriting whole files, to improve mod compatibility

Requirements
Permissions and credits
Changelogs
NOTE: This mod is experimental. Please report problems if you have them.

This mod implements a few hooks that can be used to implement many types of mods without overwriting script files. It is rudimentary at this point, but can be expanded with additional capabilities if and when they're needed. The mod currently exposes the following functionality, as functions that you can call:

INSTALLATION

Just drop the .zip file into your game's data directory. No need to unzip it. NOTE: If you just want to use mods that depend on mod_hooks, you can stop reading here. The rest of this page is only for people who want to develop new mods.

FUNCTIONS:

::mods_addHook(name, func = null)
Registers a hook that will be called by ::mods_callHook(name). Multiple functions can be registered for the same hook, in which case they'll be called in order. Arguments:
name - The name of the hook, which is arbitrary.
func - The function that will be called when the hook is invoked, or null to simply create the hook without registering a function. The function may take any number of parameters. If the hook function returns a non-null value, that value will be returned from ::mods_callHook and further hook processing will be halted.

::mods_callHook(name, ...)
Invokes hook functions by name. If no hooks have been registered, nothing will happen. If a hook function returns a non-null value, that value will be returned and further hook processing will be halted. If all hook functions return null, the function returns null. Arguments:
name - The name of the hook, which should match that provided to ::mods_addHook.
All additional arguments will be passed to the hook function.

::mods_removeHook(name, func)
Removes a hook that was previously registered. Arguments:
name - The name of the hook, which should match that provided to ::mods_addHook.
func - The function to remove. This must be the exact same function object that was registered.

::mods_hookNewObject(name, func, false)
A convenience method for hooking into the instantiation of a new object. Arguments:
name - The name of the object. If the object is implemented in scripts/x/y/z.cnut, the name should be "x/y/z"
func - A function that takes one argument, which is the newly instantiated object. Normally the function should return null, but if it returns a different value that value will be substituted for the allocated object.
once - If true, the hook will only be called the first time the object is instantiated (per campaign load). This is useful when hooking global objects so you can avoid degrading performance by leaving the hook around. The default is false.

::mods_hookNewObjectOnce(name, func)
A convenience method that calls ::mods_hookNewObject(name, func, true).

::mods_hookClass(name, func, abstract = false)
A convenience method for hooking into the creation and derivation of classes. The method is a bit unusual, since it hooks when the class is instantiated and when child classes are derived (but not when child classes are instantiated). It does not hook when grandchild classes are derived, but the cascading mechanism probably takes care of that. Arguments:
name - The name of the class. If the class is implemented in scripts/x/y/z.cnut, the name should be "x/y/z".
func - A function that takes one argument, which is either the newly instantiated class instance or the newly derived subclass. You should use ::mods_override to modify the object.
abstract - If true, it's assumed that the class is abstract and no hook is registered for when the class in instantiated. Thus, func is only called for subclasses. The default is false.

::mods_hookBaseClass(name, func)
A convenience method that calls ::mods_hookClass(name, func, true).

::mods_isClass(obj, name)
Determines whether an object is a given Battle Brothers class, or derived from it. Arguments:
obj A table representing a Battle Brothers object
name A short class name, such as "player" (not the full name "/scripts/entities/tactical/player")

::mods_addMember(obj, className, memberName, value)
Sets a property of an object or class. The property need not exist. Arguments:
obj - The object or class whose property should be set
className - The short name of the class where the property should be set. If specified, the property will be set on the given class (or base class) if the object is or derives from the given class. If null, the property will not be set on any particular class.
memberName - The name of the property to set
value - The value that the property should be set to

::mods_getMember(obj, memberName)
Gets a property of an object or class. The property must exist. Arguments:
obj - The object or class whose property should be retrieved
memberName - The name of the property to retrieve

::mods_override(obj, key, value)
Sets a property of an object or class. The property must already exist. If the object is a class, the method is capable of following the chain of inheritance and setting the property on a base class if needed. Arguments:
obj - The object or class whose property should be set
key - The name of the property to set
value - The value that the property should be set to

::mods_registerCSS(path)
Registers a CSS hook, which will be processed when the main menu is loaded for the first time. Arguments:
path - The path to a CSS file, relative to ui/mods/. For example, if "mod_foo.css" is passed, the ui/mods/mod_foo.css file will be linked into the UI.

::mods_registerJS(path)
Registers a Javascript hook, which will be executed when the main menu is loaded for the first time. Arguments:
path - The path to a Javascript file, relative to ui/mods/. For example, if "mod_foo.js" is passed, the ui/mods/mod_foo.js file will be executed.

::mods_registerMod(codeName, version, friendlyName = null, extra = null)
Registers a mod. Calling this method is not required but will allow other mods to know about and potentially respond to the existence of your mod. Arguments:
codeName - The code name of your mod. This should be short and unique and should never change. It is the stable, unique identifier that other mods can use to detect your mod.
version - An integer specifying the version of your mod.
friendlyName - An optional, friendly name for your mod meant to be displayed to users. If omitted, codeName will be used.
extra - An optional table containing additional properties you want to associated with your mod registration

::mods_getRegisteredMods()
Gets a list of all registered mods. Each mod will be described by table with these properties:
Name - The mod's code name, as passed to ::mods_registerMod
Version - The mod's version
FriendlyName - The mod's friendly name
... and any additional properties specified in the extra parameter to ::mods_registerMod.

BUILT-IN HOOKS

inherit
Called when a class is inheriting from a base class. This can be used to change class members. The hook should normally return null, but if it returns a different value that value will be substituted for the class. The hook takes two arguments:
baseScriptName - The name of the base class script, e.g. "/scripts/items/weapon"
c - The new derived class

new
Called whenever a new object is allocated. This can be used to change object members. The hook should normally return null, but if it returns a different value that value will be substituted for the new object. The hook takes two arguments:
scriptName - The name of the object allocated, e.g. "/scripts/items/weapons/knife"
obj - The newly allocated object.

beforeCampaignLoad
Called before a campaign is loaded. The campaign will not be initialized yet. No arguments.

onCampaignLoaded
Called after a campaign is loaded. The hook is passed the world_state object as an argument.

onEntityLoaded
Called when a world entity (e.g. scripts/entity/world/*) is loaded from a saved game. The hook is passed the entity as an argument.

root_state.onInit
Called when the game is loaded. The game will not be fully initialized yet. The hook takes one argument:
rootState - A reference to the root_state object

EXAMPLE

For example mods implemented using these hooks, see any of my mods for Battle Brothers.

CONFLICTS

This mod conflicts with others that change scripts/root_state.nut or ui/main.html.
Top