Script Messager

The script messager is a lightweight object designed to allow separate script systems to send and listen for string messages. It is very much like a cut-down event system, without the congestion of events that are naturally triggered by the game. Its main purpose is to underpin the mechanics of both the generated_battle system in battle and the narrative system in campaign.

Unlike the events system, the script messager supports the blocking of messages, so that one bit of script can prevent the transmission of a specific message by any other bit of script.

It is rare to need to get a handle to a script messager object directly, as the generated_battle system stores an internal reference to it and calls it automatically when necessary.

Loaded in Campaign Loaded in Campaign
Loaded in Battle Loaded in Battle
Loaded in Frontend Loaded in Frontend
Back to top

Creation

script_messager:new()

Gets or creates a script_messager object.

Returns:

  1. script_messager

defined in ../../Warhammer/working_data/script/_lib/lib_script_messager.lua, line 54

Back to top

Usage

Once an script_messager object has been created with script_messager:new, functions on it may be called in the form showed below.

Example - Specification:

<script_messager_object>:<function_name>(<args>)

Example - Creation and Usage:

local sm = script_messager:new()                    -- set up automatically by campaign or battle managers
sm:add_listener(                                    -- calling a function on the object once created
    "test_message",
    function() out("* test_message received") end
)
Back to top

Messages

script_messager:add_listener(
  
string message,
  function
callback to call,
  [boolean
persistent],
  [function
condition],
  [string
listener name]
)

Adds a listener for a message. If the specified message is received, the specified callback is called. If the third parameter is set to true then the listener will continue after it calls the callback and will listen indefinitely.

Parameters:

1

string

Message to listen for.

2

function

Target to call when the message is received and the optional condition passes.

3

boolean

optional, default value=false

Continue to listen after the target callback has been called.

4

function

optional, default value=nil

Condition function which must pass for the target callback to be called. The condition function is called when the message is received, and will be passed the message context as a single argument. It must return true, or a value that equates to true in a boolean comparison, for the condition to pass. If no condition function is supplied then the condition always passes.

5

string

optional, default value=nil

Name for this listener, by which it may be later cancelled.

Returns:

  1. nil

defined in ../../Warhammer/working_data/script/_lib/lib_script_messager.lua, line 122

script_messager:trigger_message(string message name, [table context data])

Triggers a string message. This prompts the messager system to notify any listeners for the subject message and call the callback that those listeners registered. An optional table of context data can be supplied to be passed through to the listening scripts.

Parameters:

1

string

message name

2

table

optional, default value=nil

context data

Returns:

  1. nil

defined in ../../Warhammer/working_data/script/_lib/lib_script_messager.lua, line 170

script_messager:remove_listener_by_message(string message name)

Removes any listener listening for a particular message.

Parameters:

1

string

message name

Returns:

  1. nil

defined in ../../Warhammer/working_data/script/_lib/lib_script_messager.lua, line 231

script_messager:remove_listener_by_name(string message name)

Removes any listener by name.

Parameters:

1

string

message name

Returns:

  1. nil

defined in ../../Warhammer/working_data/script/_lib/lib_script_messager.lua, line 244

script_messager:block_message(string message name, [boolean should block])

Blocks or unblocks a message from being transmitted in the future. If a message is blocked, no listeners will be notified when script_messager:trigger_message is called.

Parameters:

1

string

message name

2

boolean

optional, default value=true

should block

Returns:

  1. nil

defined in ../../Warhammer/working_data/script/_lib/lib_script_messager.lua, line 263

Last updated 12/08/2022 11:56:58