enclave's Lua thread

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

Moderators: Forum Moderators, Developers

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

Re: enclave's Lua thread

Post by enclave » June 16th, 2017, 12:56 pm

I will try that, thanks! my lua skills are like a beginner beginner level.. this helps me understand more about lua arrays.. structure..

A question.. would it look like this then?

Code: Select all

local saved_terrains = wesnoth.get_locations { x = "5", y = "25", { "not", { terrain = "M*,W*,M*^*,W*^*,Ai*,Ai*^*,Rrc,Q*,X*,Q*^*,X*^*,Iwr,Rp^Ecf,Rp^Dr,_off^_usr"} } })
What about them being stored in pairs? Would i be able to do something like this for i=0,saved_terrains.length,1 do ... end later? or this this some kind of different type of array? if i do wesnoth.set_variable("ns_saved_terrains",saved_terrains) would it work? would it be same wml array? You can see how bad I am at lua when you see my questions ;)

User avatar
Ravana
Moderator
Posts: 1786
Joined: January 29th, 2012, 12:49 am
Location: Estonia

Re: enclave's Lua thread

Post by Ravana » June 16th, 2017, 12:59 pm

>What about them being stored in pairs?
You can iterate them as pairs like your example does.

>Would i be able to do something like this for i=0,saved_terrains.length,1 do ... end later? or this this some kind of different type of array?
Lua only has tables - no special array types. So, possible, but unless you need to manipulate those indexes, better use pairs.

>if i do wesnoth.set_variable("ns_saved_terrains",saved_terrains) would it work? would it be same wml array?
It will not.

enclave
Posts: 799
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave » June 16th, 2017, 2:17 pm

Another code requiring translation.. otherwise im stuck (and btw i have no idea how ipairs work.. never met something to work in pairs in any programming language i seen so far)

Code: Select all

[store_locations]
	x=$xbegin-$xend
	y=$ybegin-$yend
	[not]
	terrain=_off^_usr
	[/not]
	variable=ns_cube
	[/store_locations]
how to translate it into wesnoth.fire ?

Code: Select all

wesnoth.fire("store_locations",
	{ x="$xbegin-$xend", y="$ybegin-$yend", not { terrain="_off^_usr" }, variable="ns_cube"})
or with local xbegin xend ybegin yend

Code: Select all

wesnoth.fire("store_locations",
	{ x="xbegin-xend", y="ybegin-yend", not { terrain="_off^_usr" }, variable="ns_cube"})
or some other way?

User avatar
Ravana
Moderator
Posts: 1786
Joined: January 29th, 2012, 12:49 am
Location: Estonia

Re: enclave's Lua thread

Post by Ravana » June 16th, 2017, 4:07 pm

Lua does not evaluate your $. I think you know by now how to transfer simple variables between WML and Lua.

I think your not needs extra {}.

enclave
Posts: 799
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave » June 16th, 2017, 5:20 pm

that's the right way to write it:

Code: Select all

wesnoth.fire("store_locations",
   { x=xbegin-xend, y=ybegin-yend, variable="ns_cube3", { "not", { terrain="Gg,Mm,Hh" }} })
xbegin xend ybegin yend are locals

enclave
Posts: 799
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave » June 28th, 2017, 7:57 pm

A question..
It is known that lua causes OOS if used math.random, will it still cause OOS if we do math.randomseed(1234) prior to it? It should generate same sequences of number for all players or no? would it still need synchronization?

User avatar
Ravana
Moderator
Posts: 1786
Joined: January 29th, 2012, 12:49 am
Location: Estonia

Re: enclave's Lua thread

Post by Ravana » June 28th, 2017, 8:02 pm

If you set seed in synchronised way, all players get same sequence. Obviously, if you set it to constant, you wont get any randomness.

enclave
Posts: 799
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave » July 1st, 2017, 8:51 pm

Hi, I'm trying to use this:
wesnoth.fire("replace_map",{map="{campaigns/Heir_To_The_Throne/maps/01_The_Elves_Besieged.map}",expand="yes",shrink="yes"})
wesnoth.fire("replace_map",{map="{add-ons/Optimisation_Tester/maps/4p_A_New_Land.map}",expand="yes",shrink="yes"})

