Ryeath_Greystalk wrote:
PosistionTable = {12.16, 12.17, 13.18} etc, etc.
Unfortunately, these will be interpreted as decimal numbers e.g. 12.16 == ( 1216 / 100 ) . As usual, there are many ways to write the code to implement this functionality. Here's one way:
Code: Select all
PositionTable = { { x = 12 , y = 16 } ,
{ x = 12 , y = 17 } , { x = 13 , y = 18 } , }
function NorthButton()
local x = itemOne.x
local y = itemOne.y - 1
local PositionValid
for _ , v in ipairs( PositionTable ) do
if v.x == x and v.y == y then
PositionValid = true
break --this ends the loop, since we already found our match
end
end
if not PositionValid then
return
end
for _ in entitiesAt( itemOne.level , x , y ) do
--if there are any entities at all, then stop here
return
end
--Put code here to move the item.
--This will probably involve destroying the old
--item and spawning a new one.
end
There's two bits of this code that probably bear more explanation:
Code: Select all
PositionTable = { { x = 12 , y = 16 } ,
{ x = 12 , y = 17 } , { x = 13 , y = 18 } , }
This creates a table, and makes the variable PositionTable refer to that table. Since we didn't specify any keys for this table, lua just starts counting upwards from 1 to assign the keys. So this is equivalent to:
Code: Select all
PositionTable = { [1] = { x = 12 , y = 16 } ,
[2] = { x = 12 , y = 17 } , [3] = { x = 13 , y = 18 } , }
The values within PositionTable are themselves tables. We did specify keys ( "x" and "y" ) for these inner tables, so lua uses the keys we specified. The values within these inner tables are individual coordinates. So we could now write for example:
Code: Select all
if PositionTable[2].y == 17 then
--lua will reach this line, based on the
--value of PositionTable given above.
end
Note that square brackets ( [] ) and the dot ( . ) are not equivalent. The relationship between them is:
Code: Select all
table.whatever == table["whatever"]
In other words, the syntax table.whatever will always treat "whatever" as a string type key. If you want to use a value of any other type (e.g. number) as a key, then you must use the square bracket syntax. If you want to use the
value of a variable as a key, then you must use the square bracket syntax. If you use the dot syntax:
then you are actually using the string "i" as the key, which has no relationship at all to the variable i .[/digression]
The other especially notable line is:
Code: Select all
for _ , v in ipairs( PositionTable ) do
ipairs is a native lua function which takes a table as its argument. Using ipairs allows you to loop on all of the key/value pairs in the table, assuming that all of the keys are consecutive positive integers starting with 1 . A table that conforms to these requirements ( such as PositionTable ) is often called an "array". There is a similar function, pairs , which allows you to loop on all of the key/value pairs in a table, regardless of the nature of the keys.
Why did I use the variable names "_" and "v" ? "v" stands for "value". I typically prefer verbose variable names, but when the lifetime of the variable is so short ( just this loop ) I'm a little more relaxed about it. "_" as a variable name is a standard lua convention for a variable that we don't actually care about. I know that the keys will be consecutive positive integers starting with 1, but I'm really only concerned with the values. But ipairs always gives me a key and a value so I have to do something with the key before I can get to the value. So I just assign the key to the "dummy" variable _ . The same convention is also in use in the next loop:
Code: Select all
for _ in entitiesAt( itemOne.level , x , y ) do
--if there are any entities at all, then stop here
return
end
Here, we don't even care what entity is at the specified location, we just care about the presence or absence of such an entity.
I hope any of this helps

. And don't forget there is also an
official lua guide. It's written with the stand-alone lua interpreter in mind, and talks about quite a few topics that aren't supported in Grimrock, but it's still immensely helpful.