combining multiple variable names

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.
User avatar
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

combining multiple variable names

Post by The_Gnat »

I have a very interesting situation where i want to store units as "in_boat_$boat.id"

Then i would be able to run a {FOREACH in_boat_$boat.id i} to find all the units in a boat with a particular id however if i do this:

Code: Select all

			[store_unit]
			   [filter]
				id=$unit.id
			   [/filter]
			   variable=in_boat_$boat_selected.id
			   kill=yes
			   mode=append
			[/store_unit]
the variable stores properly but i fail to be able to access it again through the FOREACH statement.

I am sure their must be an easier way to do this. Here is my full code:

Code: Select all

#define BOAT_STUFF
[event]
    name=moveto
    first_time_only=no

	[filter]
            [filter_adjacent]
                side=$unit.side
                count=1
		type="Elvish Fighter"
            [/filter_adjacent]
	[/filter]    

	[store_unit]
	    [filter]
            	side=$unit.side
		type="Elvish Fighter"
            	[filter_adjacent]
		      id=$unit.id
            	[/filter_adjacent]
	    [/filter]
	    variable=boat_selected
	[/store_unit]

	{MESSAGE_N (Adjacent to a Boat $boat_selected.name)}

	[message]
	   speaker=narrator
	   message=_"Get in?"

	   [option]
		message=_"Nope"
	   [/option]
	   [option]
		message=_"Yes"
	
		[command]
			[store_unit]
			   [filter]
				id=$unit.id
			   [/filter]
			   variable=in_boat_$boat_selected.id
			   kill=yes
			   mode=append
			[/store_unit]

		        {MESSAGE_N (Stored unit in_boat_$boat_selected.id)}
		[/command]
	   [/option]
	[/message]


[/event]
[event]
    name=moveto
    first_time_only=no

	[filter]
            [filter_adjacent]
                side=$unit.side
		type="Elvish Fighter"
            [/filter_adjacent]
	    [and]
		[not]
                   [filter_adjacent]
                       side=$unit.side
                       type="Elvish Fighter"
		       count=1
                   [/filter_adjacent]
		[/not]
	    [/and]
	[/filter]    

	{MESSAGE_N (More than one boat, what??)}

[/event]

[event]
    name=side turn
    first_time_only=no

    [set_menu_item]
        id=assign_hero_menu_item
        description="Unstore units from boat"
	image="misc/ums.png"
        [show_if]
		# if this is a boat on your team
                [have_unit]
                    x,y=$x1,$y1
                    side=$side_number
		    type="Elvish Fighter"
                [/have_unit]
        [/show_if]
        [command]

	[store_unit]
	    [filter]
		id=$unit.id
	    [/filter]
	    variable=boat_selected
	[/store_unit]

		[message]
			speaker=narrator
			message="Doing something with $boat_selected.id The first three in the boat are: - $in_boat_[$boat_selected[0].id].id - $in_boat_[$boat_selected[1].id].id - in_boat_$boat_selected[2].id

Also written as - in_boat_$boat_selected.id[0].id"
		[/message]
	
	{FOREACH in_boat_$boat_selected.id i}

		[message]
			speaker=narrator
			message="Releasing $in_boat_[$boat_selected[$i].id]"
		[/message]

		[unstore_unit]
		    variable=in_boat_[$boat_selected[$i].id]
		[/unstore_unit]

	{NEXT i}

	{CLEAR_VARIABLE in_boat_$boat_selected}

	[unstore_unit]
	     variable=boat_selected
	[/unstore_unit]

        [/command]
    [/set_menu_item]
[/event]
#enddef
Thank you!!!
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: combining multiple variable names

Post by zookeeper »

Likely not your current problem, but if you use unit id's as part of variable names then you have to make sure that they never contain spaces.
User avatar
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

Re: combining multiple variable names

Post by The_Gnat »

zookeeper wrote:Likely not your current problem, but if you use unit id's as part of variable names then you have to make sure that they never contain spaces.
Yep, that is my problem the unit Elvish Fighter's id is "Elvish Fighter-4" so since it has a space it would then cause problems.

Is there anyway to check if a variable contains a space? Is there anyway to then remove that space?

Thank you for the quick response! :D
User avatar
Pentarctagon
Project Manager
Posts: 5565
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: combining multiple variable names

Post by Pentarctagon »

Also, when posting chunks of code like this, I think it would be best if you instead created a minimal example of what the problem is - something that someone else can easily copy/paste into any scenario and quickly setup to see what you're seeing. Often times this even let's me figure out how to solve my own problem, since to create a minimal example requires you to also have a good understanding of what the problem is and how to reproduce it.

