Lua location syntax
Moderator: Forum Moderators
- ZombieKnight
- Posts: 371
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Lua location syntax
Hi
how to set facing in animation:add?
https://wiki.wesnoth.org/LuaAPI/wesnoth ... e_animator
(I'd like unit to face x,y)
how to set facing in animation:add?
https://wiki.wesnoth.org/LuaAPI/wesnoth ... e_animator
(I'd like unit to face x,y)
Code: Select all
... , facing = ???
Last edited by ZombieKnight on May 31st, 2024, 3:30 pm, edited 2 times in total.
-
- Posts: 1456
- Joined: August 26th, 2018, 11:46 pm
- Location: A country place, far outside the Wire
Re: Lua location syntax
Is this not enough?
Code: Select all
animator:add(unit, flag, hits, { facing = {x = 3, y = 12} })
Speak softly, and carry Doombringer.
- ZombieKnight
- Posts: 371
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Lua location syntax
Thanks ^^
I couldn't find the right syntax.
I couldn't find the right syntax.
- ZombieKnight
- Posts: 371
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Lua location syntax
No, this code doesn't work (playies animation, but doesn't care what is in facing)white_haired_uncle wrote: ↑May 31st, 2024, 10:34 am Is this not enough?
Code: Select all
animator:add(unit, flag, hits, { facing = {x = 3, y = 12} })
Code: Select all
animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]], facing = {x = 18, y = 28} })
- Celtic_Minstrel
- Developer
- Posts: 2371
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Lua location syntax
Try the name of a direction, for example
facing = 'nw'
.- ZombieKnight
- Posts: 371
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Lua location syntax
It doesn't work for meCeltic_Minstrel wrote: ↑May 31st, 2024, 11:20 pm Try the name of a direction, for examplefacing = 'nw'
.

