Follow-up Comment #7, bug #23345 (project wesnoth):
Well, I learned something today.
Apparently, putting [lua] inside [scenario] adds whatever code there is to the
collection of preload scripts. I knew about putting lua at top level (outside
of scenario) and inside events, but I did not know this one. The behavior is
not described on the wiki, and in my opinion is somewhat broken.
The wiki does not describe [lua] inside of [scenario], except in the LuaWML
page. It has this to say:
It is also possible to put this tag inside a [scenario] ScenarioWML, those
tags will be executed immideately when the lua engine loads which is even
before the scenario preload event is fired.
Note that it does *not* say that the lua will be replayed when the game is
reloaded, you would only learn that by testing.
At time of writing, the code that does this, in 1.12 and master, looks like
this:
// Execute the preload scripts.
cmd_log_ << "Running preload scripts...\n";
game_config::load_config(game_lua_kernel::preload_config);
BOOST_FOREACH(const config &cfg, game_lua_kernel::preload_scripts) {
run(cfg["code"].str().c_str());
}
BOOST_FOREACH(const config &cfg, level_.child_range("lua")) {
run(cfg["code"].str().c_str());
}
Note that in general, when an event is inside of scenario, it does not get
reregistered automatically when you reload. Instead it is only registered
once. For instance, if I have a scenario like:
[multiplayer]
id= "01_STARTUNIT_TEST"
name="Crash Test"
map_data = "Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, 2 Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, 1 Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
"
{DEFAULT_SCHEDULE}
[side]
side=1
id= "leader1"
controller=human
team_name= "team1"
type = "Elvish Archer"
canrecruit="yes"
[/side]
[side]
side=2
id= "leader2"
controller=human
team_name= "team2"
type = "Elvish Archer"
canrecruit="yes"
[/side]
[event]
name = start
{VARIABLE_OP FOO add 1}
[/event]
[/multiplayer]
If I run this scenario directly, I will get FOO=1 in inspect dialog.
If I copy pasted that event so that there are two of them, I will get FOO=2.
If I only have one event, but I save and reload, I will still only get FOO=1.
However, as I tested, with a scenario like this,
[multiplayer]
id= "01_STARTUNIT_TEST"
name="Crash Test"
map_data = "Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, 2 Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, 1 Gs^Vh, Gs^Vh, Gs^Vh
Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh, Gs^Vh
"
{DEFAULT_SCHEDULE}
[side]
side=1
id= "leader1"
controller=human
team_name= "team1"
type = "Elvish Archer"
canrecruit="yes"
[/side]
[side]
side=2
id= "leader2"
controller=human
team_name= "team2"
type = "Elvish Archer"
canrecruit="yes"
[/side]
[lua]
code = << wesnoth.set_variable("foo", (wesnoth.get_variable("foo") or
0)+1) >>
[/lua]
[/multiplayer]
I observe "foo=2" in the inspect dialog after reloading.
So that's pretty unexpected, it means that [lua] behaves fundamentally
differently from [event] inside of [scenario], and it doesn't seem to be
documented anywhere. :/
----
Anyways I don't see why we should get rid of preload. _We don't in general
make compatibility breaking changes just because something is "unnecessary"._
That would force everyone to rewrite every scenario that used lua and used
preload events, which is most of them, for no benefit.
The only configurations I would consider acceptable are (1) status quo (2)
make all lua state persistent, and then deprecate preload events, deprecate
lua inside scenario, leaving only [lua] inside of [event] and [lua] at top
level. (2) would only be if we decide that it will make everything so much
simpler that the pain is worth it.
_______________________________________________________
Reply to this item at:
<http://gna.org/bugs/?23345>
_______________________________________________
Message sent via/by Gna!
http://gna.org/
_______________________________________________
Wesnoth-bugs mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-bugs