Ceres' Lua Problems

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Ceres
Forum Regular
Posts: 620
Joined: September 18th, 2010, 7:56 pm
Location: Germany

Ceres' Lua Problems

Post by Ceres »

One thing that bugged me using [floating_text], is that it's not possible to set any colour. So I just went ahead and modified the tag, so that you may set green, red or blue text colour. The problem is, when qouting the span color part, 'text' is no longer recognized as a variable. Is there any way to solve this?

Code: Select all

function wml_actions.floating_text(cfg)
	local locs = wesnoth.get_locations(cfg)
	local text = cfg.text or helper.wml_error("[floating_text] missing required text= attribute")
	local color = cfg.color
	if color == "red" then
	for i, loc in ipairs(locs) do
		wesnoth.float_label(loc[1], loc[2], "<span color='#ff0000'>text</span>")
	end
	elseif color == "green" then
	for i, loc in ipairs(locs) do
		wesnoth.float_label(loc[1], loc[2], "<span color='#00ff00'>text</span>")
	end
	elseif color == "blue" then
	for i, loc in ipairs(locs) do
		wesnoth.float_label(loc[1], loc[2], "<span color='#0000ff'>text</span>")
	end
	end
end
Last edited by Ceres on July 3rd, 2011, 6:33 pm, edited 1 time in total.
User avatar
Alarantalara
Art Contributor
Posts: 786
Joined: April 23rd, 2010, 8:17 pm
Location: Canada

Re: Making [floating_text] recognize color

Post by Alarantalara »

String concatenation of course:

Code: Select all

"<span color='#ff0000'>"..text.."</span>"
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Making [floating_text] recognize color

Post by Elvish_Hunter »

Ceres wrote:One thing that bugged me using [floating_text], is that it's not possible to set any colour.
You just need to use Pango markup, without modifying the tag:

Code: Select all

[floating_text]
    text= _ "<span color='red'>My text</span>"
    x,y=$unit.x,$unit.y
[/floating_text]
Alarantalara's solution is valid as well (if you still want to modify the tag), and can be done also with a string.format:

Code: Select all

string.format("<span color='#ff0000'>%s</span>", text)
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)
Ceres
Forum Regular
Posts: 620
Joined: September 18th, 2010, 7:56 pm
Location: Germany

Re: Making [floating_text] recognize color

Post by Ceres »

:doh: Such a simple solution. Thanks to both of you.
Ceres
Forum Regular
Posts: 620
Joined: September 18th, 2010, 7:56 pm
Location: Germany

Re: Ceres' Lua Problems

Post by Ceres »

