spaces in names of containers

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.
SlowThinker
Posts: 876
Joined: November 28th, 2008, 6:18 pm

spaces in names of containers

Post by SlowThinker »

Wiki says:
Each variable is given a name. A given variable name may contain only alphabetic characters, digits, and underscores.
but it looks a space is considered an alphabetic character too: for example it is possible to use a container "Dwarvish Guardsman".

The problem is I didn't find a way how to access a content of such a container directly, I mean by something like

Code: Select all

$units."Dwarvish Guardsman".max_hitpoints
I can access it only indirectly, by means of an auxiliary variable:

Code: Select all

[set_variables]
	name=aux_var
	to_variable=units.Dwarvish Guardsman
[/set_variables]
.... $aux_var.max_hitpoints ....
Last edited by SlowThinker on December 15th, 2010, 11:06 pm, edited 1 time in total.
I work on Conquest Minus • I use DFoolWide, Retro Terrain Package and the add-on 'High Contrast Water'
I moved to Nosebane's corner (Doc Paterson's signature); I am spending my time there, so PM me if I don't answer your post in forums
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: spaces in names of containers

Post by zookeeper »

Have you tried loading a savefile of a scenario using variables with spaces in their names? The last time I checked (which was quite a long time ago) you'll get some (non-fatal) errors.

In any case I'm pretty sure it's not supported. It might still unintendedly work in some cases, but that's another story.
SlowThinker
Posts: 876
Joined: November 28th, 2008, 6:18 pm

Re: spaces in names of containers

Post by SlowThinker »

zookeeper wrote:Have you tried loading a savefile of a scenario using variables with spaces in their names? The last time I checked (which was quite a long time ago) you'll get some (non-fatal) errors.
You are right. The containers with spaces were kicked.

Do you have an idea how I can implement a list of unit types, which
  • can be scanned/searched
  • and simultaneously their elements can be directly accessed
?

I planned to use

Code: Select all

[Spearman]
	heal_cost="25"
	...
[/Spearman]
[item]
	name="Spearman"
[/item]
[Dwarvish Guardsman]
	heal_cost="45"
	...
[/Dwarvish Guardsman]
[item]
	name="Dwarvish Guardsman"
[/item] 
([Spearman] for a direct access, [item] for scanning)

I could replace spaces by underscores and return them back, but does WML allow to manipulate chars in strings?
Last edited by SlowThinker on December 16th, 2010, 12:59 am, edited 1 time in total.
I work on Conquest Minus • I use DFoolWide, Retro Terrain Package and the add-on 'High Contrast Water'
I moved to Nosebane's corner (Doc Paterson's signature); I am spending my time there, so PM me if I don't answer your post in forums
User avatar
bigkahuna
Posts: 657
Joined: September 11th, 2010, 6:24 pm
Location: In your mind.

Re: spaces in names of containers

Post by bigkahuna »

SlowThinker wrote:Do you have an idea how I can implement a list of unit types, which

can be scanned/searched
and simultaneously their elements can be directly accessed

Code: Select all

[store_unit]
    [filter]
        type=Spearman
    [/filter]
    kill=#no/yes
    variable=spearman
[/store_unit]
{FOREACH spearman i}
    {VARIABLE spearman[$i].max_hitpoints 45}
{NEXT i}
:eng: Arbitrary tags ([Spearman] & co.) are not allowed. [item] is not needed to "scan" the unit.

If you want to directly access a variable including multiple unit types, you cannot separate them by types unless you do some fancy maneuvering. E.G.: ###Note: this assumes that the types are already stored.###

Code: Select all

{MODIFY_UNIT (
    type=Spearman
    find_in=types   ###name of variable###
    ) max_hitpoints 45}
I don't know where you got the idea of tags being created for names of variables, but it doesn't work that way.

Of course, the supremely easiest thing to do is this:

Code: Select all

{MODIFY_UNIT (type=Spearman) max_hitpoints 45}
But again, if you want to ONLY change the spearmen in your container variable, you would use:

Code: Select all

{MODIFY_UNIT (
    type=Spearman
    find_in=types   ###name of variable###
    ) max_hitpoints 45}
Check out my campaign Sweet Revenge!
Join the new R2D forum!
SlowThinker
Posts: 876
Joined: November 28th, 2008, 6:18 pm

Re: spaces in names of containers

Post by SlowThinker »

bigkahuna, sorry for the confusion:
I talk about an user-made container, not about a container acquired by [store_unit].
(i just replaced max_hitpoints by heal_cost in my code above)