As for removing spaces, you'll almost certainly need to use lua, in which case there's string.gsub.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: combining multiple variable names

Post by gfgtdf »

I wonder whether we should replace all spaces wih underscores in mainline, code like recuit="Elvish Archer" or type="Elvish Archer" woudl ofc still work since we could make those points spaces treat the same as underscores.

I think we had this discussion already someho but i don't remember the conclusion.

EDIT: just noticed that in 1.13 the - chracter is invalid in variablenames aswell, so we maybe want to replace - with _ aswell
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Paulomat4
Moderator Emeritus
Posts: 730
Joined: October 16th, 2012, 3:32 pm
Location: Wesmere library, probably summoning Zhangor

Re: combining multiple variable names

Post by Paulomat4 »

I wonder whether we should replace all spaces wih underscores in mainline, code like recuit="Elvish Archer" or type="Elvish Archer" woudl ofc still work since we could make those points spaces treat the same as underscores.

I think we had this discussion already someho but i don't remember the conclusion.

EDIT: just noticed that in 1.13 the - chracter is invalid in variablenames aswell, so we maybe want to replace - with _ aswell
I fully support this! :)
Creator of Dawn of Thunder and Global Unitmarkers

"I thought Naga's used semi-automatic crossbows with incendiary thermite arrows . . . my beliefs that this race is awesome are now shattered." - Evil Earl
User avatar
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

Re: combining multiple variable names

Post by The_Gnat »

Pentarctagon wrote:Also, when posting chunks of code like this, I think it would be best if you instead created a minimal example of what the problem is - something that someone else can easily copy/paste into any scenario and quickly setup to see what you're seeing. Often times this even let's me figure out how to solve my own problem, since to create a minimal example requires you to also have a good understanding of what the problem is and how to reproduce it.

As for removing spaces, you'll almost certainly need to use lua, in which case there's string.gsub.
Thanks Pentarctagon, i will look at my code closer and try to isolate the problem. I agree pasting all that code is very confusing :mrgreen:
User avatar
Ravana
Forum Moderator
Posts: 3009
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: combining multiple variable names

Post by Ravana »

Usual way for this would be "boat.variables.units".
User avatar
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

Re: combining multiple variable names

Post by The_Gnat »

Ravana wrote:Usual way for this would be "boat.variables.units".
:mrgreen: Thanks
User avatar
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

Re: combining multiple variable names

Post by The_Gnat »

Hello Dev's i have managed to create a working event that allows units to enter a boat or caravan and then get off again on a right-click menu button.

Here is the thread i post the code on. It seems mostly functional and should work in multiplayer and campaigns!

Unfortunately i read that you can not use [allow_undo] in 1.12 if you have [messages] and [options], that is unfortunate. (Also does this also happen in 1.13)

Thank you for all your help.

(btw i figured out how to do this after examining SoBe, so this is not entirely my genious :mrgreen: )
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: combining multiple variable names

Post by gfgtdf »

Unfortunately i read that you can not use [allow_undo] in 1.12 if you have [messages] and [options], that is unfortunate. (Also does this also happen in 1.13)
in 1.13 you can use [allow_undo] in events with [messages] and [option]s, unfortunatley we couldn't backport that fix because it could have resulted in incompabilities version in the 1.12 multipler server.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

Re: combining multiple variable names

Post by The_Gnat »

gfgtdf wrote:
Unfortunately i read that you can not use [allow_undo] in 1.12 if you have [messages] and [options], that is unfortunate. (Also does this also happen in 1.13)
in 1.13 you can use [allow_undo] in events with [messages] and [option]s, unfortunatley we couldn't backport that fix because it could have resulted in incompabilities version in the 1.12 multipler server.
A very late reply: Thank you, yes that makes sense and i am glad to hear it has been fixed in 1.13!
User avatar
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

another variable problem

Post by The_Gnat »

Hello again. I have had another variable problem.
inspector.png
As you can see from the inspector the event has a variable as its x and y coordinates. However this is a problem, because i overwrite that variable in order to use it for another event.

Is there a way to give an event a variable amount, but then assign it the actual value of that variable so the filter in my event would instead say:

Code: Select all

[filter]
   x=10
   y=8
[/filter]
Which is what my variables contain until they are overwritten.
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: combining multiple variable names

Post by zookeeper »

User avatar
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

Re: combining multiple variable names

Post by The_Gnat »

I think i understand, basically if i put delayed_variable_substituition=no then it will set my variables from the moment the event is created!

Thank you i will see if this works! :D
Post Reply