Event.Force module

Force global creation.

All new forces will be added to the global.forces table.

This modules events should be registered after any other Init functions but before any scripts needing global.players.

This modules can register the following events: on_force_created, and on_forces_merging.

Usage

local Force = require('__stdlib__/stdlib/event/force').register_events()
-- inside your Init event Force.init() -- to properly handle any existing forces

Functions

get(force) Get game.forces[name] & global.forces[name], or create global.forces[name] if it doesn’t exist.
add_data_all(data) Merge a copy of the passed data to all forces in global.forces.
init([event][, overwrite=false]) Init or re-init a force or forces.
merged(event) When forces are merged, just remove the original forces data
register_events(do_on_init) Register Events

Functions

# get(force)

Get game.forces[name] & global.forces[name], or create global.forces[name] if it doesn’t exist.

Parameters: Returns:
  • (LuaForce) the force instance
  • (table) the force’s global data
Usage:
local Force = require('__stdlib__/stdlib/event/force')
local force_name, force_data = Force.get("player")
local force_name, force_data = Force.get(game.forces["player"])
-- Returns data for the force named "player" from either a string or LuaForce object
# add_data_all(data)

Merge a copy of the passed data to all forces in global.forces.

Parameters:
  • data : (table) a table containing variables to merge
Usage:
local data = {a = "abc", b = "def"}
Force.add_data_all(data)
# init([event][, overwrite=false])

Init or re-init a force or forces.

Passing a nil event will iterate all existing forces.

Parameters:
  • event : (string or table) table or a string containing force name (optional)
  • overwrite : (boolean) the force data (default: false)
# merged(event)

When forces are merged, just remove the original forces data

Parameters:
  • event
# register_events(do_on_init)

Register Events

Parameters:
  • do_on_init