Giving arrays variable names and values that are also variables

The place to post your WML questions and answers.

Moderators: Forum Moderators, Developers

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

Giving arrays variable names and values that are also variables

Post by Nyanyanyan » April 16th, 2019, 12:01 pm

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.

User avatar
Nyanyanyan
Posts: 28
Joined: May 11th, 2018, 10:40 pm

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

Post by Nyanyanyan » April 16th, 2019, 12:22 pm

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?

User avatar
Ravana
Moderator
Posts: 2196
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

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

Post by Ravana » April 16th, 2019, 3:01 pm

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

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

Post by Nyanyanyan » April 16th, 2019, 9:39 pm

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.

Atreides
Posts: 15
Joined: March 30th, 2019, 10:38 pm

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

Post by Atreides » April 21st, 2019, 9:22 pm

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.
--
Ya Hya Chouhada!

User avatar
Nyanyanyan
Posts: 28
Joined: May 11th, 2018, 10:40 pm

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

Post by Nyanyanyan » August 3rd, 2019, 8:38 am

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.

Post Reply