i tried tons of different ways and nothing works, always same error:
a terrain with a string with more than 4 characters has been found, the affected terrain is : {add-ons/Optimisation_Tester/maps/4p_A_New_Land.map}

[replace_map] works fine, but lua doesn't how to replace "{" with string so that it doesnt think of it as about a table?? or what I need to do to make it work? Thanks!

User avatar
Ravana
Moderator
Posts: 1786
Joined: January 29th, 2012, 12:49 am
Location: Estonia

Re: enclave's Lua thread

Post by Ravana » July 1st, 2017, 9:42 pm

[args] I guess. Or do the inclusion manually. I think 1.13 Lua has support to read files too.

enclave
Posts: 799
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave » July 2nd, 2017, 3:42 pm

i tried to google [args] lua, but couldn't find anything.. what do you mean? Thanks

User avatar
Ravana
Moderator
Posts: 1786
Joined: January 29th, 2012, 12:49 am
Location: Estonia

Re: enclave's Lua thread

Post by Ravana » July 2nd, 2017, 3:47 pm

ctrl+f would have helped. Or search. When you search for args you get 6 results, 3. is most useful.

enclave
Posts: 799
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave » July 2nd, 2017, 8:37 pm

thanks, but my level of lua didn't let me understand any of them to find them useful. I'll just workaround if nobody knows how to solve the problem, its a lot quicker. If i find solution in future I'll post it, so far just to let people know, things like \{ \} didn't work.

Ok,

Code: Select all

 local string st = "{campaigns/Heir_To_The_Throne/maps/01_The_Elves_Besieged.map}"
wesnoth.fire("replace_map",
{map = st, expand = "yes" , shrink = "yes" })
didn't work.

But

Code: Select all

{VARIABLE st "{campaigns/Heir_To_The_Throne/maps/01_The_Elves_Besieged.map}"}
[lua]
code = <<
wesnoth.fire("replace_map",
{map = wesnoth.get_variable("st"), expand = "yes" , shrink = "yes" })
>>
[/lua]
has worked ;)

And just to give people more examples of how it works:
{campaigns/Heir_To_The_Throne/maps/01_The_Elves_Besieged.map} to get campaign map
{~add-ons/Optimisation_Tester/maps/4p_A_New_Land.map} to get map from any of your installed add-ons
{multiplayer/maps/4p_A_New_Land.map} to get any default multiplayer map that comes together with Wesnoth clean install.

Spoiler:

enclave
Posts: 799
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave » July 27th, 2017, 1:27 pm

hi, any ideas why this filter would not work?

Code: Select all

local bool bb = wesnoth.match_location(xx, yy, { terrain = wesnoth.get_variable("fvar_terrains"), { "not", { "filter_adjacent_location", { terrain = wesnoth.get_variable("fvar_village_type") } } } })
The particular place is { "not", { "filter_adjacent_location", because the following below code is working perfectly fine:

Code: Select all

local bool b = wesnoth.match_location(xx, yy, { terrain = wesnoth.get_variable("fvar_terrains"), { "filter_adjacent_location", { terrain = wesnoth.get_variable("fvar_village_type") } } })
I tried multiple versions with notafter filter adjacent location... and without brackets.. doesn't work for me, says "bad argument #3, WML table expected, got table"
Thanks..

mattsc
Posts: 1049
Joined: October 13th, 2010, 6:14 pm
Location: Wandering, mostly aimlessly

Re: enclave's Lua thread

Post by mattsc » July 27th, 2017, 2:08 pm

You need an additional {} around "filter_adjacent_location" ?

Edit: I mean, not just around that string, but around the string and the actual filter that follows it.

enclave
Posts: 799
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave » August 8th, 2017, 3:43 pm

mattsc wrote:You need an additional {} around "filter_adjacent_location" ?

Edit: I mean, not just around that string, but around the string and the actual filter that follows it.
Thanks mattsc, I['m quite sure I already tried it and it didnt work.. so workaround was just to use some obvious and unneeded filter..
Something like that local bool bb = wesnoth.match_location(xx, yy, { terrain = wesnoth.get_variable("fvar_terrains"), "not", { x=0, { "filter_adjacent_location", { terrain = wesnoth.get_variable("fvar_village_type") } } } }) not sure exactly now.. lazy to search how i "solved" it.. since nobody cares. if anyone really needs it, just ask me.

Post Reply