[SOLVED] select hex

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
Adamant14
Posts: 968
Joined: April 24th, 2010, 1:14 pm

[SOLVED] select hex

Post by Adamant14 »

Is there a way to trigger a message when the player selects a certain hex (e.g. x,y= 33,44 ) ?
something like:

Code: Select all

		[event]
			name=select
			[filter]
				x,y=33,44
			[/filter]
			[message]
				speaker=narrator
				message= _ "Test."
				image=wesnoth-icon.png
			[/message]
		[/event]
EDIT: removed the second [/event] tag
Last edited by Adamant14 on October 27th, 2018, 6:37 pm, edited 2 times in total.
Author of Antar, Son of Rheor ( SP Campaign) | Development Thread + Feedback Thread + Replays of ASoR
User avatar
Pentarctagon
Project Manager
Posts: 5564
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: select hex

Post by Pentarctagon »

It looks like that's part of a nested event? Otherwise you've got an extra [/event] there.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Ravana
Forum Moderator
Posts: 3000
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: select hex

Post by Ravana »

[filter] is about unit. For detecting selection of nonunit hex you might need Lua.
User avatar
Adamant14
Posts: 968
Joined: April 24th, 2010, 1:14 pm

Re: select hex

Post by Adamant14 »

Pentarctagon wrote: October 27th, 2018, 10:49 am It looks like that's part of a nested event? Otherwise you've got an extra [/event] there.
That is right, the piece of code is used inside of a nested event.
Ravana wrote: October 27th, 2018, 10:54 am [filter] is about unit. For detecting selection of nonunit hex you might need Lua.
LUA again. Sadly I still have no clue about using LUA :augh:
Author of Antar, Son of Rheor ( SP Campaign) | Development Thread + Feedback Thread + Replays of ASoR
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: select hex

Post by enclave »

Spoiler:
I was about to say that I have some clue about lua, but your idea is impossible... because there is no such event "on select hex" no matter its lua or not..

my second thought was actually yeah its possible... i will write a code for him... but I realized it might not be what you want and I will spend some time in vain.. because I never done it before I'm not sure which way it will work.. it might be quite not what you want or work not like I think it will work..

Anyway.. I think I will try to write it for you.. I don't know how far it will take me.. and if i manage at all..

before I do it, there is only 1 simple thing u need to know about lua... to use it in wml, all you have to do is write lua tags, some brackets and you can use lua code:

Code: Select all

[lua]
code = <<
------------ here you can use lua functions for example wesnoth.message("Hello world") would be equal to [chat] message="Hello world" [/chat]
----- these dashes mean comments... anything I write after dashes won't be executed. same as # sign for WML.
>>
[/lua]
So it's actually very simple.. I recommend you to look at it sometimes... but the select thing you want is too advanced for a total lua newbie.. dont even try..

