enclave's Lua thread

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

Moderators: Forum Moderators, Developers

Re: enclave's Lua thread

Postby 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 ;)
enclave
 
Posts: 471
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby 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.
User avatar
Ravana
Moderator
 
Posts: 1380
Joined: January 29th, 2012, 12:49 am
Location: Estonia

Re: enclave's Lua thread

Postby 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?
enclave
 
Posts: 471
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby 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 {}.
User avatar
Ravana
Moderator
 
Posts: 1380
Joined: January 29th, 2012, 12:49 am
Location: Estonia

Re: enclave's Lua thread

Postby 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: 471
Joined: December 15th, 2007, 8:52 am

Previous

Return to Lua Labs

Who is online

Users browsing this forum: No registered users and 1 guest