Using arrays to avoid duplication of events

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
Bob_The_Mighty
Posts: 870
Joined: July 13th, 2006, 1:15 pm

Using arrays to avoid duplication of events

Post by Bob_The_Mighty »

I got another one for you... :)

This is what I want to do: When a unit moves into a certain area, i want to check if that unit has learnt the 'tracking' skill. If so, it triggers a message telling you stuff.

There are 4 units, each of whom may or may not have the skill. Since there are numerous locations with different messages I only want to have one event per location which means that it must work for all 4 players.

At the start of the scenario I make sure none of them has it with this:

Code: Select all

[set_variable]
	name=tracking1
	value=no
	[/set_variable]
[set_variable]
	name=tracking2
	value=no
	[/set_variable]
[set_variable]
	name=tracking3
	value=no
	[/set_variable]
[set_variable]
	name=tracking4
	value=no
	[/set_variable]
Then when they learn the skill I store the unit and do this:

Code: Select all

[set_variable]
	name=tracking[$unit.side]
	value=yes
	[/set_variable]
And when I want to use the skill I have this:

Code: Select all

[event]
name=moveto
first_time_only=no
[filter]
x=93-98
y=25-28
side="1,2,3,4"
canrecruit=1
[/filter]
[store_unit]
	[filter]
	x=$x1
	y=$y1
	[/filter]
	variable=unit
	[/store_unit]
[if]
[variable]
	name=tracking[$unit.side]
	equals=yes
	[/variable]
[then]
[message]
	speaker=unit
	message= _ "There are deep footprints here of a very large two-legged creature. They seem to be leading north."
	[/message]
[/then]
[/if]
[unstore_unit]
	variable=unit
	[/unstore_unit]
[/event]

But it doesn't work. I tried replacing the first four [set_variable] tags with a single one, like this:

Code: Select all

[set_variable]
	name=tracking[$unit.side]
	value=no
	[/set_variable]
But I'm pretty sure that doesn't work either. Do I need to store each of the units in turn to make it work? Or what?
My current projects:
MP pirate campaign: The Altaz Mariners
RPG sequel: Return to Trent
MP stealth campaign: Den of Thieves
NeoPhile
Posts: 155
Joined: July 22nd, 2004, 4:33 am
Location: Halifax, NS, Canada
Contact:

Post by NeoPhile »

Is there any reason you couldn't use role=tracker? You seem to be making this needlessly complex.
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: Using arrays to avoid duplication of events

Post by Sapient »

Well, there are numerous ways to do this; and there are numerous problems with the way you tried to do it. ;)
At the start of the scenario I make sure none of them has it with this:

Code: Select all

[set_variable]
	name=tracking1
	value=no
	[/set_variable]
[set_variable]
	name=tracking2
	value=no
	[/set_variable]
[set_variable]
	name=tracking3
	value=no
	[/set_variable]
[set_variable]
	name=tracking4
	value=no
	[/set_variable]
First of all, those aren't arrays. You don't really need to use arrays in this situation if you don't want to. If you play your cards right, you don't even need to initialize anything either.

But let's say I did want to initialize these values, and for some reason I wanted them to be in an array tracking[]. Then I would put this inside the scenario tag somewhere:

Code: Select all

[variables]
 [tracking]
# player 0, unused
 [/tracking]
 [tracking]
# player 1
  is_tracking=false
 [/tracking]
 [tracking]
# player 2
  is_tracking=false
 [/tracking]
 [tracking]
# player 3
  is_tracking=false
 [/tracking]
 [tracking]
# player 4
  is_tracking=false
 [/tracking]
[/variables]
And then the variable for each side is
tracking[$side_number].is_tracking

And the value is
$tracking[$side_number].is_tracking

But, there are easier ways of doing it.
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
Bob_The_Mighty
Posts: 870
Joined: July 13th, 2006, 1:15 pm

Post by Bob_The_Mighty »

Quote:
And then the variable for each side is
tracking[$side_number].is_tracking

And the value is
$tracking[$side_number].is_tracking


Is that what you meant? Because I would take it to mean that you could check if they had the skill by doing this:

Code: Select all

[if]
[variable]
   name=tracking[$side_number].is_tracking
   equals=$tracking[$side_number].is_tracking
   [/variable]
[then]
But that is a tautology. And anyway, how would I give them the skill? Like this?

Code: Select all

[variables]
 [tracking]
  tracking[$side_number].is_tracking=true
 [/tracking]
[/variables]
The reason i did it like this was because I want to use it for other things. For instance, say the unit had to have tracking level 3 to get the message. Would I initialise it to zero then check like this?

Code: Select all

[if]
[variable]
   name=tracking[$side_number].is_tracking
   greater_than_equal_to=3
   [/variable]
[then]
Oh, I just realised I am terribly confused...
Last edited by Bob_The_Mighty on February 21st, 2007, 7:27 pm, edited 2 times in total.
My current projects:
MP pirate campaign: The Altaz Mariners
RPG sequel: Return to Trent
MP stealth campaign: Den of Thieves
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Post by Sapient »

To anwer your questions: no, no, and no. You could check if they had the skill by doing this:

Code: Select all

[if]
  [variable]
   name=tracking[$side_number].is_tracking
   equals=true
  [/variable]
[then]
The [variables] tag can only be used for initialization at the beginning of the scenario. Inside an event, you can give them the skill like this:

Code: Select all

  {VARIABLE  tracking[$side_number].is_tracking true}
If you want a number instead of a boolean, you should initialize level=0 instead of is_tracking=false.

(note: in the above code I assume that $side_number variable has been set by the side turn event. Otherwise, you could use the $side of a stored unit.)
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