You don't need lua in 99% of cases.. you just don't need it.. Only time you need lua is (a) when you want to optimize your code to run faster (your FOREACH loop is slowing down the game somewhere... WML loops may be slow and make your game lag in huge calculations. If you dont have lags or slow downs then you dont need lua.. and it's 99% time..) and then (b) when there is no WML alternative for the function... it's also rare... WML has most things any new add-on creator may need... 99% of time u can use WML alternative..

Now some kind of representation of your code... don't try to understand it.. it has a mix of 2 or 3 worlds there.. to understand it you would need to understand both wesnoth lua, other lua functions and lua theme items.. which I guess you dont yet:

Code: Select all

[event]
name=preload
first_time_only=no
{VARIABLE selected_tile.x 0}
{VARIABLE selected_tile.y 0}
[lua]
code=<<
local old_display_terrain = wesnoth.theme_items.terrain
wesnoth.theme_items.terrain = function()
local oldvalue = old_display_terrain()
--
local lua_selected_tile_x = tonumber(wesnoth.get_variable("selected_tile[0].x"))
local lua_selected_tile_y = tonumber(wesnoth.get_variable("selected_tile[0].y"))
local new_hex_selected = false
--
local x, y = wesnoth.get_selected_tile()
if x ~= nil then
if y ~= nil then
	if x ~= lua_selected_tile_x then 
		new_hex_selected = true
	end
	if y ~= lua_selected_tile_y then 
		new_hex_selected = true
	end
	if new_hex_selected == true then
		wesnoth.message("You selected hex with coordinates ("..tostring(x)..","..tostring(y)..")")
		wesnoth.set_variable("selected_tile.x",tostring(x))
		wesnoth.set_variable("selected_tile.y",tostring(y))
	end
end
end
  return oldvalue
end
>>
[/lua]
[/event]
The code above will fire message on select of any unit (unlike "select" event which only works on your own units... not enemy ones..)

The code below will fire message on moving your mouse over any hex:

Code: Select all

[event]
name=preload
first_time_only=no
{VARIABLE selected_tile.x 0}
{VARIABLE selected_tile.y 0}
## on select tile message
[lua]
code=<<
local old_display_terrain = wesnoth.theme_items.terrain
wesnoth.theme_items.terrain = function()
local oldvalue = old_display_terrain()
--
local lua_selected_tile_x = tonumber(wesnoth.get_variable("selected_tile[0].x"))
local lua_selected_tile_y = tonumber(wesnoth.get_variable("selected_tile[0].y"))
local new_hex_selected = false
--
local old_position = wesnoth.theme_items.position()
 local position_value = helper.get_child(old_position, "element")
 local x_coordinate = " "
 local y_coordinate = ""
 
if position_value then
local position_string = position_value.text
if position_string then 
x_coordinate = ""
 for k=1,string.len(position_string),1 do
	if string.sub(position_string,k,k) ~= "," then
		x_coordinate = x_coordinate..string.sub(position_string,k,k)
	else
		for m=k+1,string.len(position_string),1 do
			if string.sub(position_string,m,m) ~= " " then
				y_coordinate = y_coordinate..string.sub(position_string,m,m)
			else
				break
			end
		end
		break
	end
 end
end
end

local x=x_coordinate
local y=y_coordinate

if x ~= " " then
	if x ~= tostring(lua_selected_tile_x) then 
		new_hex_selected = true
	end
	if y ~= tostring(lua_selected_tile_y) then 
		new_hex_selected = true
	end
	if new_hex_selected == true then
		wesnoth.message("You moved your mouse over hex with coordinates ("..tostring(x)..","..tostring(y)..")")
		wesnoth.set_variable("selected_tile.x",tostring(x))
		wesnoth.set_variable("selected_tile.y",tostring(y))
	end
end
  return oldvalue
end
>>
[/lua]
[/event]
Both ways are not gona work to fire some message on click of some hex that don't contain a unit.. there is no "on click hex" event in wesnoth.. unfortunately..

If you read a lot of non-wesnoth lua tutorials you might find the way to execute on click event.. in this case you could combine it with on mouse over event above.. and make it work for you. I unfortunately do not possess the knowledge to help you. I think I once tried, but failed.. I wish i knew how to execute anything on click of mouse in wesnoth.. but i dont..

if any of the events/codes above work for you then just add your "if" x=33... then... if y=44 then... or ask me how to.. i might have time to help.
Best of luck...

PS. even if you do find the on click event from external source.. you probably wont be able to apply it smoothly.. wesnoth will not know the difference between clicking the hex or clicking the menu... so... it wont work really..
User avatar
Adamant14
Posts: 968
Joined: April 24th, 2010, 1:14 pm

Re: select hex

Post by Adamant14 »

I never thought that it is so complicated. :doh:

I tested both codes:

The first works, but only with a unit selected, and on any hex, not only on the hex the message should trigger. In other words, the message tells me every move the unit makes, on which hex the unit is.

The second does nothing, except giving an error message:

Code: Select all

20181027 17:01:37 error scripting/lua: [string "..."]:11: variable 'helper' must be assigned before being used
stack traceback:
	[C]: in function '.error'
	lua/ilua.lua:131: in metamethod '__index'
	[string "..."]:11: in function <[string "..."]:3>
	
But do no longer spend time on that issue, it seems way to much work, and that event is not really important for my campaign.
Thank you for your time.
Author of Antar, Son of Rheor ( SP Campaign) | Development Thread + Feedback Thread + Replays of ASoR
User avatar
Ravana
Forum Moderator
Posts: 3000
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: select hex

Post by Ravana »

There is on click hex event, I used that when trying to implement query_location. (wesnoth.game_events.on_mouse_action and you find entire code by searching query_location)
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: select hex

Post by enclave »

Spoiler:
Sorry Adamant, i forgot that helper thing.. because i already have it..
just include this event somewhere and error will disappear:

Code: Select all

[event]
name=preload
first_time_only=no
[lua] 
code = <<
helper = wesnoth.require "lua/helper.lua"
T = helper.set_wml_tag_metatable {}
>>
[/lua]
[/event]
for the first one if you modify this string wesnoth.message("You selected hex with coordinates ("..tostring(x)..","..tostring(y)..")")
put insted the following code:

Code: Select all

if x==33 then
   if y==44 then
      wesnoth.message("You selected hex with coordinates (33,44) and this message appears...")
   end
end
I understand that u probably dont want to mess with lua, so you can replace:
wesnoth.message... with wesnoth.fire_event("my_custom_event_name")
in this case if you create your custom event with [event] name=my_custom_event_name first_time_only=no [/event]
then you could use WML inside of it.. just like normal...

Code: Select all

[event] 
name=my_custom_event_name
 first_time_only=no
[message]
message="You selected coordinate (33,44)"
[/message]
 [/event] 
lua will just trigger your WML event...
Ravana wrote: October 27th, 2018, 4:31 pm There is on click hex event, I used that when trying to implement query_location. (wesnoth.game_events.on_mouse_action and you find entire code by searching query_location)
Thanks Ravana, i somehow missed it... i will look documentation.. in this case I might rewrite the code for adamant, if it makes him happier :)

