[SOLVED] select hex
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.
[SOLVED] select hex
Is there a way to trigger a message when the player selects a certain hex (e.g. x,y= 33,44 ) ?
something like:
EDIT: removed the second [/event] tag
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]
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
- Pentarctagon
- Project Manager
- Posts: 5564
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: select hex
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: select hex
[filter] is about unit. For detecting selection of nonunit hex you might need Lua.
Re: select hex
That is right, the piece of code is used inside of a nested event.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.
LUA again. Sadly I still have no clue about using LUA
Author of Antar, Son of Rheor ( SP Campaign) | Development Thread + Feedback Thread + Replays of ASoR
Re: select hex
Spoiler:
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]
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 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]
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..
Re: select hex
I never thought that it is so complicated.
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:
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.
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>
Thank you for your time.
Author of Antar, Son of Rheor ( SP Campaign) | Development Thread + Feedback Thread + Replays of ASoR
Re: select hex
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)
Re: select hex
Spoiler:
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]
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
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]
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]
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]
Re: select hex
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):
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
Re: select hex
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
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
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
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..
Re: select hex
Oh, I must have missed the edit.
The new code works perfect, now I am really happy.
I thank you Enclave, and Ravana too
Author of Antar, Son of Rheor ( SP Campaign) | Development Thread + Feedback Thread + Replays of ASoR
Re: [SOLVED] select hex
you are very welcome, I'm glad i found some time for it 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
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
Re: select hex
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 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):Let me know if something doesnt work... i have tested and it works for me. wohooo.. stronger together!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]
Re: select hex
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.
Re: [SOLVED] select hex
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