enclave's Lua thread

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

Moderator: Forum Moderators

Post Reply
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Regarding Lua,
are local lua variables stored "only" on each separate client?
I mean if I assign

Code: Select all

{VARIABLE variable 25} 
then each player can work with it, access, modify...
for each player $variable would be same (25) unless somebody change it.

but if I do

Code: Select all

local variable = 25 
is it only available on each separate client, and they can not access each other's arrays or variables? So each client potentialy could have same local variable with different value? for example for player 1 lua variable=25, player 2 same name lua variable=20, player 3 variable=67 ? And they can not share it, access, modify.. ?

Thank you!
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Post by gfgtdf »

enclave wrote: I'm observing replay of a game of my mod..
The players got OOS in turn 1.. and again i see "calculated movement destination (x=39 y=7) didn't match the original destination (x=38 y=7)".. Why could it be?
There is a known bug in in 1.12.1 and older version that can casue this OOS. Make sure all players have version 1.12.2. Otherwise this can of course also happen because of unsafe wml code.
enclave wrote: is prestart synced?
Yes prestart is synced.
enclave wrote: my placement of [unit] units is happening in prestart event.. should i put it with synchronized choice or move it to turn 1 or leaving it as is is safe? Any advices very much appreciated, thank you!!!
No things inside sync_choice will only happen in one client. meaning everything inside sync_choice is unsynced so modiifying teh gamestate there (for example with [unit]) will cause OOS, this is also explained in the wiki page about OOS.
enclave wrote: PPS. how to use this http://wiki.wesnoth.org/ReplayWML ??? where to put it? why? any information would help :) thanks!
You don't write it anywhere, its just for people who want to anylyze replay saves with a text editor. (or it ca explain some OOS messages where teh OOS message sais somethign like "... not found, found indead: <wmlcode>")
enclave wrote: PPPS. the latest bug I just got is: some OOSes.. and then I was offered to recruit a unit in a saved replay game.. ??? wth.. I was also able to recruit, but the units were becoming different color (becoming previous side's ownership on current side territory.. or similar.. hard to understand) and was given hell lot of other options, please check attachment for replay.. I think it is because by mistake they gave control to non-existing side in this particular case.. was 7 players, they gave observer control of side 8.. So i guess this particular replay is not bugged by code.. but by user's actions.. in the end of replay blue's units were in 4 different parts of world..
[/quote]
I assume by recruit you mean custom recuit that way implemented in wml/lua with [message][option]? In case of an OOS it can sometimes happen that teh game does not know which [option] was selected by the player. In this case teh game promts the [message] to the replay viewer hopin that he knows which [option] was selected.

enclave wrote:Regarding Lua,
are local lua variables stored "only" on each separate client?
I mean if I assign

Code: Select all

{VARIABLE variable 25} 
then each player can work with it, access, modify...
for each player $variable would be same (25) unless somebody change it.

but if I do

Code: Select all

local variable = 25 
is it only available on each separate client, and they can not access each other's arrays or variables? So each client potentialy could have same local variable with different value? for example for player 1 lua variable=25, player 2 same name lua variable=20, player 3 variable=67 ? And they can not share it, access, modify.. ?

Thank you!
No except that the lua variable is local is both the same. In both cases the variable is store in each client seperately and should be the same on all clients if you write safe code.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Thank you gfgtdf for all the answers!
gfgtdf wrote:
enclave wrote: my placement of [unit] units is happening in prestart event.. should i put it with synchronized choice or move it to turn 1 or leaving it as is is safe? Any advices very much appreciated, thank you!!!
No things inside sync_choice will only happen in one client. meaning everything inside sync_choice is unsynced so modiifying teh gamestate there (for example with [unit]) will cause OOS, this is also explained in the wiki page about OOS.
This one i didn't understand :( using sync_choice with [unit] would cause OOS?
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Post by gfgtdf »

enclave wrote: This one i didn't understand :( using sync_choice with [unit] would cause OOS?
You shall not change the gamestate in the function you pass to sync_choice (this usually imples using [unit]) this is explained here http://wiki.wesnoth.org/OOS_%28Out_of_Sync%29 and here http://wiki.wesnoth.org/LuaWML:Misc#wes ... ize_choice.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Thanks very much gfgtdf, I will read it!;)
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Hello!
1) Anyone knows a lua way to react to the keyboard input? something like OnKeyPress("h") do hablabla()
2) Anyone knows a lua way to react on opening right click menu and selecting "terrain description"?
for example...

Code: Select all

function wesnoth.game_events.on_event()
if wesnoth.current.event_context.name == "RC_Options_terrain_description" then
hablabla()
end
end
Thanks!
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: enclave's Lua thread

Post by Elvish_Hunter »

enclave wrote:1) Anyone knows a lua way to react to the keyboard input? something like OnKeyPress("h") do hablabla()
There isn't any that I'm aware of, sorry. The only keyboard bindings available (which cannot be modified) when using GUI2/Lua stuff are Enter and Esc, which will both close a dialog and return two different values.
enclave wrote:2) Anyone knows a lua way to react on opening right click menu and selecting "terrain description"?

Code: Select all

function wesnoth.game_events.on_event()
if wesnoth.current.event_context.name == "RC_Options_terrain_description" then
Both wesnoth.game_on_event() and wesnoth.current.event_context refer to WML events, not to GUI events. Given this fact, you can actually use something like this to fire a "RC_Options_terrain_description" event:

Code: Select all