Ok Adamant, thanks to Ravana the code will be much easier for you :)
Here we go (a basic example of how it works):

Code: Select all

[event]
	name=preload
	first_time_only=no
	[lua]
		code = <<
			function wesnoth.game_events.on_mouse_action(x,y)       	
				wesnoth.message("You clicked your mouse on hex with coordinates ("..tostring(x)..","..tostring(y)..")")
			return {}
			end
		>>
	[/lua]
[/event]
And the code that you could use for your mod here, put whatever IFs inside your WML event to change behavior...

Code: Select all

[event]
	name=preload
	first_time_only=no
	[lua]
		code = <<
			function wesnoth.game_events.on_mouse_action(x,y) 
				wesnoth.set_variable("my_mouse_click_event_x",tostring(x))
				wesnoth.set_variable("my_mouse_click_event_y",tostring(y))      	
				wesnoth.fire_event("my_mouse_click_event")
			return {}
			end
		>>
	[/lua]
[/event]

[event]
name=my_mouse_click_event
first_time_only=no
[if]
[variable]
name=my_mouse_click_event_x
equals=33                                                     ## CHANGE TO WHATEVER YOU LIKE HERE the X of HEX
[/variable]
[and]
[variable]
name=my_mouse_click_event_y
equals=44                                                     ## CHANGE TO WHATEVER YOU LIKE HERE the Y of HEX
[/variable]
[/and]
[then]
                            ## PUT WHATEVER YOU LIKE HERE instead of MESSAGE tag, use your WML skills to the limits..
                            [message]
                             speaker="narrator"
                             message="You clicked on hex 33,44 and now you are going to suffer and look at this message"
                            [/message]
[/then]
[/if]
[/event]
Let me know if something doesnt work... i have tested and it works for me. wohooo.. stronger together!
User avatar
Adamant14
Posts: 968
Joined: April 24th, 2010, 1:14 pm

Re: select hex

Post by Adamant14 »

I changed the code, now the error message no longer appears, but the code does not work as intended - nothing happens.

Here is the changed code (for testing I use x,y=10,10 instead of x,y=33,44):

Code: Select all

	[event]
		name=preload
		first_time_only=no



		[lua] 
		code = <<
		helper = wesnoth.require "lua/helper.lua"
		T = helper.set_wml_tag_metatable {}
		>>
		[/lua]



		{VARIABLE selected_tile.x 0}
		{VARIABLE selected_tile.y 0}
		## on select tile message
		[lua]
		code=<<
		local old_display_terrain = wesnoth.theme_items.terrain
		wesnoth.theme_items.terrain = function()
		local oldvalue = old_display_terrain()
		--
		local lua_selected_tile_x = tonumber(wesnoth.get_variable("selected_tile[0].x"))
		local lua_selected_tile_y = tonumber(wesnoth.get_variable("selected_tile[0].y"))
		local new_hex_selected = false
		--
		local old_position = wesnoth.theme_items.position()
			local position_value = helper.get_child(old_position, "element")
			local x_coordinate = " "
			local y_coordinate = ""
		
		if position_value then
		local position_string = position_value.text
		if position_string then 
		x_coordinate = ""
		 for k=1,string.len(position_string),1 do
			if string.sub(position_string,k,k) ~= "," then
				x_coordinate = x_coordinate..string.sub(position_string,k,k)
			else
				for m=k+1,string.len(position_string),1 do
					if string.sub(position_string,m,m) ~= " " then
						y_coordinate = y_coordinate..string.sub(position_string,m,m)
					else
						break
					end
				end
				break
			end
		 end
		end
		end

		local x=x_coordinate
		local y=y_coordinate

		if x ~= " " then
			if x ~= tostring(lua_selected_tile_x) then 
				new_hex_selected = true
			end
			if y ~= tostring(lua_selected_tile_y) then 
				new_hex_selected = true
			end
			if new_hex_selected == true then
				if x==10 then
					if y==10 then
						wesnoth.message("You selected hex with coordinates (10,10) and this message appears...")
					end
				end
				wesnoth.set_variable("selected_tile.x",tostring(x))
				wesnoth.set_variable("selected_tile.y",tostring(y))
			end
		end
			return oldvalue
		end
		>>
		[/lua]
	[/event]
