Difference between revisions of "LuaWML/Misc"

From The Battle for Wesnoth Wiki
(Added synchronization command)
(document helper.rand)
Line 59: Line 59:
  
 
  helper.move_unit_fake({ id="Delfador" }, 14, 8)
 
  helper.move_unit_fake({ id="Delfador" }, 14, 8)
 +
 +
==== helper.rand ====
 +
{{DevFeature1.9}}
 +
 +
(A shortcut to set_variable's rand= since math.rand is an OOS magnet and therefore disabled.) Pass a string like you would to set_variable's rand=.
 +
 +
create a random unit at (1, 1) on side=1 :
 +
wesnoth.put_unit(1, 1, { type = helper.rand("Dwarvish Fighter,Dwarvish Thunderer,Dwarvish Scout") })

Revision as of 20:10, 23 October 2010

This page describes miscellaneous LuaWML objects and helpers.

wesnoth.game_config

Contrarily to the other values of the wesnoth table, game_config is simply a proxy table. Its fields offer an interface to the global settings of Wesnoth:

  • version: string (read only)
  • base_income: integer (read/write)
  • village_income: integer (read/write)
  • poison_amount: integer (read/write)
  • rest_heal_amount: integer (read/write)
  • recall_cost: integer (read/write)
  • kill_experience: integer (read/write)
  • debug: boolean (read only)
-- Healing, poison, and regeneration, are a bit weak? Let's boost them!
wesnoth.game_config.poison_amount = 15

wesnoth.current

As with game_config, current is a proxy table. Its fields are getter for game-related properties:

  • side: integer (read only)
  • turn: integer (read only)
  • event_context: WML table with attributes name, x1, y1, x2, y2, and children weapon, second_weapon, describing the trigger for the current event.
wesnoth.message(string.format("Turn %d, side %d is playing.", wesnoth.current.turn, wesnoth.current.side))

wesnoth.synchronize_choice

Template:DevFeature1.9

Recovers a WML table that was computed on one client only or was stored in a replay. The actual computation is performed by the function passed as the first argument, assuming that the client is the side currently playing. For all the other clients, the function will not be called.

local result = wesnoth.synchronize_choice(function()
    -- called only on the client handling the current side
    local choice = 0
    wesnoth.show_dialog(some_dialog_cfg, nil, function() choice = wesnoth.get_dialog_value "some_list" end)
    return { value = choice }
end)
wesnoth.message(string.format("Selected item: %d", result.value))

helper.set_wml_tag_metatable

Sets the metable of a table so that it can be used to create subtags with less brackets. Returns the table. The fields of the table are simple wrappers around table constructors.

T = helper.set_wml_tag_metatable {}
W.event { name = "new turn", T.message { speaker = "narrator", message = "?" } }

helper.modify_unit

Modifies all the units satisfying the given filter (argument 1) with some WML attributes/objects (argument 2). This is a Lua implementation of the MODIFY_UNIT macro.

helper.modify_unit({ id="Delfador" }, { moves=0 })

helper.move_unit_fake

Fakes the move of a unit satisfying the given filter (argument 1) to the given position (argument 2). This is a Lua implementation of the MOVE_UNIT macro.

helper.move_unit_fake({ id="Delfador" }, 14, 8)

helper.rand

Template:DevFeature1.9

(A shortcut to set_variable's rand= since math.rand is an OOS magnet and therefore disabled.) Pass a string like you would to set_variable's rand=.

create a random unit at (1, 1) on side=1 :

wesnoth.put_unit(1, 1, { type = helper.rand("Dwarvish Fighter,Dwarvish Thunderer,Dwarvish Scout") })