[set_menu_item]
    id=terrain_description
    description= _ "Terrain description"
    [command]
        [fire_event]
            name=RC_Options_terrain_description
        [/fire_event]
    [/command]
[/set_menu_item]
But at this point, you may as well code your actions in a custom WML tag and put it inside [command], instead of using [fire_event]. ;)
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)
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Post by gfgtdf »

wml menu items are implemented by generating a event form the content of the [command] where teh name is "menu item <menu_item_id>" (in 1.13 the spaces were changes to underscores to its "menu_item_<menu_item_id>")
so in the code form EH this code:

Code: Select all

if wesnoth.current.event_context.name == "menu item terrain_description" then ... end
coudl also be used to detect the menu item click

enclave wrote: 1) Anyone knows a lua way to react to the keyboard input? something like OnKeyPress("h") do hablabla()
You can specify default hotkeys in [set_menu_item].
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Wow, Thank you both Elvish_Hunter and gfgtdf, you both gave me some information and I will try it soon and let you know what worked for me and what didn't..
You can specify default hotkeys in [set_menu_item].
This is unfortunately not what I am looking for.. I need something stronger.. like "n" key that you use to circle through units..
but I don't know if it is C++ coded or it can be changed with lua or wml..
My goal is to try to make an unsynchronized menu... (which works in 1.12 when it is not your turn) that would react to key press instead of mouse manipulations..
to replace this:

Code: Select all

local last_mouse_over_time
local mouseover_counter = 0

function wesnoth.game_events.on_event()
--test_new_lua_things()
	if wesnoth.current.event_context.name == "select" then
		local time = wesnoth.get_time_stamp()
		if last_mouse_over_time and ((time - last_mouse_over_time) > 2000) then
			mouseover_counter = 0
			unit1_moused_over_x = nil; unit1_moused_over_y = nil
			unit2_moused_over_x = nil; unit2_moused_over_y = nil
		end
		last_mouse_over_time = time
		local du = wesnoth.get_displayed_unit()
		if du then
			if (du.x == unit2_moused_over_x) and (du.y == unit2_moused_over_y) then
				mouseover_counter = mouseover_counter + 1
				unit1_moused_over_x = unit2_moused_over_x
				unit1_moused_over_y = unit2_moused_over_y
				unit2_moused_over_x = du.x
				unit2_moused_over_y = du.y
				if mouseover_counter > 2 then
					mouseover_counter = 0
					wesnoth.fire_event("show_unsynced_menu")
				end
			else
				-- think here, mouseover_counter = 0
				unit2_moused_over_x = du.x
				unit2_moused_over_y = du.y
			end
		end	
	end
end
One of my ideas was to replace it with just openening "terrain description" instead.. if it is possible.. who would need to read terrain description anyway?...
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Post by gfgtdf »

in wesnoth 1.13 you cna just add unsyced=yes to [set_menu_item] which can be used liek this (also together with defualt_hotkey) which is then also avaialbe in ememy turn.

No, all default hotkeys like show help or cycle units are coded in c++ and the only way to to add hotkeys from wml/lua is default_hotkey from [set_menu_item].

What you could do it tracking the mouse movement (when mouse enters hexes) with wesnoth.theme_items and use that to make your code react to some mouse gestures, but i wound't call that easy.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Ravana
Forum Moderator
Posts: 3002
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: enclave's Lua thread

Post by Ravana »

What you could do it tracking the mouse movement (when mouse enters hexes) with wesnoth.theme_items and use that to make your code react to some mouse gestures, but i wound't call that easy.
Actually, this time reading through the reference of wesnoth.theme_items I started to understand something. Though I am quite content with the solution I managed to get, this way it isnt immediately obvious that its from EoHS.

Btw, does the 1.13 standard unsynced menu provide way to see who opened it? Given that side_number wont be reliable anymore. (unsynced key isnt documented in wiki currently)
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Post by gfgtdf »

Ravana wrote: Btw, does the 1.13 standard unsynced menu provide way to see who opened it? Given that side_number wont be reliable anymore. (unsynced key isnt documented in wiki currently)
Yes afaik 1.13 has a lua function to query the viewing side wesnoth.get_vewing_side() see the definition here: https://github.com/wesnoth/wesnoth/blob ... l.cpp#L664 this was introduced in wesnoth 1.13.1.

Also note the such unsynced meanus could also be used from observers.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Ravana
Forum Moderator
Posts: 3002
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: enclave's Lua thread

Post by Ravana »

Also note the such unsynced meanus could also be used from observers.
Nice! I didnt really expect that much. That was my main motivation for working with select-based menu.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Thanks very much for information gfgtdf! if no other tricks work i will think about just selecting some specific tile on map.. may help:)

I just came across some simple task.. And it will take me ages to find it in help, so maybe if I ask you would know..

I done this:

Code: Select all

[side]
side=1
team_name=Portugal
color=red
canrecruit=yes
controller=human
name=Portugal
...
I need them to have name, because so people know their starting positions on map..
Now i just want all names on prestart to be renamed to normal..
because my settings made all leader names be "Portugal" and others.. And I just tried to access original names and I can not do it with WML.. nothing works.. i did [store_side] and then tried to access name, team_name, user_team_name, current_player
everything gives me either Portugal, or nil.. but in the status table I can see original names.. so they must be somewhere,
accessable.. somehow... but how? is there a quick lua way?

Thanks very much in advance!
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Post by gfgtdf »

hmm i dont really understand what do you maen by 'original names' ?
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
Post Reply