lua placing/removing Labels

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
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

lua placing/removing Labels

Post by enclave »

Sorry to take your time, I have googled hard before writting here.. (Wesnoth version 1.10.7)

Anyone could tell me the lua codes for adding and removing labels? (partuicularily team labels)
if you have full information please include also lua commands to change label from "show to team only" into "show to everyone"
{REMOVE_LABEL $x1 $y1} doesnt seem to remove labels intended for allies view only. Please help :)

Here is my problem:
1) During playing game, I place ally label somewhere.. lets say "piu"
2) Then I plan to build a village there and I specify name
2.1) it is done in New Settlers era, via following WML script:
[label]
x=$x1
y=$y1
text=$memorize
visible_in_fog=no
[/label]
3) The result is that (during game) I still see my "show to allies only" label "piu"
4) then I can right click (during game), rename label to "" (empty string)
4.1) if I rename in into empty string then ally label disappears and now I can see my $memorize label.
4.2) if I untick "show to team only" then my label "piu" becomes my team color and is "immutable" (can not be cleared or renamed)
(edited: I think after playing around I found that next turn the label becomes from "piu" to $memorize again anyway.. not 100% sure though)

Clearing label with {REMOVE_LABEL $x1 $y1} before [label] does nothing.. tried many other things, nothing helps.
Only hope if somebody knows lua command to remove allied labels.. since it works with "clear labels" or if renamed to empty string then there must be way to clear it..

Thank you!
Last edited by enclave on March 18th, 2015, 2:57 pm, edited 1 time in total.
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: lua placing/removing Labels

Post by Elvish_Hunter »

enclave wrote:Anyone could tell me the lua codes for adding and removing labels? (partuicularily team labels)
Lua won't be useful for that, because you'll have to call the wesnoth.wml_actions.label function, which is exactly like using [label]. In fact, placing labels in Lua is done in this way:

Code: Select all

wesnoth.wml_actions.label {
	x = 7,
	y = 7,
	text = "Totally not a label"
}
wesnoth.wml_actions is just a table containing all the WML action tags, and that includes [label] too. So, your problem should be solved with WML. :eng:
enclave wrote:Clearing label with {REMOVE_LABEL $x1 $y1} before [label] does nothing.. tried many other things, nothing helps.
REMOVE_LABEL deletes the labels by just setting their text to "" (empty string).
enclave wrote:Only hope if somebody knows lua command to remove allied labels.. since it works with "clear labels" or if renamed to empty string then there must be way to clear it..
Our wiki says that [label] has a team_name= key, which can be used to make the label visible only to one team. Perhaps you can try adding it to your [label] tag. Let me know if this works :)
Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: lua placing/removing Labels

Post by enclave »

Thank you so much Elvish_Hunter for all your help.

I will try and will let you know if anything solved my problem.

Your lua labeling will be a great help for me anyway! (I'm only beginning with "Lua" now and its very hard at the moment :) every little helps)
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: lua placing/removing Labels

Post by enclave »

Ok, so you were right, team_name was able to resolve my problem with labels.

Here is how:

Code: Select all

## first i used {remove_label} to remove any non-team label on map..
{REMOVE_LABEL $x1 $y1}
## then i used [label] which is working same way as rename label.. basically [label] will rename previously named label.. 
[label]
	x=$x1
	y=$y1
## to remove label we just need to make it's text equal to nothing text=""
	text=""
## to make sure we remove label of player 1 we would need his team_name.. which by default would be just "1" 
##(took me at least an hour to figure out a team name, since its not documented.. used [store_side] to find out)
## so in my case I just used $side_number and it works in my particular case. I assume will work in most cases.
	team_name=$side_number
## i put the keys below just in case.. i guess they are not needed.
	visible_in_fog=yes
	immutable=no
[/label]
## then my code on top of already removed label.. 
[label]
	x=$x1
	y=$y1
	text=$memorize
	##color=green
	visible_in_fog=no
[/label]
It still doesnt work for games with teams.. where player 1 and player 2 would be in same team1 team_name=1
$side_number will not work for teams.
If anybody wants to make sure you want to delete any label I have only 1 idea, you have to use the following code (please check for basic mistakes, never tried if it works) and try it (if team names are default "1", "2" etc:

Code: Select all

{REMOVE_LABEL $x1 $y1}
	[while]
		[variable]
			name=i_team_number
			less_than=10
		[/variable]
	[do]
	{VARIABLE_OP i_team_number add 1}
[label]
	x=$x1
	y=$y1
	text=""
	team_name=$i_team_number
	visible_in_fog=yes
	immutable=no
[/label]
	[/do]
	[/while]
It would remove any labels for teams 1-9 at coordinates $x1 $y1.. if their names are default.. othwerwise I dont know. Hope it will help at least somebody.
Last edited by Elvish_Hunter on March 18th, 2015, 8:49 pm, edited 1 time in total.
Reason: Added [code] tags
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: lua placing/removing Labels

Post by Elvish_Hunter »

Since the problem wasn't Lua related, after all, I moved the topic to the WML Workshop.
Also, when posting code, the next time please use the [ code ] tags, otherwise without indentation it may become very hard to read. But for now, I added them for you.
enclave wrote:Ok, so you were right, team_name was able to resolve my problem with labels.
Glad to have been of service :)
Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: lua placing/removing Labels

Post by enclave »

Thank you, will do in future. But you actually wrote lua code to place labels there.. so it was same related to lua as to wml.. anyway most important I could find this in google. Will be useful to somebody some day. Thanks again for your help very much!
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: lua placing/removing Labels

Post by Elvish_Hunter »

enclave wrote:But you actually wrote lua code to place labels there.. so it was same related to lua as to wml..
That's one of the issues with moderation: some times, it isn't easy to decide where a thread belongs. But in this case, there is a thing that made me move it. Check my chunk of Lua code:

Code: Select all

wesnoth.wml_actions.label {
   x = 7,
   y = 7,
   text = "Totally not a label"
}
and compare it with the WML version:

Code: Select all

[label]
   x=7
   y=7
   text= _ "Totally not a label"
[/label]
Do you notice any similarities? That's because the Lua code, in this case, was just calling the WML tag - nothing more, nothing less. That's why I said that the problem "wasn't Lua related, after all": because I simply called a WML tag from Lua. :eng:
enclave wrote:Will be useful to somebody some day.
Another reason to move it is that most UMC authors don't use Lua, and your solution was written entirely in WML: in this situation, for the other authors it'll be easier to find it in the WML Workshop. ;)
Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: lua placing/removing Labels

