Skip to main content

LuaBootstrap

Struct LuaBootstrap 

Source
pub struct LuaBootstrap;
Expand description

Entry point for registering event handlers. It is accessible through the global object named script.

Implementations§

Source§

impl LuaBootstrap

Source

pub fn active_mods(&self) -> HashMap<String, String>

A dictionary listing the names of all currently active mods and mapping them to their version.

Source

pub fn feature_flags(&self) -> LuaBootstrapFeatureFlags

A dictionary of feature flags mapping to whether they are enabled.

Source

pub fn level(&self) -> LuaBootstrapLevel

Information about the currently running scenario/campaign/tutorial.

Source

pub fn mod_name(&self) -> &str

The name of the mod from the environment this is used in.

Source

pub fn object_name(&self) -> &str

The class name of this object. Available even when valid is false. For LuaStruct objects it may also be suffixed with a dotted path to a member of the struct.

Source

pub fn generate_event_name(&self) -> &str

Generate a new, unique event ID that can be used to raise custom events with LuaBootstrap::raise_event.

Source

pub fn get_event_filter(&self, event: LuaAny) -> Option<LuaAny>

Gets the filters for the given event.

Source

pub fn get_event_handler(&self, event: LuaAny) -> Option<LuaAny>

Find the event handler for an event.

Source

pub fn get_event_id(&self, event: LuaAny) -> &str

Converts LuaEventType into related value of defines.events. Value will be provided also if event was not given a constant inside of defines.events.

Source

pub fn get_event_order(&self) -> &str

Gets the mod event order as a string.

Source

pub fn new_notification_queue(&self) -> LuaNotificationQueue

Creates new empty instance of LuaNotificationQueue

Source

pub fn on_configuration_changed(&self, handler: Option<LuaAny>)

Register a function to be run when mod configuration changes.

This is called when the game version or any mod version changed, when any mod was added or removed, when a startup setting has changed, when any prototypes have been added or removed, or when a migration was applied. It allows the mod to make any changes it deems appropriate to both the data structures in its storage table or to the game state through LuaGameScript.

For more context, refer to the Data Lifecycle page.

Source

pub fn on_event( &self, event: LuaAny, filters: Option<LuaAny>, handler: Option<LuaAny>, )

Register a handler to run on the specified event(s). Each mod can only register once for every event, as any additional registration will overwrite the previous one. This holds true even if different filters are used for subsequent registrations.

Source

pub fn on_init(&self, handler: Option<LuaAny>)

Register a function to be run on mod initialization.

This is only called when a new save game is created or when a save file is loaded that previously didn’t contain the mod. During it, the mod gets the chance to set up initial values that it will use for its lifetime. It has full access to LuaGameScript and the storage table and can change anything about them that it deems appropriate. No other events will be raised for the mod until it has finished this step.

For more context, refer to the Data Lifecycle page.

Source

pub fn on_load(&self, handler: Option<LuaAny>)

Register a function to be run on save load. This is only called for mods that have been part of the save previously, or for players connecting to a running multiplayer session.

It gives the mod the opportunity to rectify potential differences in local state introduced by the save/load cycle. Doing anything other than the following three will lead to desyncs, breaking multiplayer and replay functionality. Access to LuaGameScript is not available. The storage table can be accessed and is safe to read from, but not write to, as doing so will lead to an error.

The only legitimate uses of this event are these:

  • Re-setup metatables as they are not persisted through the save/load cycle.

  • Re-setup conditional event handlers, meaning subscribing to an event only when some condition is met to save processing time.

  • Create local references to data stored in the storage table.

For all other purposes, LuaBootstrap::on_init, LuaBootstrap::on_configuration_changed or migrations should be used instead.

For more context, refer to the Data Lifecycle page.

Source

pub fn on_nth_tick(&self, handler: Option<LuaAny>, tick: LuaAny)

Register a handler to run every nth-tick(s). When the game is on tick 0 it will trigger all registered handlers.

Source

pub fn raise_biter_base_built( &self, params: LuaBootstrapRaiseBiterBaseBuiltParams, )

Source

pub fn raise_console_chat(&self, params: LuaBootstrapRaiseConsoleChatParams)

Source

pub fn raise_event(&self, data: LuaAny, event: LuaAny)