This example describes what I need:
I need a list of unit types, and to store an attribute "heal_cost" with each type.
Once it is created, I need:
  • a direct access, i.e. for a given unit_type I need to get access to unit_type.heal_cost
  • ability to scan unit types, i.e. to be able to create a menu with all unit types in the list and their heal_cost values.
I can do this by the structure I posted above, but it works only if no unit name contains a space.
I work on Conquest Minus • I use DFoolWide, Retro Terrain Package and the add-on 'High Contrast Water'
I moved to Nosebane's corner (Doc Paterson's signature); I am spending my time there, so PM me if I don't answer your post in forums
User avatar
Dixie
Posts: 1757
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: spaces in names of containers

Post by Dixie »

I haven't tried it, but wouldn't this work? : my_container["Dwarvish Thunderguard"].hitpoints
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
User avatar
bigkahuna
Posts: 657
Joined: September 11th, 2010, 6:24 pm
Location: In your mind.

Re: spaces in names of containers

Post by bigkahuna »

Just out of curiosity, could you give me a link to the wiki section on how to make user-made tags/containers? I've always wondered how :hmm: Unless it is in Lua.
Check out my campaign Sweet Revenge!
Join the new R2D forum!
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: spaces in names of containers

Post by zookeeper »

SlowThinker wrote:
zookeeper wrote:Have you tried loading a savefile of a scenario using variables with spaces in their names? The last time I checked (which was quite a long time ago) you'll get some (non-fatal) errors.
You are right. The containers with spaces were kicked.

Do you have an idea how I can implement a list of unit types, which
  • can be scanned/searched
  • and simultaneously their elements can be directly accessed
?
Sounds like something you'd need Lua for. With WML, the best you can do is something like this:

Code: Select all

[heal_costs]
	[unit]
		id=Spearman
		heal_cost=25
	[/unit]
	[unit]
		id=Dwarvish Guardsman
		heal_cost=45
	[/unit]
	# ...
[/heal_costs]
Of course that means you have to search for the right container in a [while] loop everytime. With Lua you could probably make some sort of a table and then a custom WML tag which gives you quick access to it.
SlowThinker wrote:I could replace spaces by underscores and return them back, but does WML allow to manipulate chars in strings?
Sure. To get a space-less version of a unit type name into a variable, all you need is this (untested):

Code: Select all

[set_variables]
	name=unittypewordlist
	
	[split]
		list=stored_guardsman.type
		key=word
		separator=" "
	[/split]
[/set_variables]

# -> [unittypewordlist]
#        word=Dwarvish
#    [/unittypewordlist]
#    [unittypewordlist]
#        word=Guardsman
#    [/unittypewordlist]

[set_variable]
	name=typewithoutspaces
	
	[join]
		variable=unittypewordlist
		key=word
		separator=""
	[/join]
[/set_variable]

# -> typewithoutspaces="DwarvishGuardsman"
If you also need to convert the other way, then you should store the original string inside your array.
Mecha^Croc
Posts: 7
Joined: October 5th, 2006, 5:14 am

Re: spaces in names of containers

Post by Mecha^Croc »

It is 1 line of WML to lookup, if you use the macro from utils.cfg

Code: Select all

[set_variables]
  name=heal_costs
  [value]
    type="Dwarvish Guardsman"
    heal_cost=45
  [/value]
  [value]
    type="Spearman"
    heal_cost=25
  [/value]
[/set_variables]


{LOOKUP_VALUE heal_costs type "Dwarvish Guardsman" heal_cost $default_cost result}


{DEBUG_MSG "Dwarvish Guardsman has heal cost of $result"}
there is also LOOKUP_INDEX... you should know that, guy ;)
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: spaces in names of containers

Post by zookeeper »

Mecha^Croc wrote:It is 1 line of WML to lookup, if you use the macro from utils.cfg

Yes, and LOOKUP_INDEX is 20 lines of WML and LOOKUP_VALUE is 38 lines. It's still a [while] loop.
SlowThinker
Posts: 876
Joined: November 28th, 2008, 6:18 pm

Re: spaces in names of containers

Post by SlowThinker »

Dixie wrote:I haven't tried it, but wouldn't this work? : my_container["Dwarvish Thunderguard"].hitpoints
Of course not. Arrays may be indexed by integers only: my_container[5].hitpoints
A 5th sub-container has a sense, but a Thunderguard'th sub-container does not.
bigkahuna wrote:Just out of curiosity, could you give me a link to the wiki section on how to make user-made tags/containers? I've always wondered how :hmm: Unless it is in Lua.
By set_variables:
http://wiki.wesnoth.org/InternalActions ... riables.5D
With [value] you can manually insert values (with to_variable you assign to another container/array: BTW I think this is a situation where container and container[0] have a different effect)
Edit 1:Also [variables] should work, but I suppose it rewrites all existing variables.

