Giving arrays variable names and values that are also variables

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
Nyanyanyan
Posts: 73
Joined: May 11th, 2018, 10:40 pm

Giving arrays variable names and values that are also variables

Post by Nyanyanyan »

What I'm trying to do is making an array out of multiple variables, though for some reason using a variable as the name for the value in the array doesn't work and the game just saves the array as empty.
What I did and which isn't working:

Code: Select all

	
		{VARIABLE $newrebelleader "Id of the rebel leader"}
		[set_variables]
			name=capturedvillagestotal
			[value]
			x$newrebelleader=$leaderdeadsidevillages[$i].x
			y$newrebelleader=$leaderdeadsidevillages[$i].y
			[/value]
		[/set_variables]
$leaderdeadsidevillages[$i].x and y are just iterating over an array created by [store_villages], this whole thing being inside a FOREACH loop.

From my testing it seems that you simply can't use variables in the name for other variables created in this way.
Is there any way around this?
The whole point of this is trying to save which villages should belong to a certain leader that shares it's side with others, since to my knowledge, you can't save variables with locations, so my previous technique doesn't work here.
If there's an easier way to do this I'm all ears.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.
User avatar
Nyanyanyan
Posts: 73
Joined: May 11th, 2018, 10:40 pm

Re: Giving arrays variable names and values that are also variables

Post by Nyanyanyan »

Upon further testing I found out that the values of the id variables themselves are the problem.
A generic id would be something like 'Drake Blademaster-5'. Neither the space nor the - are compatible with the syntax from what I'm seeing.
Does anyone know of a way to circumvent this?
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.
User avatar
Ravana
Forum Moderator
Posts: 2997
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Giving arrays variable names and values that are also variables

Post by Ravana »

There are Lua solutions such as location sets https://wiki.wesnoth.org/LuaWML/Location_set

But WML variables are limited by WML syntax rules - they are in save file as WML, so only valid attribute names can be used.

You could do

Code: Select all

[value]
			x=$leaderdeadsidevillages[$i].x
			y=$leaderdeadsidevillages[$i].y
leader=$newrebelleader
			[/value]
User avatar
Nyanyanyan
Posts: 73
Joined: May 11th, 2018, 10:40 pm

Re: Giving arrays variable names and values that are also variables

Post by Nyanyanyan »

Ravana wrote: April 16th, 2019, 3:01 pm You could do

Code: Select all

[value]
			x=$leaderdeadsidevillages[$i].x
			y=$leaderdeadsidevillages[$i].y
leader=$newrebelleader
			[/value]
Thanks a lot, after a lot of debugging I got it to work as intended.

I uploaded the finished mod to the add-on server under "Revolution and Civil War" for anyone interested.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.
User avatar
Atreides
Posts: 1074
Joined: March 30th, 2019, 10:38 pm
Location: On the 2nd story of the centre village of Merwuerdigliebe turning the lights on and off

Re: Giving arrays variable names and values that are also variables

Post by Atreides »

Hi, just tried the mod and it gave an error (long) but the gist seems to be that side 99 is invalid when it tried to create rebel units. 99 is the default set by the mod but can be altered, should I try with a lower #? It's a neat idea.
User avatar
Nyanyanyan
Posts: 73
Joined: May 11th, 2018, 10:40 pm

Re: Giving arrays variable names and values that are also variables

Post by Nyanyanyan »

There's a slider for the side number the rebels use during game setup (as you probably saw). The mod will kill all units of that side at the start of the match, so it's set to 99 to prevent killing an actual player.

To fix your problem you can simply set this to a side number that actually exists (e.g. in a 5 player map, there are sides 1-5).
The drawback is that you need one extra side so a four player map will only have 3 actual players and so on.

I usually play on custom maps that already have a designated rebellion side, but when playing on standard maps I recommend just setting the slider to the last side in the game and using maps with a higher number of players than you would like to play with.

PS: Sorry for the late reply, I didn't have much time for wesnoth in the past months.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.
User avatar
Atreides
Posts: 1074
Joined: March 30th, 2019, 10:38 pm
Location: On the 2nd story of the centre village of Merwuerdigliebe turning the lights on and off

Re: Giving arrays variable names and values that are also variables

Post by Atreides »

Been having great fun with this mod but it does seem to have a few lil bugs. I just played a game where the rebels ended up with 2 leaders. The original side from which the rebels sprang was dead already so maybe it just needs a check to see if there are any sides left before allocating a new leader.

There are now 4 map packs that support this mod: Dominus, Red Maps, Atz Maps and Dusan Maps.
User avatar
Atreides
Posts: 1074
Joined: March 30th, 2019, 10:38 pm
Location: On the 2nd story of the centre village of Merwuerdigliebe turning the lights on and off

Re: Giving arrays variable names and values that are also variables

Post by Atreides »

Noticed something interesting with the civ reb war mod. I played a game with it left at 99 and while there were no rebelllions it does still replace the leader whenever killed. I'd assumed that it was off but I guess the leader replacement is always on when the mod is on. Silly of me. : )
In any case just wanted to add some documentation in that the mod can be used as a replace leader mod if so desired.
Since the rebellions work best with large battles on large maps it has been added to Bom Site map pack as well now.

P.S. My previous post is likely wrong. I have since found out that there can be multiple rebellions, that was confusing me.
Post Reply