How to use Array Variables in WML?

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.
AI
Developer
Posts: 2396
Joined: January 31st, 2008, 8:38 pm

Re: How to use Array Variables in WML?

Post by AI »

I hope you don't have any flying units in that scenario then.
Dret
Posts: 79
Joined: June 13th, 2007, 11:06 pm
Contact:

Re: How to use Array Variables in WML?

Post by Dret »

No... the scenario is a battle 1vs1 with only pre-recruit units.

I want to simulate a sort of mix between a chess play and the strategic hex-based board game.

The units are only humans without fantasy elements (no magic units).

The map is simmetric and there's:

- 4 Special Operation avaiable by Leader inside the General Quarter
- 2 Special Operation avaiable by Right-Click Mouse
- 2 Special Operation avaiable in a location in the middle of the map
(at this moment!)

.... give me time to test as better for balancing...!!

Bye! :wink:
User avatar
Lizard
Posts: 355
Joined: January 19th, 2008, 8:20 am
Location: Hiding in a swamp (gtm +1; DST)

Re: How to use Array Variables in WML?

Post by Lizard »

How do I check if at a given location is a trap?
I want to random place n traps at the beginning of scenario, but I don't want two traps at the same location.(I got random placing from "Hall of Lame")

Code: Select all

#define PLACE_A_TRAP_RANDOM
# generating the random locations... 
[if]
	### now I want to check if at $xtarget/$ytarget isn't a trap. This is the problem.

	# then i place a trap, and decrease a var.
[/if]
#enddef
~ I'll heal you by 4 hp if you post next to me ~
Have a look at the Era of Strife, featuring Eltireans, Eventide, Minotaurs, Saurians and Triththa
Dret
Posts: 79
Joined: June 13th, 2007, 11:06 pm
Contact:

Re: How to use Array Variables in WML?

Post by Dret »

Hi to all...
A new question about array...

I used succesfully ALL the suggested solutions You purpose to my problems... (thanks a lot again!)

Now I have to improve my code in this way:

Using this code:

Code: Select all

  

  [store_locations]
       x=$x1
       y=$y1
       [or]
           find_in=fire
       [/or]
       variable=fire
    [/store_locations]

I store some locations in an array, each have a "x", "y", "terrain" variable inside...

BUT i if need do "ADD" (in the same moment) another variable to each location?
(for example $turn_number)


I try something like this but i have a dubt for the Index value:
When an array elemets is erased, the index are re-numerated?

Example:

fire[0]
fire[1]
fire[2]
fire[3]

removing fire[1].... become:

fire[0]
fire[1] (ex 2)
fire[2] (ex 3)

I don't understand what kind of index i have to use do considering the possibility to "ERASE" one element during the game??

May I use $fire.length in this way?

Code: Select all

  
  [store_locations]
       x=$x1
       y=$y1
       [or]
           find_in=fire
       [/or]
       variable=fire
    [/store_locations]
	{VARIABLE indice $fire.length} 
	[set_variable]
	name=indice
	add=-1
	[/set_variable]	
	 {VARIABLE fire[$indice].turn $turn_number} 

 


Thanks..
Bye!
User avatar
DEATH_is_undead
Posts: 960
Joined: March 4th, 2007, 3:00 pm
Location: Northern United States

Re: How to use Array Variables in WML?

Post by DEATH_is_undead »

How would i set it for a random unit?
3P MP Scenario - Great Dwarves Escape
The best way to learn is to follow. In order to learn WML, you have to follow other's work, and check their codes.
Dret
Posts: 79
Joined: June 13th, 2007, 11:06 pm
Contact:

Re: How to use Array Variables in WML?

Post by Dret »

Problem.. solved!! :roll:

It works... i made a mistake in another part of code...!! :annoyed: :annoyed:

Bye!
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: How to use Array Variables in WML?

Post by Sapient »

DEATH_is_undead wrote:How would i set it for a random unit?
store all locations with units, store length minus one in another variable, and choose a random one using set_variable rand=0..n


don't clutter the Array Tutorial thread please...
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
MCP
Posts: 518
Joined: May 23rd, 2005, 5:23 pm
Location: California

Re: How to use Array Variables in WML?

Post by MCP »

I am interested in 2-D or multi-dimensional arrays. I am wondering if there is syntax such as array1[0][0].x for [rows][columns].

I am pretty sure I can define the size and type of contents of these 2-D array at the beginning of the scenario, if that matters.

To give an idea,
I want to store an area of terrain information so I can temporarily change it until some other event occurs, which may occur zero or more turns from then. This area is gonna be something like 10x10 and it won't change dynamically.

I am pretty sure I know how to read and change the terrain, but I just don't know how to use arrays for 2-D.

edit:
Can I, or maybe should I ask, do I have to do array_row[0].array_column[0].x?
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: How to use Array Variables in WML?

Post by Sapient »

a scalar value in a 2D-array could be expressed as name=column[$x].row[$y].value

alernatively, for a sparsely populated array you may find it more efficient to construct the variable name out of the index, like name=coords.column_$x.row_$y

however, the first example is much easier to use in a FOREACH loop
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: How to use Array Variables in WML?

Post by Sapient »

If someone would like to write up an array tutorial on the wiki covering the topics in this sticky thread as well as other relevant topics (such as iteration using FOREACH), then we may unstickify this thread and/or replace it with a link to the relevant wiki article. Meanwhile please note that this thread contains slightly outdated and/or misleading advice. For example, [set_variables] notation is now the preferred way of initializing and modifying array variables in WML.
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
Post Reply