Xalzar's WML Headaches
Moderator: Forum Moderators
Forum rules
- Please use [code] BBCode tags in your posts for embedding WML snippets.
- To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Re: Xalzar's WML Headaches
I believe it is possible to use it with
And then in your [if] or [filter_condition] have [is_ai][/is_ai]
But not able to test now, going to sleep.
Code: Select all
[event]
first_time_only=no
name=preload
[lua]
code=<<
function wesnoth.wml_conditionals.is_ai()
return wesnoth.synchronize_choice(
function()
return false
end,
function()
return true
end)
end
>>
[/lua]
[/event]
But not able to test now, going to sleep.
Re: Xalzar's WML Headaches
Those functions need to return tables, so
The version below sets a variable that's accessible to WML instead of adding an [is_ai] tag. Haven't tested in multiplayer yet, but I'll also post the code and find out in the morning if it worked
return { value = false }
. Thanks for posting the pointers, I need the same logic in my code and was wondering how to do it.The version below sets a variable that's accessible to WML instead of adding an [is_ai] tag. Haven't tested in multiplayer yet, but I'll also post the code and find out in the morning if it worked
Code: Select all
[event]
name=side turn
first_time_only=no
{VARIABLE additional_upkeep 0}
# omitted - code counting the number of units that the Non-Fatal Wounds mod has saved
[lua]
# wesnoth.synchronize_choice itself takes two functions as arguments, and chooses which one to run based on whether it's an AI player
code=<<
wesnoth.set_variable ("is_ai",
wesnoth.synchronize_choice(
function()
return { value = false }
end,
function()
return { value = true }
end
)
)
>>
[/lua]
[message]
speaker=narrator
message=_"Upkeep for wounded units on side $side_number is $additional_upkeep (and the AI flag is true)"
[show_if]
[variable]
name=is_ai[0].value
boolean_equals=true
[/variable]
[/show_if]
[/message]
[message]
speaker=narrator
message=_"Upkeep for wounded units on side $side_number is $additional_upkeep (and the AI flag is false)"
[show_if]
[variable]
name=is_ai[0].value
boolean_equals=false
[/variable]
[/show_if]
[/message]
[/event]
Re: Xalzar's WML Headaches
Sadly both codes gave me errors (bad argument #1 to 'set_variable_local' (empty variable name)).
Posting part of my code so you can tell me if it's because of a mistake on my part.
@Ravana's
@Octalot's:
Before you ask, yes, I need to split the custom event from the first, because I need it to fire it as an action too and not only on recruit.
Posting part of my code so you can tell me if it's because of a mistake on my part.
@Ravana's
Code: Select all
[event]
name=recruit
id=modrecruit
first_time_only=no
[lua]
code=<<
function wesnoth.wml_conditionals.is_ai()
return wesnoth.synchronize_choice(
function()
return false
end,
function()
return true
end)
end
>>
[/lua]
[filter_condition]
[is_ai]
[/is_ai]
[/filter_condition]
[store_gold]
side=$unit.side
variable=budget
[/store_gold]
[gold]
amount=$unit.cost
side=$unit.side
[/gold]
[set_variable]
name=recruitcost
value=$unit.cost
[/set_variable]
[fire_event]
id=40k_modweapons
[/fire_event]
[/event]
[event]
name=unit_recruited
id=40k_modweapons
first_time_only=no
[message]
id=$unit.id
image="$unit.image"
speaker=narrator
message= _ "REDACTED"
[option]
...
[/option]
[/message]
[/event]
Code: Select all
[event]
name=recruit
id=modrecruit
first_time_only=no
[lua]
# wesnoth.synchronize_choice itself takes two functions as arguments, and chooses which one to run based on whether it's an AI player
code=<<
wesnoth.set_variable ("is_ai",
wesnoth.synchronize_choice(
function()
return { value = false }
end,
function()
return { value = true }
end
)
)
>>
[/lua]
[store_gold]
side=$unit.side
variable=budget
[/store_gold]
[gold]
amount=$unit.cost
side=$unit.side
[/gold]
[set_variable]
name=recruitcost
value=$unit.cost
[/set_variable]
[fire_event]
id=40k_modweapons
[/fire_event]
[/event]
[event]
name=unit_recruited
id=40k_modweapons
first_time_only=no
[message]
id=$unit.id
image="$unit.image"
speaker=narrator
message= _ "REDACTED"
[show_if]
[variable]
name=is_ai[0].value
boolean_equals=false
[/variable]
[/show_if]
[option]
...
[/option]
[/message]
[/event]
Re: Xalzar's WML Headaches
Preload event is important.
If said it needs table, returns would be
function wesnoth.wml_conditionals.is_ai()
return wesnoth.synchronize_choice(
function()
return {value=false}
end,
function()
return {value=true}
end).value
end
If said it needs table, returns would be
function wesnoth.wml_conditionals.is_ai()
return wesnoth.synchronize_choice(
function()
return {value=false}
end,
function()
return {value=true}
end).value
end
Re: Xalzar's WML Headaches
Aaah ok, I misunderstood. I'll try it.
Also, I'm sorry but please you both disregard for the moment my previous post. I'm a bit sleepy and I've probably introduced an error somewhere else (I've got an idea of where), so I'll revise your code tomorrow with a clear mind.
Re: Xalzar's WML Headaches
I tried it out in debug, it only works for sides that start out as ai - and 1.14 does not allow undroiding such sides. If you droid side, it is still treated as human.
Code used for testing (:debug, then lua console)
Code used for testing (:debug, then lua console)
Code: Select all
function wesnoth.wml_conditionals.is_ai()
return wesnoth.synchronize_choice(
function()
return {value=false}
end,
function()
return {value=true}
end).value
end
wesnoth.wml_actions.event{
name="side turn",
first_time_only=false,
{"lua", {code="wesnoth.message(wesnoth.wml_conditionals.is_ai())"}}
}
Re: Xalzar's WML Headaches
Thanks, it seems I have encountered an unexpected roadblock.
I'll try to think of some workaround, for now the only thing I can think of is trying to add wait_description to the message, but even if it works it's not elegant.
And even then, the AI needs to have events reserved for it, for setting the recruitment criteria - so the problem is still there.
Maybe I should have kept insisting with AMLAs instead of switching to [message] after all...
(Or I should write a disclaimer: do not droid sides, use AIs only if set up in pre-game?)
Edit: tried you code, Ravana, it does not work for me. I've inserted it in a preload event (inside a unit, does this change anything?), gives error "macro/file 'value=false' is missing".
I cannot understand anything about this kind of code, and I don't want you to do my work for me, so maybe I need to take a little pause and think about how to proceed.
Also, I've noticed you added this discussion to the SideWML page, lol| :lol:Let's hope something good will come out off of this topic but I'm afraid I cannot be of much help to create a functioning Lua code.
I'll try to think of some workaround, for now the only thing I can think of is trying to add wait_description to the message, but even if it works it's not elegant.
And even then, the AI needs to have events reserved for it, for setting the recruitment criteria - so the problem is still there.
Maybe I should have kept insisting with AMLAs instead of switching to [message] after all...
(Or I should write a disclaimer: do not droid sides, use AIs only if set up in pre-game?)
Edit: tried you code, Ravana, it does not work for me. I've inserted it in a preload event (inside a unit, does this change anything?), gives error "macro/file 'value=false' is missing".
I cannot understand anything about this kind of code, and I don't want you to do my work for me, so maybe I need to take a little pause and think about how to proceed.
Also, I've noticed you added this discussion to the SideWML page, lol| :lol:Let's hope something good will come out off of this topic but I'm afraid I cannot be of much help to create a functioning Lua code.
Re: Xalzar's WML Headaches
That error means the WML preprocessor is trying to process the Lua code. If you're putting Lua in a WML file, put
<<
at the start and >>
at the end to fix it.That was me I knew I'd read somewhere on the Wiki that it didn't work in multiplayer, but I couldn't find it again until you added the link to StandardSideFilter.
Re: Xalzar's WML Headaches
Of course! Please do not take anything for granted, I don't know anything about Lua. Now the error says something like (translated from my language, the exact wording may change in other versions): "unexpected characters at the beginning of the line", referring undoubtedly to the aforementioned << >>.
Ah!
Edit: I've found some answers in the LuaWML page, I needed to add "code=" before all the lua code. See? I'm a complete n00b. Now at least it does not produce errors while loading.
Edit2: After adding the [show_if] in the recruit event, an error in-game was saying that [is_ai] is not valid (I had my doubts already since it was coming out of nowhere, WML-side speaking). Therefore I semi-improvised and I wrapped it in a [lua] tag: no error! But no event even for the human side. Tried wrapping [lua] in a [not], now both human and AI sides trigger the event. We returned to the start point.
Re: Xalzar's WML Headaches
wesnoth.wml_conditionals is special - if you add functions to it, they become available to wml. So wesnoth.wml_conditionals.is_ai() defines condition tag [is_ai].
Preload event fires each time game is started, including reloaded. It is used to set up lua variables and functions, since unlike wml variables they are not part of save file. So defining function wesnoth.wml_conditionals.is_ai() in preload event means it is always available.
Preload event fires each time game is started, including reloaded. It is used to set up lua variables and functions, since unlike wml variables they are not part of save file. So defining function wesnoth.wml_conditionals.is_ai() in preload event means it is always available.
Re: Xalzar's WML Headaches
We're mixing multiple languages together, and using insufficiently-documented features - I certainly wouldn't call you a n00b.
(Ravana has already posted the rest of the message that I was going to post.)
(Ravana has already posted the rest of the message that I was going to post.)
Re: Xalzar's WML Headaches
Unit events are only added when unit is placed on map, and preload happens before map is loaded, so yes, it should be scenario/era/modification event.
Re: Xalzar's WML Headaches
Since I'm no expert in those, I've tried to avoid [modification]s, but now it's the time to use them. In the end, they're simpler than I thought.
This new code:
Code: Select all
[modification]
id=recruit_mod
name=_"Recruit Modifications Mod"
description= _ "All units can be modified during recruitment."
require_modification=no
allow_era=REDACTED
[event]
first_time_only=no
name=preload
[lua]
code= <<
function wesnoth.wml_conditionals.is_ai()
return wesnoth.synchronize_choice(
function()
return {value=false}
end,
function()
return {value=true}
end).value
end
wesnoth.wml_actions.event{
name="side turn",
first_time_only=false,
{"lua", {code="wesnoth.message(wesnoth.wml_conditionals.is_ai())"}}
}
>>
[/lua]
[/event]
[event]
name=recruit
id=xrecruit
first_time_only=no
[filter_condition]
[not]
[lua]
[is_ai]
[/is_ai]
[/lua]
[/not]
[/filter_condition]
[store_gold]
side=$unit.side
variable=budget
[/store_gold]
[gold]
amount=$unit.cost
side=$unit.side
[/gold]
[set_variable]
name=recruitcost
value=$unit.cost
[/set_variable]
[fire_event]
id=modweapons
[/fire_event]
[/event]
[event]
name=unit_recruited
id=modweapons
first_time_only=no
[message]
id=$unit.id
image="$unit.image"
speaker=narrator
message= _ "Unit Modifications: Weapons"
#[show_if]
#[/show_if]
[option]
...
[/option]
[/message]
[/event]
[/modification]
Same thing! This [is_ai] seems to not discriminate between human and ai sides (yes, I've set one side as ai during the pre-match phase, did not use "droid" command).
Maybe I should try to add that condition inside those #ed [show_if]s too?
Edit: what about this? I know that is included in the code we are testing already, but it seems to me that in that example it is used a bit differently... Could taking from that help me?
Otherwise, I'm coming up with a workaround, surprisingly WML-based! It should be simple albeit of course a bit unelegant.
Edit2: new version of the code! Now with Lua messages in-game ("No" and "Yes" which means it works a bit!), and the same final results as before!
Last edited by Xalzar on June 13th, 2019, 2:22 pm, edited 1 time in total.
- Pentarctagon
- Project Manager
- Posts: 5565
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: Xalzar's WML Headaches
Does the
code
attribute work placed directly in [event] like that?99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
take one down, patch it around
-2,147,483,648 little bugs in the code