Code: Select all
animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]],facing = 'nw'})
(this code is tuned from :lua wesnoth.require("wml-utils").handle_event_commands(wml.tovconfig(wml.load("~add-ons/Bandits_from_Brown_Hills/test_WML_event.cfg"))))
Code: Select all
[store_unit]
[filter]
id="BfBH Fugitive-79"
[/filter]
variable = exploitation_units
[/store_unit]
[store_unit]
[filter]
id="BfBH Dwarvish Berserker-81"
[/filter]
variable = unit
[/store_unit]
[lua]
code=<<
local attacker= wesnoth.units.get(wml.variables["exploitation_units[0].id"])
local defender= wesnoth.units.get(wml.variables["unit[0].id"])
local defender_t= wml.variables["unit[0]"]
local attacking_weapon_index = 0
local i = 0
while wml.variables["exploitation_units[0].attack["..i.."]"] ~= nil do
local weapon = wml.find_child(wml.variables["exploitation_units[0].attack["..i.."]"],"specials",{{"dummy",{id="exploitation"}}})
if weapon ~= nil then
attacking_weapon_index = i + 1
break
end
i = i + 1
end
defender.attacks[(#defender.attacks + 1)] = {}
local _, _, attacker_stats, defender_stats =wesnoth.simulate_combat(attacker,attacking_weapon_index, defender, #defender.attacks)
defender.attacks[#defender.attacks] = nil
for key, value in pairs(attacker_stats) do
wml.variables["attacker_stats."..key] = tonumber(value) or tostring(value)
end
for key, value in pairs(defender_stats) do
wml.variables["defender_stats."..key] = tonumber(value) or tostring(value)
end
>>
[/lua]
[lua] #TODO make lua animation
code=<<
--making vars , v for shorter code
local v=wml.variables
local animation = wesnoth.units.create_animator()
local attacker= wesnoth.units.get(v["exploitation_units[0].id"])
local defender= wesnoth.units.get(v["unit[0].id"])
local attacking_weapon_index
animation:add(defender, "defend", "miss",{with_bars = false, primary = defender.attacks[v["defender_stats.name"]], secondary = defender.attacks[v["defender_stats.name"]]})
animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]],facing = 'nw'})
animation:run()
animation:clear()
>>
[/lua]
-
- Posts: 1456
- Joined: August 26th, 2018, 11:46 pm
- Location: A country place, far outside the Wire
Re: Lua location syntax
Try "target" instead of "facing", and probably use { x = X, y = Y }.
Speak softly, and carry Doombringer.
- ZombieKnight
- Posts: 371
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Lua location syntax
Thanks, I'll try tommorrow!
(sry for delay, internet wasn't really working today)
(sry for delay, internet wasn't really working today)
- ZombieKnight
- Posts: 371
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Lua location syntax
Like thiswhite_haired_uncle wrote: ↑June 1st, 2024, 4:31 pm Try "target" instead of "facing", and probably use { x = X, y = Y }.
target = {x=19,y=28}
?Doesn't work
- ZombieKnight
- Posts: 371
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Lua location syntax
Got it!!!
Got to dig inside WML tag implementation to find it.
Now it's clear animation:add() DOESN'T take facing as a parameter, but target.
The whole doc of the animation:add should be added (where to contact someone who can edit docs?)
My Working code:
It's taken from lua wml.actions.animate_unit.
Got to dig inside WML tag implementation to find it.
Now it's clear animation:add() DOESN'T take facing as a parameter, but target.
The whole doc of the animation:add should be added (where to contact someone who can edit docs?)
My Working code:
Code: Select all
local facing = {x = 17, y = 30}
local unit = attacker
local facing_loc = wesnoth.map.find(facing)[1]
local dir = wesnoth.map.get_relative_dir(unit.x, unit.y, facing_loc[1], facing_loc[2])
unit.facing = dir
facing = wesnoth.map.get_direction(unit.x, unit.y, dir)
animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]], target = facing})
-
- Posts: 1456
- Joined: August 26th, 2018, 11:46 pm
- Location: A country place, far outside the Wire
Re: Lua location syntax
Did/could you try this:
Code: Select all
animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]], target = wesnoth.map.find{x = 17, y = 30}[1])
Speak softly, and carry Doombringer.
- ZombieKnight
- Posts: 371
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Lua location syntax
Doesn't workwhite_haired_uncle wrote: ↑June 2nd, 2024, 11:21 am Did/could you try this:
Code: Select all
animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]], target = wesnoth.map.find{x = 17, y = 30}[1])
-
- Posts: 1456
- Joined: August 26th, 2018, 11:46 pm
- Location: A country place, far outside the Wire
Re: Lua location syntax
Hmm. I think we can tell that the wiki is wrong and it should be "target", not "facing" from src/scripting/game_lua_kernel.cpp. And it appears target= needs a location, but I can't find a solid definition of location.
Based on the output of [store_locations], it appears a location is just { x=X, y=Y, terrain=TERRAIN}, though some comments seem to imply that just x,y is enough. Perhaps a terrain is necessary?
Could you try this, maybe substituting in the correct terrain?
Based on the output of [store_locations], it appears a location is just { x=X, y=Y, terrain=TERRAIN}, though some comments seem to imply that just x,y is enough. Perhaps a terrain is necessary?
Could you try this, maybe substituting in the correct terrain?
animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]], target = { x = 17, y = 30, terrain = "Gg"})
Speak softly, and carry Doombringer.
- Celtic_Minstrel
- Developer
- Posts: 2371
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Lua location syntax
The documentation is here for the record.
A location is either a two-element array, eg
It should accept "facing" which is a direction, egwhite_haired_uncle wrote: ↑June 2nd, 2024, 12:03 pm Hmm. I think we can tell that the wiki is wrong and it should be "target", not "facing" from src/scripting/game_lua_kernel.cpp. And it appears target= needs a location, but I can't find a solid definition of location.
'nw'
. Thus this seems like it might be a bug. It's fine for it to accept "target" as an alternative though, taking a location. From the sounds of it, only the latter is currently supported?A location is either a two-element array, eg
{3, 5}
, or anything that contains x
and y
tags, eg {x = 3, y = 5}
. It does not require terrain
, and usually does not have that. There's no need to use wesnoth.map.get
or wesnoth.map.find
to get a hex reference.- ZombieKnight
- Posts: 371
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Lua location syntax
Yes.Celtic_Minstrel wrote: ↑June 3rd, 2024, 3:26 am The documentation is here for the record.
It should accept "facing" which is a direction, egwhite_haired_uncle wrote: ↑June 2nd, 2024, 12:03 pm Hmm. I think we can tell that the wiki is wrong and it should be "target", not "facing" from src/scripting/game_lua_kernel.cpp. And it appears target= needs a location, but I can't find a solid definition of location.'nw'
. Thus this seems like it might be a bug. It's fine for it to accept "target" as an alternative though, taking a location. From the sounds of it, only the latter is currently supported?
If you look-up for the code I've posted, you discover, you aren't right (that code is from animate_unit implementation).
A location is either a two-element array, eg{3, 5}
, or anything that containsx
andy
tags, eg{x = 3, y = 5}
. It does not requireterrain
, and usually does not have that. There's no need to usewesnoth.map.get
orwesnoth.map.find
to get a hex reference.
(Or it just doesn't work in animator)