Array variables inside the prestart event
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.
Array variables inside the prestart event
I've just noticed, that using array variables e.g. $arrayname.[$arraynumber].name inside a prestart event doesn't work. It seems the variable is considered as empty. The solution for me was, to put it in the start event. I'm still curious: Is this a bug or this intended?
The future belongs to those, who believe in the beauty of their dreams.
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
Re: Array variables inside the prestart event
That seems like a restriction which would be too bizarre to be intentional (even for WML).Heindal wrote: ↑March 16th, 2025, 8:17 pm I've just noticed, that using array variables e.g. $arrayname.[$arraynumber].name inside a prestart event doesn't work. It seems the variable is considered as empty. The solution for me was, to put it in the start event. I'm still curious: Is this a bug or this intended?
But I see that some mainline campaigns are using arrays in prestart - for example, The Sceptre of Fire.
I also notice that you have an extra dot in $arrayname.[$arraynumber].name - I'm not sure if that's your issue. I would post your entire prestart event code here so people can check the whole thing for possible problems.
Re: Array variables inside the prestart event
Thanks gnombat for the information.
My bad, the point isn't in the original. Pew.
I've realized this problems two times now, under different circumstances, so this might be a problem after all.
I'm with you - the behavior is bizarre as the code works in start event and not in prestart.
First example: Imagine you made an array with amazing relics the player can find giving power beyond legendofinvinciblish imagination.
You would set the array like this:
After that you call an event to create these events on the map:
This works fine in start, but not in prestart event. However, if you change:
to
It works fine. So ... where is the problem? Same code behaves differently if started in prestart or start event.
My bad, the point isn't in the original. Pew.
I've realized this problems two times now, under different circumstances, so this might be a problem after all.
I'm with you - the behavior is bizarre as the code works in start event and not in prestart.
First example: Imagine you made an array with amazing relics the player can find giving power beyond legendofinvinciblish imagination.
You would set the array like this:
Code: Select all
[set_variables]
name=relics
mode=replace
[value]
id=0
name=Flamme
[/value]
[value]
id=1
name=Tectonic
[/value]
[value]
id=2
name=Goibniu
[/value]
[value]
id=3
name=Tyrfing
[/value]
[/set_variables]
After that you call an event to create these events on the map:
Code: Select all
[set_variable]
name=relicnumber
rand=0..3
[/set_variable]
[set_variable]
name=relic
value=$relics[$relicnumber].name
[/set_variable]
[label]
x,y=16,7
text=$relic
color=200,200,0
[/label]
[item]
x,y=16,7
image=overlay/relic/$relic|.png
halo=halo/illuminates-aura.png
[/item]
Code: Select all
[set_variable]
name=relicnumber
rand=0..3
[/set_variable]
[set_variable]
name=relic
value=$relics[$relicnumber].name
[/set_variable]
Code: Select all
[set_variable]
name=relic
rand=Flamme,Tectonic,Goibniu,Tyrfing
[/set_variable]
The future belongs to those, who believe in the beauty of their dreams.
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
Re: Array variables inside the prestart event
To not have any confusions would be good to have reproducible example of prestart event which behaves strangely.
Re: Array variables inside the prestart event
Found it, thanks for the advice, but it was a little tricky.
Defining a variable in prestart and start isn't the only thing, it is where you place labels and images. If you place image and label in the scenario itself and define a prestart variable with arrays, labels and images don't know the variable $relic. This looks like this:
The event itself, seems to know the variable. Maybe this code can help to reproduce it, but I think it isn't a bug as I feared - the variable is probably just parsed after the images and labels inside the scenario itself. Still curious why the other approach with fixed random attributes was working.
How I have to fix my broken code back and fix it. Some much about publishing this update this evening.
Fixed: this is how it looks like when it is working (just copied label and image into the prestart).
Defining a variable in prestart and start isn't the only thing, it is where you place labels and images. If you place image and label in the scenario itself and define a prestart variable with arrays, labels and images don't know the variable $relic. This looks like this:
The event itself, seems to know the variable. Maybe this code can help to reproduce it, but I think it isn't a bug as I feared - the variable is probably just parsed after the images and labels inside the scenario itself. Still curious why the other approach with fixed random attributes was working.
Code: Select all
[event]
name=prestart
[set_variables]
name=relics
mode=replace
[value]
id=0
name=Flamme
[/value]
[value]
id=1
name=Tectonic
[/value]
[value]
id=2
name=Goibniu
[/value]
[value]
id=3
name=Tyrfing
[/value]
[/set_variables]
[set_variable]
name=relicnumber
rand=0..3
[/set_variable]
[set_variable]
name=relic
value=$relics[$relicnumber].name
[/set_variable]
[/event]
[event]
name=start
[/event]
[label]
x,y=16,7
text=$relic
color=200,200,0
[/label]
[item]
x,y=16,7
image=overlay/relic/$relic|.png
halo=halo/illuminates-aura.png
[/item]
[event]
name=moveto
first_time_only=yes
[filter]
x=16
y=7
id=you
[/filter]
[set_variable]
name=$relic
add=1
[/set_variable]
[message]
speaker=you
message= _ "The <b>$relic|</b> is yours now!"
[/message]
[remove_item]
x,y=16,7
[/remove_item]
[/event]
How I have to fix my broken code back and fix it. Some much about publishing this update this evening.
Fixed: this is how it looks like when it is working (just copied label and image into the prestart).
Spoiler:
Last edited by Heindal on March 17th, 2025, 7:24 pm, edited 1 time in total.
The future belongs to those, who believe in the beauty of their dreams.
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
Re: Array variables inside the prestart event
Yes, scenario tags outside of [event] are not able to use variables.
- Spannerbag
- Posts: 761
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Array variables inside the prestart event
Weird, I don't think I could reproduce this?
I tweaked your code to use standard items and also cleared the label.
Due to my test campaign having a tiny map I changed the location to 4,4.
Could you copy and paste this code into your scenario or test campaign and see if it works as expected?
The setup is all done in
After prestart I see this: When I move to 4,4 the
Here's my code, hope it helps - tho' I've not done anything different to you as far as I know.
There are messages to report what the random selection does.
Of course I may have misunderstood what was wrong in the first place!
Anyway, hope this helps...
Cheers!
-- Spannerbag
I tweaked your code to use standard items and also cleared the label.
Due to my test campaign having a tiny map I changed the location to 4,4.
Could you copy and paste this code into your scenario or test campaign and see if it works as expected?
The setup is all done in
prestart
so there are only 2 events; prestart
and moveto
.After prestart I see this: When I move to 4,4 the
message
is issued:
Tidy up after message dismissed:
Is that how you wanted the logic to work?Here's my code, hope it helps - tho' I've not done anything different to you as far as I know.
There are messages to report what the random selection does.
Of course I may have misunderstood what was wrong in the first place!