Author of Antar, Son of Rheor ( SP Campaign) | Development Thread + Feedback Thread + Replays of ASoR
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: select hex

Post by enclave »

Adamant14 wrote: October 27th, 2018, 6:19 pm I changed the code, now the error message no longer appears, but the code does not work as intended - nothing happens.
Look previous message I edited, there is a working code for just what you need in the end of it.

For the code that you modified, it's hard to explain but x, y are strings there... I could change it but just dont want to overcomplicate things for you.. so you have to try either of 3:

Code: Select all

if x=="10" then
					if y=="10" then
						wesnoth.message("You selected hex with coordinates (10,10) and this message appears...")
					end
				end
or

Code: Select all

if x==tostring(10) then
					if y==tostring(10) then
						wesnoth.message("You selected hex with coordinates (10,10) and this message appears...")
					end
				end
or

Code: Select all

if tonumber(x)==10 then
					if tonumber(y)==10 then
						wesnoth.message("You selected hex with coordinates (10,10) and this message appears...")
					end
				end
tostring() or tonumber() converts between number and string.. or string and integer.. whatever you prefer :) i dont know if you have any previous programming skills or you started from WML and programming is still a bit allien for you ;)
without these conversion it is trying to compare number to a string.. and it doesnt work out..

not sure if that was the problem though, just quessing

IMPORTANT: in my first example (where you select unit to get message, the x was a number/integer, so your code would work), but in my second example where you mouseover any hex and get message realtime as you move your mouse the x was already a string (so there you need to convert it), it is a string because I had to mess with strings before to get the x, y values... i won't be able to explain in details, you would need to know how lua theme items work.. and i don't think you want to know all that useless information ;) you might come back to it some day.. for now it would probably be an excess of information..
User avatar
Adamant14
Posts: 968
Joined: April 24th, 2010, 1:14 pm

Re: select hex

Post by Adamant14 »

enclave wrote: October 27th, 2018, 6:24 pm
Look previous message I edited, there is a working code for just what you need in the end of it.
Oh, I must have missed the edit.

The new code works perfect, now I am really happy. :D

I thank you Enclave, and Ravana too
Author of Antar, Son of Rheor ( SP Campaign) | Development Thread + Feedback Thread + Replays of ASoR
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: [SOLVED] select hex

Post by enclave »

you are very welcome, I'm glad i found some time for it :D and it worked..
y unfortunately the edits are very easy to miss, but moderators don't understand it.. their point of view is not to make 100 messages but modify 1 message 100 times.. and result is lack of communication.. I don't know if it saves moderator's time or server's space or what :) such is life :) good that we have a forum;) so rules are the rules :)
User avatar
Ravana
Forum Moderator
Posts: 3000
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: select hex

Post by Ravana »

enclave wrote: October 27th, 2018, 5:35 pm
Ok Adamant, thanks to Ravana the code will be much easier for you :)
Here we go (a basic example of how it works):

Code: Select all

[event]
	name=preload
	first_time_only=no
	[lua]
		code = <<
			function wesnoth.game_events.on_mouse_action(x,y)       	
				wesnoth.message("You clicked your mouse on hex with coordinates ("..tostring(x)..","..tostring(y)..")")
			return {}
			end
		>>
	[/lua]
[/event]
Let me know if something doesnt work... i have tested and it works for me. wohooo.. stronger together!
It is supposed to not work, you need the call the original wesnoth.game_events.on_mouse_action(), otherwise it should eat all clicks and disable attacking for example.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: select hex

Post by enclave »

Ravana wrote: October 27th, 2018, 8:40 pm It is supposed to not work, you need the call the original wesnoth.game_events.on_mouse_action(), otherwise it should eat all clicks and disable attacking for example.
Well maybe you are right.. when i was reading on it I couldnt make it work at all.. only worked after some 10s of tries of different ways of putting things.. otherwise errored on me... like x, y nil or x y not initialized etc etc..
Could you correct the code and show how it's supposed to work without eating anything? I was reading your links and wiki and it didn't give me a working example.. or i failed to understand it somehow.
User avatar
Adamant14
Posts: 968
Joined: April 24th, 2010, 1:14 pm

Re: [SOLVED] select hex

Post by Adamant14 »

For me the code seems to work, I haven't noticed any problems. (though I haven't had much time to do a lot of playtesting today, maybe tomorrow I know more)
Author of Antar, Son of Rheor ( SP Campaign) | Development Thread + Feedback Thread + Replays of ASoR
Post Reply