Edit 2
i wrote a nonsense, deleted
Last edited by SlowThinker on December 16th, 2010, 11:57 am, edited 2 times in total.
I work on Conquest Minus • I use DFoolWide, Retro Terrain Package and the add-on 'High Contrast Water'
I moved to Nosebane's corner (Doc Paterson's signature); I am spending my time there, so PM me if I don't answer your post in forums
SlowThinker
Posts: 876
Joined: November 28th, 2008, 6:18 pm

Re: spaces in names of containers

Post by SlowThinker »

zookeeper wrote:Sounds like something you'd need Lua for.
I was referred to lua already, because recently I theoretically talked about a similar problem here. But I would have to learn lua, and also I want to understand wml abilities first.
Sure. To get a space-less version of a unit type name into a variable, all you need is this (untested):

Code: Select all

[set_variables]
	name=unittypewordlist
	[split]
        ...
Tested now, it works. Thank you. I will use this way because then I don't need to rewrite my code. (For "join" I will use "_" as a separator, beacuse then I can return back without an exigency to store the original value)

So a direct access to characters in a string (like string[5]) is impossible in a pure WML? (I don't count the possibility to split a string with all possible characters :) )

Code: Select all

[heal_costs]
	[unit]
		id=Spearman
		heal_cost=25
	[/unit]
Of course that means you have to search for the right container in a [while] loop everytime.
This way is somewhat painful, but I suppose it wouldn't slow down the code, because the interpreter must use a loop for a "direct" access to heal_costs.Spearman too.
This is why I don't understand the limitations for container names: theoretically any string that doesn't spoil the syntax could be allowed.
I work on Conquest Minus • I use DFoolWide, Retro Terrain Package and the add-on 'High Contrast Water'
I moved to Nosebane's corner (Doc Paterson's signature); I am spending my time there, so PM me if I don't answer your post in forums
SlowThinker
Posts: 876
Joined: November 28th, 2008, 6:18 pm

Re: spaces in names of containers

Post by SlowThinker »

SlowThinker wrote:This way is somewhat painful, but I suppose it wouldn't slow down the code, because the interpreter must use a loop for a "direct" access to heal_costs.Spearman too.
This is why I don't understand the limitations for container names: theoretically any string that doesn't spoil the syntax could be allowed.
A quality of my fastthinking is quite low.
The loop that must be interpreted is a lot slower than an internal loop (except all the loop is translated as a whole, but this is not possible with [insert tag])
"This is why" is wrong, as there is no relationship between both sentences.
I work on Conquest Minus • I use DFoolWide, Retro Terrain Package and the add-on 'High Contrast Water'
I moved to Nosebane's corner (Doc Paterson's signature); I am spending my time there, so PM me if I don't answer your post in forums
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: spaces in names of containers

Post by Sapient »

WML does not have associative arrays (i.e. "dictionaries").
LOOKUP_VALUE is the closest thing to it that is currently available. If you want a pure WML approach, I highly recommend that you just use LOOKUP_VALUE. In fact, this thread should be considered soft-locked because that is a definitive answer for your problem.

I also suggest you stick to the basics of making code that is clearly understood and works correctly, rather than worrying about optimizing your WML for efficiency. Premature optimization is well known to be a bad influence-- it leads to code that is unnecessarily ugly and verbose and does not address any real bottlenecks.
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
SlowThinker
Posts: 876
Joined: November 28th, 2008, 6:18 pm

Re: spaces in names of containers

Post by SlowThinker »

Sapient wrote:If you want a pure WML approach, I highly recommend that you just use LOOKUP_VALUE.
I forgot to mention this:
The problem of WML is there are situations where
  • you can use a direct variable value
    and simultaneously
  • you cannot use {LOOKUP_VALUE}
These situations are:
Inside a tag that shall be applied by [insert_tag]:
no {MACRO} may be used, and so no {LOOKUP_VALUE}
inside a declarational-type code:
no action-type code may be used, and so no {LOOKUP_VALUE}
I work on Conquest Minus • I use DFoolWide, Retro Terrain Package and the add-on 'High Contrast Water'
I moved to Nosebane's corner (Doc Paterson's signature); I am spending my time there, so PM me if I don't answer your post in forums
Post Reply