Code: Select all
[event]
name=prestart
[set_variables]
name=relics
mode=replace
[value]
id=0
name=altar
[/value]
[value]
id=1
name=armor-golden
[/value]
[value]
id=2
name=ball-green
[/value]
[value]
id=3
name=book2
[/value]
[/set_variables]
[set_variable]
name=relicnumber
rand=0..3
[/set_variable]
{DEBUG_MSG (_"relicnumber=$relicnumber")}
[set_variable]
name=relic
value=$relics[$relicnumber].name
[/set_variable]
{DEBUG_MSG (_"relic=$relic, placing label")}
[label]
x,y=4,4
text=$relic
color=200,200,0
[/label]
{DEBUG_MSG (_"placing item")}
[item]
x,y=4,4
image=items/$relic|.png
halo=halo/illuminates-aura.png
[/item]
{CLEAR_VARIABLE relics} # Assuming it isn't needed again...
[/event]
[event]
name=moveto
first_time_only=yes
[filter]
x,y=4,4
side=1
[/filter]
[set_variable]
name=$relic
add=1
[/set_variable]
[message]
x,y=4,4
message= _ "The <b>$relic|</b> is yours now!"
[/message]
[remove_item]
x,y=4,4
[/remove_item]
{REMOVE_LABEL 4 4}
[/event]
Cheers!
-- Spannerbag
Re: Array variables inside the prestart event
Hi spanner,
yeah I've already mentioned what the problem was in my previous post. If you place the item and the label inside scenario and not in an event and set the variable with an array variable in a prestart event, the label und the item don't get the necessary variable.
Strange thing, if you don't use array variables this works. Must have something to do with the internal order of the call.
yeah I've already mentioned what the problem was in my previous post. If you place the item and the label inside scenario and not in an event and set the variable with an array variable in a prestart event, the label und the item don't get the necessary variable.
Strange thing, if you don't use array variables this works. Must have something to do with the internal order of the call.
The future belongs to those, who believe in the beauty of their dreams.
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
- Spannerbag
- Posts: 761
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Array variables inside the prestart event
Ah, I'd misread your code and saw what I expected to see, not what was there. 
I understand now.
Cheers!
-- Spannerbag

I understand now.
Cheers!
-- Spannerbag