Raise an event. Only events generated with LuaBootstrap::generate_event_name and the following can be raised:

Source

pub fn raise_market_item_purchased( &self, params: LuaBootstrapRaiseMarketItemPurchasedParams, )

Source

pub fn raise_player_crafted_item( &self, params: LuaBootstrapRaisePlayerCraftedItemParams, )

Source

pub fn raise_player_fast_transferred( &self, params: LuaBootstrapRaisePlayerFastTransferredParams, )

Source

pub fn raise_script_built(&self, params: LuaBootstrapRaiseScriptBuiltParams)

Source

pub fn raise_script_destroy(&self, params: LuaBootstrapRaiseScriptDestroyParams)

Source

pub fn raise_script_destroy_segmented_unit( &self, params: LuaBootstrapRaiseScriptDestroySegmentedUnitParams, )

Source

pub fn raise_script_revive(&self, params: LuaBootstrapRaiseScriptReviveParams)

Source

pub fn raise_script_set_tiles( &self, params: LuaBootstrapRaiseScriptSetTilesParams, )

Source

pub fn raise_script_teleported( &self, params: LuaBootstrapRaiseScriptTeleportedParams, )

Source

pub fn register_metatable(&self, metatable: LuaAny, name: &str)

Register a metatable to have linkage recorded and restored when saving/loading.

The metatable itself will not be saved. Instead, only the linkage to a registered metatable is saved, and the metatable registered under that name will be used when loading the table.

register_metatable() can not be used in the console, in event listeners or during a remote.call().

The metatable first needs to be defined in the mod’s root scope, then registered using this method. From then on, it will be properly restored for tables in storage.


local metatable =
{
  __index = function(key)
    return "no value for key " .. key
  end
}
script.register_metatable("my_metatable", metatable)
```text


This previously defined `metatable` can then be set on any table as usual:

```text

local table = {key="value"}
setmetatable(table, metatable)
```text
Source

pub fn register_on_object_destroyed(&self, object: LuaAny) -> (u64, u64, &str)

Registers an object so that after it’s destroyed, on_object_destroyed is called.

Once an object is registered, it stays registered until it is actually destroyed, even through save/load cycles. The registration is global across all mods, meaning once one mod registers an object, all mods listening to on_object_destroyed will receive the event when it is destroyed. Registering the same object multiple times will still only fire the destruction event once, and will return the same registration number.

Depending on when a given object is destroyed, on_object_destroyed will either be fired at the end of the current tick or at the end of the next tick.

Source

pub fn set_event_filter(&self, event: LuaAny, filters: Option<LuaAny>)

Sets the filters for the given event. The filters are only retained when set after the actual event registration, because registering for an event with different or no filters will overwrite previously set ones.

Limit the on_marked_for_deconstruction event to only be received when a non-ghost entity is marked for deconstruction.


script.set_event_filter(defines.events.on_marked_for_deconstruction, {{filter = "ghost", invert = true}})
```text


Limit the [on_built_entity](runtime:on_built_entity) event to only be received when either a `unit` or a `unit-spawner` is built.

```text

script.set_event_filter(defines.events.on_built_entity, {{filter = "type", type = "unit"}, {filter = "type", type = "unit-spawner"}})
```text


Limit the [on_entity_damaged](runtime:on_entity_damaged) event to only be received when a `rail` is damaged by an `acid` attack.

```text

script.set_event_filter(defines.events.on_entity_damaged, {{filter = "rail"}, {filter = "damage-type", type = "acid", mode = "and"}})
```text

Trait Implementations§

Source§

impl Clone for LuaBootstrap

Source§

fn clone(&self) -> LuaBootstrap

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for LuaBootstrap

Source§

impl Debug for LuaBootstrap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LuaBootstrap

Source§

fn default() -> LuaBootstrap

Returns the “default value” for a type. Read more
Source§

impl Eq for LuaBootstrap

Source§

impl From<LuaBootstrap> for LuaAny

Source§

fn from(_: LuaBootstrap) -> Self

Converts to this type from the input type.
Source§

impl LuaObject for LuaBootstrap

Source§

impl PartialEq for LuaBootstrap

Source§

fn eq(&self, other: &LuaBootstrap) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for LuaBootstrap

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.