mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-02 11:40:05 +00:00

The purpose of this is to make it easy for functions implemented in Lua to handle locations in the same way as functions implemented in C++. The location_set module has been updated to make use of this functionality in its setter functions. Usage example: Imagine a function foo with the following signature: foo(bar : string, home : location, mode : string, target : location, moo : boolean) -> boolean With the new read_location function it could be implemented as follows: function foo(...) -- First argument goes in bar local bar = ... -- Read location starting at the second argument local home, n = wesnoth.mP.read_location(select(2, ...)) -- note: n will be 0 if a location wasn't found at that position -- This could be an error, or it could be handled as an optional parameter -- Next argument after that goes in mode local mode = select(n + 2, ...) -- Then read a location into target local target, m = wesnoth.map.read_location(select(n + 2, ...)) -- Finally, read a parameter into moo local moo = select(m + n + 2, ...) -- Do stuff with all these parameters return true end With that code, all the following invocations of foo work: foo('a', 'b', true) -- both optional locations omitted foo('a', 1, 2, 'q', 5, 6, false) -- locations given as separate integer parameters foo('a', 'm', {1, 7}, true) -- first location omitted, second given as 2-element array foo('a', some_unit, 'z', {x = 5, y = 10}, false) -- a unit also functions as a location foo('a', 7, 12, 'q', my_leader, true) -- mixing different forms also works