Skip to main content

LuaHelpers

Struct LuaHelpers 

Source
pub struct LuaHelpers;
Expand description

Provides various helper and utility functions. It is accessible through the global object named helpers in all stages (settings, prototype and runtime).

Implementations§

Source§

impl LuaHelpers

Source

pub fn game_version(&self) -> &str

Current version of game

Source

pub fn instrument_mod(&self) -> &str

The name of the active Instrument Mode mod, if any.

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 stage(&self) -> LuaHelpersStage

Stage of scripting that is currently running

Source

pub fn check_prototype_translations(&self)

Goes over all items, entities, tiles, recipes, technologies among other things and logs if the locale is incorrect.

Also prints true/false if called from the console.

Not available in settings and prototype stages.

Source

pub fn compare_versions(&self, first: &str, second: &str) -> i32

Compares 2 version strings.

Source

pub fn create_profiler(&self, stopped: Option<bool>) -> LuaProfiler

Creates a LuaProfiler, which is used for measuring script performance.

LuaProfiler cannot be serialized.

Not available in settings and prototype stages.

Source

pub fn decode_string(&self, string: &str) -> Option<&str>

Base64 decodes and inflates the given string.

Source

pub fn direction_to_string(&self, direction: &str) -> &str

Converts the given direction into the string version of the direction.

Source

pub fn encode_string(&self, string: &str) -> Option<&str>

Deflates and base64 encodes the given string.

Source

pub fn evaluate_expression( &self, expression: MathExpression, variables: Option<HashMap<String, f64>>, ) -> f64

Evaluate an expression, substituting variables as provided.

Source

pub fn is_valid_ambient_sound(&self, name: &str) -> bool

Checks if an ambient sound of a given name is valid.

Not available in settings and prototype stages.

Source

pub fn is_valid_animation_path(&self, name: &str) -> bool

Checks if the given animation name is valid and contains a loaded animation.

Not available in settings and prototype stages.

Source

pub fn is_valid_sound_path(&self, sound_path: SoundPath) -> bool

Checks if the given SoundPath is valid.

Not available in settings and prototype stages.

Source

pub fn is_valid_sprite_path(&self, sprite_path: SpritePath) -> bool

Checks if the given SpritePath is valid and contains a loaded sprite. The existence of the image is not checked for paths of type file.

Not available in settings and prototype stages.

Source

pub fn json_to_table(&self, json: &str) -> Option<LuaAny>

Convert a JSON string to a table.

Source

pub fn multilingual_to_lower(&self, input: &str) -> &str

Converts the given string to lowercase and returns it. Unlike string.lower(), this function supports non-Latin characters.

Source

pub fn parse_map_exchange_string( &self, map_exchange_string: &str, ) -> MapExchangeStringData

Convert a map exchange string to map gen settings and map settings.

Not available in settings and prototype stages.

Source

pub fn recv_udp(&self, for_player: Option<u32>)

Dispatch defines.events.on_udp_packet_received events for any new packets received by the specified player or the server.

This must be enabled per-instance with --enable-lua-udp.

UDP socket when enabled requests 256KB of receive buffer from the operating system. If there is more data than this between two subsequent calls of this method, data will be lost. That also applies to periods when the game is paused or is being saved as in those case the game update is not happening.

Note: lua event is not raised immediately as the UDP packet needs to be introduced into game state by means of input actions. Please keep incoming traffic as small as possible as in case of multiplayer game with many players, all this data will have to go through the multiplayer server and be distributed to all clients.

Not available in settings and prototype stages.

Source

pub fn remove_path(&self, path: &str)

Remove a file or directory in the script-output folder, located in the game’s user data directory. Can be used to remove files created by LuaHelpers::write_file.

Source

pub fn send_udp(&self, data: &str, for_player: Option<u32>, port: u16)

Send data to a UDP port on localhost for a specified player, if enabled.

This must be enabled per-instance with --enable-lua-udp.

Source

pub fn table_to_json(&self, data: LuaAny) -> &str

Convert a table to a JSON string

Source

pub fn write_file( &self, append: Option<bool>, data: &str, filename: &str, for_player: Option<u32>, )

Write a file to the script-output folder, located in the game’s user data directory. The name and file extension of the file can be specified via the filename parameter.

Trait Implementations§

Source§

impl Clone for LuaHelpers

Source§

fn clone(&self) -> LuaHelpers

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 LuaHelpers

Source§

impl Debug for LuaHelpers

Source§

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

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

impl Default for LuaHelpers

Source§

fn default() -> LuaHelpers

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

impl Eq for LuaHelpers

Source§

impl From<LuaHelpers> for LuaAny

Source§

fn from(_: LuaHelpers) -> Self

Converts to this type from the input type.
Source§

impl LuaObject for LuaHelpers

Source§

impl PartialEq for LuaHelpers

Source§

fn eq(&self, other: &LuaHelpers) -> 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 LuaHelpers

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.