Difference between revisions of "LuaAPI/wesnoth/units"
m (→wesnoth.units.create_animator) |
(→wesnoth.units.create_animator: Adding multiple animations for the same unit is not intended to be supported) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 81: | Line 81: | ||
=== wesnoth.units.to_map === | === wesnoth.units.to_map === | ||
− | * {{LuaGameOnly}} '''wesnoth.units.to_map'''(''unit'') | + | * {{LuaGameOnly}} '''wesnoth.units.to_map'''(''unit'' [,''fire_event'']) |
− | * {{LuaGameOnly}} '''wesnoth.units.to_map'''(''unit'', ''x'', ''y'') | + | * {{LuaGameOnly}} '''wesnoth.units.to_map'''(''unit'', ''x'', ''y'' [,''fire_event'']) |
− | * {{LuaGameOnly}} ''unit'':'''to_map'''([''x'', ''y'']) | + | * {{LuaGameOnly}} ''unit'':'''to_map'''([''x'', ''y'' [,''fire_event'']]) |
Places a unit on the map. This unit is described either by a WML table or by a proxy unit (to use the third form, it must be a proxy). Coordinates can be passed as additional arguments; otherwise the table is expected to have two fields '''x''' and '''y''', which indicate where the unit will be placed. | Places a unit on the map. This unit is described either by a WML table or by a proxy unit (to use the third form, it must be a proxy). Coordinates can be passed as additional arguments; otherwise the table is expected to have two fields '''x''' and '''y''', which indicate where the unit will be placed. | ||
Line 117: | Line 117: | ||
When the argument is a proxy unit, no duplicate is created. In particular, if the unit was private or on the map, it no longer is. Note: passing a WML table is just a shortcut for calling [[#wesnoth.units.create]] and then putting the resulting unit on a recall list. | When the argument is a proxy unit, no duplicate is created. In particular, if the unit was private or on the map, it no longer is. Note: passing a WML table is just a shortcut for calling [[#wesnoth.units.create]] and then putting the resulting unit on a recall list. | ||
+ | |||
+ | === wesnoth.units.rebuild === | ||
+ | |||
+ | {{DevFeature1.19|4}} | ||
+ | |||
+ | * {{LuaGameOnly}} '''wesnoth.units.rebuild'''(''unit'') | ||
+ | * {{LuaGameOnly}} ''unit'':'''rebuild'''() | ||
+ | |||
+ | Immediately rebuilds the unit from its unit type and modifications, discarding any alterations to non-transient attributes. Does not reset transient attributes such as the units current hitpoints, moves, or experience. | ||
=== wesnoth.units.transform === | === wesnoth.units.transform === | ||
Line 173: | Line 182: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | === wesnoth. | + | === wesnoth.units.resistance_against === |
* {{LuaGameOnly}} '''wesnoth.units.resistance_against'''(''unit'', ''damage_type'', [''as_attacker''], [''x'', ''y'']) → ''resistance value'' | * {{LuaGameOnly}} '''wesnoth.units.resistance_against'''(''unit'', ''damage_type'', [''as_attacker''], [''x'', ''y'']) → ''resistance value'' | ||
Line 271: | Line 280: | ||
* ''animator'':'''add(''unit'', ''flag'', ''hits'', ''params'')''' | * ''animator'':'''add(''unit'', ''flag'', ''hits'', ''params'')''' | ||
− | Adds a unit to the animation. The ''flag'' specifies which animation to play, and the ''hits'' parameter is required for attack animations to specify which variant of the animation to play. Possible keys in ''params'' are: | + | Adds a unit to the animation. The unit should be one that wasn't previously added. The ''flag'' specifies which [[AnimationWML#Common_animation_events_used_by_the_engine|animation]] to play, and the ''hits'' parameter is required for attack animations to specify which variant of the animation to play. Possible keys in ''params'' are: |
* '''facing''': A location. The animation will be played with the unit facing that location. | * '''facing''': A location. The animation will be played with the unit facing that location. |
Latest revision as of 19:04, 23 December 2024
Contents
- 1 Functions
- 1.1 wesnoth.units.advance
- 1.2 wesnoth.units.clone
- 1.3 wesnoth.units.erase
- 1.4 wesnoth.units.extract
- 1.5 wesnoth.units.matches
- 1.6 wesnoth.units.find_attack
- 1.7 wesnoth.units.to_map
- 1.8 wesnoth.units.to_recall
- 1.9 wesnoth.units.rebuild
- 1.10 wesnoth.units.transform
- 1.11 wesnoth.units.select
- 1.12 wesnoth.units.scroll_to
- 1.13 wesnoth.units.ability
- 1.14 wesnoth.units.chance_to_be_hit
- 1.15 wesnoth.units.defense_on
- 1.16 wesnoth.units.resistance_against
- 1.17 wesnoth.units.jamming_on
- 1.18 wesnoth.units.movement_on
- 1.19 wesnoth.units.vision_on
- 1.20 wesnoth.units.add_modification
- 1.21 wesnoth.units.remove_modifications
- 1.22 wesnoth.units.create_animator
- 1.23 wesnoth.units.create
- 1.24 wesnoth.units.find_on_map
- 1.25 wesnoth.units.find_on_recall
- 1.26 wesnoth.units.find
- 1.27 wesnoth.units.get
- 1.28 wesnoth.units.get_hovered
(Version 1.15.0 and later only)
The entire units module is only available in the game. It is not available to plugins or map generators.
Functions
wesnoth.units.advance
Advances the unit (and shows the advance unit dialog if needed) if the unit has enough XP. This function should be called after modifying the unit's experience directly. A similar function is called by Wesnoth internally after unit combat. The second argument is a boolean value that specifies whether the advancement should be animated. The third argument is a boolean value that specifies whether advancement related events should be fired.
This function only works for units on the map.
This function can also trigger multiple advancements if the unit has enough XP.
wesnoth.units.clone
Creates a private unit from another unit.
-- extract a unit from the map
local u = wesnoth.units.find_on_map{ type = "Thug" }[1]:clone()
wesnoth.units.erase(u.x, u.y) -- note: not the same as u:erase(), which would be an error
-- u is still valid at this point
wesnoth.units.erase
Erases a unit from the map. After calling this on a unit, the unit is no longer valid. Does not work on private units - this usage will raise an error.
wesnoth.units.extract
Removes a unit from the map or from a recall list and makes it private.
-- remove all the units from the recall list of side 1 and put them in a WML container
local list = {}
for i,u in ipairs(wesnoth.units.find_on_recall{ side = 1 }) do
u:extract()
table.insert(list, u.__cfg)
end
wml.array_access.set("player_recall_list", list)
Note: if the unit is on the map, this is just a shortcut for calling #wesnoth.units.clone and then #wesnoth.units.erase. It is, however, the only way to remove a unit from a recall list without putting it on the map.
wesnoth.units.matches
- ♟ wesnoth.units.matches(unit, filter, [other_unit]) → matched?
- ♟ wesnoth.units.matches(unit, filter, [location]) → matched?
- ♟ unit:matches(filter, [other_unit]) → matched?
- ♟ unit:matches(filter, [location]) → matched?
Returns true if the given unit matches the WML filter passed as the second argument. If other_unit is specified, it is used for the $other_unit auto-stored variable in the filter. Otherwise, this variable is not stored for the filter. If an extra location is specified, the filter matches as if the unit were at that location.
assert(unit.canrecruit == wesnoth.units.matches(unit, { canrecruit = true }))
wesnoth.units.find_attack
Returns the first attack that matches the given StandardWeaponFilter.
wesnoth.units.to_map
- ♟ wesnoth.units.to_map(unit [,fire_event])
- ♟ wesnoth.units.to_map(unit, x, y [,fire_event])
- ♟ unit:to_map([x, y [,fire_event]])
Places a unit on the map. This unit is described either by a WML table or by a proxy unit (to use the third form, it must be a proxy). Coordinates can be passed as additional arguments; otherwise the table is expected to have two fields x and y, which indicate where the unit will be placed.
-- create a unit with random traits, then erase it
wesnoth.units.to_map({ type = "Elvish Lady" }, 17, 42)
wesnoth.units.erase(17, 42)
When the argument is a proxy unit, no duplicate is created. In particular, if the unit was private or on a recall list, it no longer is; and if the unit was on the map, it has been moved to the new location. Note: passing a WML table is just a shortcut for calling #wesnoth.units.create and then putting the resulting unit on the map.
-- move the leader back to the top-left corner
wesnoth.units.find_on_map({ canrecruit = true })[1]:to_map(1, 1)
If x,y is a village, this function does not capture it (as of 1.14). Use wesnoth.set_village_owner(x, y, unit.side) if that's what you want.
Caution: Using this function will trigger unit placed events. While this may be correct in most cases, there are some cases where it shouldn't, especially if the code is inside the definition of a custom WML tag — it would be an unexpected side-effect from the users point. To work around this, false can be passed additionally as the last argument. This is what is used internally for some tags such as [move_unit], [harm_unit], [unpetrify] or the feeding ability and is not officially part of the API.
wesnoth.units.to_recall
Places a unit on a recall list. This unit is described either by a WML table or by a proxy unit (to use the third form, it must be a proxy). The side of the recall list is given by the second argument if present, or by the side of the unit otherwise.
-- put the unit at location 17,42 on the recall list for side 2
wesnoth.units.find_on_map{ x= 17, y = 42 })[1]:to_recall(2)
When the argument is a proxy unit, no duplicate is created. In particular, if the unit was private or on the map, it no longer is. Note: passing a WML table is just a shortcut for calling #wesnoth.units.create and then putting the resulting unit on a recall list.
wesnoth.units.rebuild
(Version 1.19.4 and later only)
Immediately rebuilds the unit from its unit type and modifications, discarding any alterations to non-transient attributes. Does not reset transient attributes such as the units current hitpoints, moves, or experience.
wesnoth.units.transform
Changes the type of a unit and adjust attributes accordingly. Note that hit points are only changed if necessary to accommodate the new maximum hit points. Poison is automatically removed if the transformed unit is immune.
local ev = wesnoth.current.event_context
local u = wesnoth.units.find_on_map{x=ev.x1, y=ev.y1}[1]
u:transform("Spearman")
-- If a full heal is desired:
u.hitpoints = u.max_hitpoints
u.status.poisoned = false
wesnoth.units.select
Alias of LuaAPI/wesnoth/interface#wesnoth.interface.select_unit.
wesnoth.units.scroll_to
Alias of LuaAPI/wesnoth/interface#wesnoth.interface.scroll_to_hex.
wesnoth.units.ability
Returns true if the unit is currently affected by an ability with this given tag name. This means that the ability could be owned by the unit itself, or by an adjacent unit.
function has_teleport(u)
return u:ability("teleport")
end
wesnoth.units.chance_to_be_hit
- ♟ wesnoth.units.chance_to_be_hit(unit, terrain_code) → hit chance
- ♟ unit:chance_to_be_hit(terrain_code) → hit chance
Returns the chance that a unit will be hit on a particular terrain (based on its defence).
wesnoth.units.defense_on
- ♟ wesnoth.units.defense_on(unit, terrain_code) → defense value
- ♟ unit:defense_on(terrain_code) → defense value
Returns the defense of a unit on a particular terrain. (Note: it is the actual defense. So the lower it is, the weaker the unit is.)
local flat_defense = u:defense_on("Gt")
wesnoth.units.resistance_against
- ♟ wesnoth.units.resistance_against(unit, damage_type, [as_attacker], [x, y]) → resistance value
- ♟ unit:resistance_against(damage_type, [as_attacker], [x, y]) → resistance value
Returns the resistance of a unit against an attack type. (Note: it is the actual resistance. So the lower it is, the weaker the unit is.) The third argument indicates whether the unit is the attacker. Last arguments are the coordinates of an optional map location (for the purpose of taking abilities into account).
local fire_resistance = u:resistance_against("fire")
wesnoth.units.jamming_on
Returns the jamming cost of a unit on a particular terrain.
local jam_cost = u:jamming_on("Gt")
wesnoth.units.movement_on
Returns the movement cost of a unit on a particular terrain.
local move_cost = u:movement_on("Gt")
wesnoth.units.vision_on
Returns the vision cost of a unit on a particular terrain.
local see_cost = u:vision_on("Gt")
wesnoth.units.add_modification
- ♟ wesnoth.units.add_modification(unit, type, effects, [write_to_mods])
- ♟ unit:add_modification(type, effects, [write_to_mods])
Modifies a given unit. It needs to be a proxy unit. The second argument is the type of the modification (one of "trait", "object", or "advancement"). The option "advancement" applies effects as if the unit would advance (e.g. AMLA effects). The third argument is a WML table describing the effect, so mostly containing [effect] children. See EffectWML for details about effects. If write_to_mods false, causes it to not write the modification tag to the unit's [modifications] (as would be done with an [object] with no_write=true).
local T = wml.tag
local u = wesnoth.units.find_on_map{ canrecruit = true }[1]
local effects = {
id = "my_effect_id",
T.effect {
apply_to = "image_mod",
replace = "RC(red>blue)"
},
T.effect {
apply_to = "new_animation",
T.standing_animation {
-- AnimationWML
}
}
}
u:add_modification("object", effects)
wesnoth.units.remove_modifications
Modifies a given unit. The unit needs to be a proxy unit. The second argument is a filter for the modifications to remove. It takes the same syntax as [filter_wml]; all matching modifications will be removed. The third argument is the type (tag name) of the modifications to search for; it defaults to "object", but you can also pass "trait" or "advancement".
local u = wesnoth.units.find_on_map{ canrecruit = true }[1]
u:remove_modifications({ id = "my_effect_id" })
wesnoth.units.create_animator
- ♟ wesnoth.units.create_animator()
Returns an object that can be used to set up and run an animation. The object has three methods:
- animator:run()
Runs the animation. Implicitly clears the animator.
- animator:clear()
Clears any units previously added to the animation.
- animator:add(unit, flag, hits, params)
Adds a unit to the animation. The unit should be one that wasn't previously added. The flag specifies which animation to play, and the hits parameter is required for attack animations to specify which variant of the animation to play. Possible keys in params are:
- facing: A location. The animation will be played with the unit facing that location.
facing is currently not implemented/working. See https://github.com/wesnoth/wesnoth/issues/9032
- value: Either a number or a list of two numbers. Use this to pass value and/or value_second to default animations that use them.
- with_bars: Whether to show HP bars and such while the animation plays.
- text: Text to float as the animation plays.
- color: Color of the floating text - a list of red, green, blue.
- primary: The primary weapon to use for the animation. Must be a Lua unit attack proxy.
- secondary: The secondary weapon to use for the animation.
Normal usage is to create it, call add one or more times, then call run.
wesnoth.units.create
- ♟ wesnoth.units.create(unit_info) → unit
Creates a private unit from a WML table.
local u = wesnoth.units.create{ type = "White Mage", gender = "female" }
wesnoth.units.find_on_map
- ♟ wesnoth.units.find_on_map(filter) → array of units
- ♟ wesnoth.units.find_on_map(filter, fake_location) → array of units
- ♟ wesnoth.units.find_on_map(filter, other_unit) → array of units
Returns an array of all the units on the map matching the WML filter passed as the first argument. See StandardUnitFilter for details about filters. If a second unit is passed, it can be referenced via the $other_unit variable in the main filter as well as via the "other" variable in WFL formulas used in the main filter. If a location is passed, the filter is run as if the unit were at that location (rather than its real location). This affects things such as [filter_adjacent] and ability_active, and should work even for a unit on the recall list.
local leaders_on_side_two = wesnoth.units.find_on_map{ side = 2, canrecruit = true }
local name_of_leader = leaders_on_side_two[1].name
wesnoth.units.find_on_recall
- ♟ wesnoth.units.find_on_recall(filter) → array of units
Returns an array of all the units on the recall lists matching the WML filter passed as the first argument.
wesnoth.units.find
- ♟ wesnoth.units.find(filter) → array of units
Returns a list of all units matching the WML filter, both those on the map and those on the recall lists. It is a shorthand for calling both find_on_map and find_on_recall and combining the resulting arrays.
wesnoth.units.get
Returns the unit at the given location or with the given ID.
local args = ...
local unit = wesnoth.units.get(args.x1, args.y1)
wesnoth.units.get_hovered
Alias of LuaAPI/wesnoth/interface#wesnoth.interface.get_displayed_unit.