Here we go with another one.
That code is supposed to get all units in a radius of 8 hexes around a hex, and move all of them one step towards this hex, if possible (they are pulled towards the centre of a circle).
Upon firing, I get this output: <Lua Error> [string "..."]:6: malformed number near '1..'
(Be warned, my Lua knowledge is based on a quick tutorial and reading code, so don't hit me if the mistake is just made up code, syntax mistakes and such.)
Here's the Lua snippet:

Code: Select all

[lua]
					code= <<
						local altar_victims = get_variable_array "altar_victims"
						local number = wesnoth.get_variable "range_counter.length"
						for i = 1, number, 1 do
							local altar_path = wesnoth.find_path (altar_victims[i], 22, 14, {ignore_units = false, viewing_side = 0})
							wesnoth.set_variable ("altar_goto["..i-1.."].x", altar_path[i].x)
							wesnoth.set_variable ("altar_goto["..i-1.."].y", altar_path[i].y)
							wesnoth.set_variable ("altar_goto["..i-1.."].id", altar_victims[i].id)
						end
					>>
				[/lua]
And here's the whole event including the Lua snippet, if anyone's interested:

Code: Select all

	[event]
		name=new turn
		first_time_only=no
		[filter_condition]
			[variable]
				name=altar_active
				equals=1
			[/variable]
		[/filter_condition]
		[set_variable]
			name=range_counter
			value=1
		[/set_variable]
		[while]
			[variable]
				name=range_counter
				less_than_equal_to=8
			[/variable]
			[do]
				[store_unit]
					[filter]
						side=1
						[filter_location]
							x,y=22,15
							radius=$range_counter
							[not]
								x,y=22,15
								radius="$($range_counter-1)"
							[/not]
						[/filter_location]
					[/filter]
					variable=altar_victims
				[/store_unit]
				[lua]
					code= <<
						local altar_victims = get_variable_array "altar_victims"
						local number = wesnoth.get_variable "range_counter.length"
						for i = 1, number, 1 do
							local altar_path = wesnoth.find_path (altar_victims[i], 22, 14, {ignore_units = false, viewing_side = 0})
							wesnoth.set_variable ("altar_goto["..i-1.."].x", altar_path[i].x)
							wesnoth.set_variable ("altar_goto["..i-1.."].y", altar_path[i].y)
							wesnoth.set_variable ("altar_goto["..i-1.."].id", altar_victims[i].id)
						end
					>>
				[/lua]
				{FOREACH altar_goto i}
					[if]
						[not]
							[have_unit]
								x,y=$altar_goto[i].x,$altar_goto[i].y
							[/have_unit]
						[/not]
						[then]
							[move_unit]
								id=$altar_goto[i].id
								to_x=$altar_goto[i].x
								to_y=$altar_goto[i].y
							[/move_unit]
						[/then]
					[/if]
				{NEXT i}
				[clear_variable]
					name=altar_victims
				[/clear_variable]
				[set_variable]
					namr=range_counter
					add=1
				[/set_variable]
			[/do]
		[/while]
	[/event]
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Ceres' Lua Problems

Post by 8680 »

For one thing, it's [u]helper[/u].get_variable_array, and you'll need to load the helper library to use it.
User avatar
Dixie
Posts: 1757
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: Ceres' Lua Problems

Post by Dixie »

Well, IIRC it depends which variable you load the helper library, but as a convention, that variable is generally named helper (it's simpler). If you wanted, you could name it anything, however.
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Ceres' Lua Problems

Post by 8680 »

I was referring to it by the name used in the documentation. I rarely use those names in practice, typically aliasing them to something shorter, like gvar() instead of wesnoth.get_variable().
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Ceres' Lua Problems

Post by Elvish_Hunter »

Ceres wrote:Upon firing, I get this output: <Lua Error> [string "..."]:6: malformed number near '1..'
Well, let's see what happens at the Lua interactive prompt when attempting to concatenate numbers and strings:

Code: Select all

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> return "test" .. 12 .. "try"
test12try
> return "test"..12.."try"
stdin:1: malformed number near '12..'
> return "test"..12
test12
> return 12.."try"
stdin:1: malformed number near '12..'
> return 12 .. "try"
12try
Adding one space before an one space after the concatenation operator should solve the issue. You may want also to consider use of string.format:

Code: Select all

wesnoth.set_variable ( string.format( "altar_goto[%d].x", i-1 ), altar_path[i].x )
Finally, range.counter is a scalar variable:

Code: Select all

      [set_variable]
         name=range_counter
         value=1
      [/set_variable]
            [set_variable]
               namr=range_counter
               add=1
            [/set_variable]
Why are you attempting to get is length here? :?

Code: Select all

local number = wesnoth.get_variable "range_counter.length"
Notice also the "namr" in the latest set_variable. :P
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)
Ceres
Forum Regular
Posts: 620
Joined: September 18th, 2010, 7:56 pm
Location: Germany

Re: Ceres' Lua Problems

Post by Ceres »

Thanks everyone, I fixed these mistakes and some additional ones caused by an error in reasoning.
Now the engine complains "bad argument #1 to 'find_path' (expected number, got table)".
Lua code

Code: Select all

[lua]
							code= <<
								local helper = wesnoth.require "lua/helper.lua"
								local altar_victims = helper.get_variable_array "altar_victims"
								local number = wesnoth.get_variable "altar_victims.length"
								for i = 1, number, 1 do
									local altar_path = wesnoth.find_path (altar_victims[i], 22, 14, {ignore_units = false, viewing_side = 0})
									wesnoth.set_variable ("altar_goto[" .. i-1 .. "].x", altar_path[i].x)
									wesnoth.set_variable ("altar_goto[" .. i-1 .. "].y", altar_path[i].y)
									wesnoth.set_variable ("altar_goto[" .. i-1 .. "].id", altar_victims[i].id)
								end
							>>
						[/lua]
whole code

Code: Select all

	[event]
		name=new turn
		first_time_only=no
		[filter_condition]
			[variable]
				name=altar_active
				equals=1
			[/variable]
		[/filter_condition]
		[set_variable]
			name=range_counter
			value=1
		[/set_variable]
		[while]
			[variable]
				name=range_counter
				less_than_equal_to=8
			[/variable]
			[do]
				[store_unit]
					[filter]
						side=1
						[not]
							role=altar_moved
						[/not]
						[filter_location]
							x,y=22,15
							radius=$range_counter
						[/filter_location]
					[/filter]
					variable=altar_victims
				[/store_unit]
				{DEBUG_MSG "$range_counter"}
				{DEBUG_MSG "$altar_victims.id"}
				[if]
					[variable]
						name=altar_victims.length
						greater_than_equal_to=1
					[/variable]
					[then]
						[lua]
							code= <<
								local helper = wesnoth.require "lua/helper.lua"
								local altar_victims = helper.get_variable_array "altar_victims"
								local number = wesnoth.get_variable "altar_victims.length"
								for i = 1, number, 1 do
									local altar_path = wesnoth.find_path (altar_victims[i], 22, 14, {ignore_units = false, viewing_side = 0})
									wesnoth.set_variable ("altar_goto[" .. i-1 .. "].x", altar_path[i].x)
									wesnoth.set_variable ("altar_goto[" .. i-1 .. "].y", altar_path[i].y)
									wesnoth.set_variable ("altar_goto[" .. i-1 .. "].id", altar_victims[i].id)
								end
							>>
						[/lua]
						{FOREACH altar_goto i}
							[if]
								[not]
									[have_unit]
										x,y=$altar_goto[$i].x,$altar_goto[$i].y
									[/have_unit]
								[/not]
								[then]
									[move_unit]
										id=$altar_goto[$i].id
										to_x=$altar_goto[$i].x
										to_y=$altar_goto[$i].y
									[/move_unit]
								[/then]
							[/if]
							[modify_unit]
								[filter]
									id=$altar_goto[$i].id
								[/filter]
								role=altar_moved
							[/modify_unit]
						{NEXT i}
					[/then]
				[/if]
				[set_variable]
					name=range_counter
					add=1
				[/set_variable]
				[clear_variable]
					name=altar_victims
				[/clear_variable]
			[/do]
		[/while]
	[/event]
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Ceres' Lua Problems

Post by 8680 »

wesnoth.find_unit expects either a proxy unit or a pair of coordinates as its first argument; altar_victims[i] is a cfg dump, not a proxy unit. Change wesnoth.find_unit(altar_victims[i], ...) to wesnoth.find_unit(altar_victims[i].x, altar_victims[i].y, ...).
Ceres
Forum Regular
Posts: 620
Joined: September 18th, 2010, 7:56 pm
Location: Germany

Re: Ceres' Lua Problems

Post by Ceres »

Thanks 8680 (though I suspect you mean find_path, right?).
Almost there, but only almost: while altar_goto.id is set correctly, altar_goto.x and .y aren't. Why? How are the path coordinates output?
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Ceres' Lua Problems

Post by 8680 »

I reread your code (at least the Lua), and I don't see why you're using wesnoth.find_path at all; it returns a path, like what [move_unit] takes (not quite the same, I don't think...); if you already know the destination coordinates, just set altar_goto[i].x and .y to them directly. Actually, if the destination is constant, why use a variable at all?
Ceres wrote:(though I suspect you mean find_path, right?)
:doh: Yes, yes I did.
Ceres
Forum Regular
Posts: 620
Joined: September 18th, 2010, 7:56 pm
Location: Germany

Re: Ceres' Lua Problems

Post by Ceres »

They're supposed to go only one step towards this point.
(I could try to set moves of all units in range to 1, then set the goto, and after they moved reset the mp, but now I want to finish this shiny code I started with)

So how to extract the coordinates of the first hex of the path?
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Ceres' Lua Problems

Post by 8680 »

Try 1 instead of i as the index of altar_path in wesnoth.set_variable().
Post Reply