Post by enclave »

yeah i completely agree with everything you said above :)

this post is still "googlable".. for some reason I'm mostly looking for things relating to undocumented wml through the google for some reason, so whoever looking for lua labeling will find it.

Just to add a little bit,
im only thinking that lua is a lot harder.. and every little bit helps a lot for those who only begins with it.. like me.

From your post I understood that any wml tag can be called if you use wesnoth.wml_actions
I guess if i wanted to do:

Code: Select all

[message]
image=units/human-loyalists/peasant.png
speaker=narrator
side_for=1
caption="not a caption"
message="not a message at all"
[/message]
I could just do

Code: Select all

wesnoth.wml_actions.message {
image="units/human-loyalists/peasant.png",
speaker=narrator,
side_for=1,
caption="not a caption",
message="not a message at all"
}
which i never knew before you mentioned it.. is it right? or i am wrong with code above?
lua labeling allows you to use labels without converting variables from lua into wml.. it may save some code strings I believe..
I have no much idea how to use lua variables properly, why local, why global.... and regarding arrays I completely failed... but I'm only beginning..
and so far I done everything I really wanted with lua.. so I guess im succeeding :) and every little post like yours helps a lot.
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: lua placing/removing Labels

Post by Elvish_Hunter »

Sorry for the delay in answering.
enclave wrote:Just to add a little bit,
im only thinking that lua is a lot harder.. and every little bit helps a lot for those who only begins with it.. like me.

From your post I understood that any wml tag can be called if you use wesnoth.wml_actions
...
which i never knew before you mentioned it.. is it right? or i am wrong with code above?
lua labeling allows you to use labels without converting variables from lua into wml.. it may save some code strings I believe..
I have no much idea how to use lua variables properly, why local, why global.... and regarding arrays I completely failed... but I'm only beginning..
and so far I done everything I really wanted with lua.. so I guess im succeeding :) and every little post like yours helps a lot.
From your post, I can see that you really want to learn Lua and that you have a lot of questions. That's good :) I was thinking that maybe you can start your own topic in the Lua Labs forum, call it "enclave's Lua thread" or something to that effect and post all your Lua questions there. In this way, you'll have all your answers in a single place, without needing to google for them.
enclave wrote:

Code: Select all

wesnoth.wml_actions.message {
image="units/human-loyalists/peasant.png",
speaker=narrator,
side_for=1,
caption="not a caption",
message="not a message at all"
}
which i never knew before you mentioned it.. is it right? or i am wrong with code above?
How can I say it... you're both right and wrong :P The general structure is fine. However, narrator should be a string (that means: wrapped between quotes, otherwise it's just a variable name); also, the values for "not a caption" and "not a message at all" are missing the translation marker, which is still an underscore like in WML, but before you use it you need to call the wesnoth.textdomain function and add a textdomain comment before or after said call. But we're going quite off-topic, so I'll wait for your Lua thread, OK? ;)
Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
Post Reply