Array variables inside the prestart event

The place to post your WML questions and answers.

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.
Post Reply
User avatar
Heindal
Posts: 1459
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Array variables inside the prestart event

Post by Heindal »

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
gnombat
Posts: 892
Joined: June 10th, 2010, 8:49 pm

Re: Array variables inside the prestart event

Post by gnombat »

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?
That seems like a restriction which would be too bizarre to be intentional (even for WML).

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.
User avatar
Heindal
Posts: 1459
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Re: Array variables inside the prestart event

Post by Heindal »

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:

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]
            
            
This works fine in start, but not in prestart event. However, if you change:

Code: Select all

[set_variable]
            name=relicnumber
            rand=0..3
        [/set_variable]
		
        [set_variable]
            name=relic
            value=$relics[$relicnumber].name
        [/set_variable]
to

Code: Select all

		
        [set_variable]
            name=relic
            rand=Flamme,Tectonic,Goibniu,Tyrfing
        [/set_variable]
It works fine. So ... where is the problem? Same code behaves differently if started in prestart or start event.
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
User avatar
Ravana
Forum Moderator
Posts: 3314
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Array variables inside the prestart event

Post by Ravana »

To not have any confusions would be good to have reproducible example of prestart event which behaves strangely.
User avatar
Heindal
Posts: 1459
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Re: Array variables inside the prestart event

Post by Heindal »

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:

empytrelicvariable.png

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
User avatar
Ravana
Forum Moderator
Posts: 3314
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Array variables inside the prestart event

Post by Ravana »

Yes, scenario tags outside of [event] are not able to use variables.
User avatar
Spannerbag
Posts: 761
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Array variables inside the prestart event

Post by Spannerbag »

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 prestart so there are only 2 events; prestart and moveto.

After prestart I see this:
01_after_prestart.jpg
When I move to 4,4 the message is issued:
02_after_moveto.jpg
Tidy up after message dismissed:
03_after_message.jpg
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! :doh:

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]
Anyway, hope this helps...

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Heindal
Posts: 1459
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Re: Array variables inside the prestart event

Post by Heindal »

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.
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
User avatar
Spannerbag
Posts: 761
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Array variables inside the prestart event

Post by Spannerbag »

Ah, I'd misread your code and saw what I expected to see, not what was there. :doh:
I understand now.
Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
Post Reply