Variable scoping problem

Hi, I’m having a strange scope problem that clearly shows I’m still having a few teething problems with Lua :slight_smile:

Basically I’m trying to swap these two grid positions around (just a table with x and y), after storing off the position on line 1 and then setting the first tiles position to the second on line 2 I will then set the second tiles position back to the stored off position but this is where my problem lies. After setting the first tiles position the stored off value is also changed to the new position for the first tile (this may not make sense).
[lua]local tempPos = self:getGridPosition()
self:setGridPosition(otherTile:getGridPosition())
otherTile:setGridPosition(tempPos ) [/lua]

Edit: I can provide code if needed.
[import]uid: 5833 topic_id: 1200 reply_id: 301200[/import]

Have you tried printing out the type of “tempPos” after line 1?

print(type(tempPos))

I’m wondering if you are storing the function instead of the value returned which is why line 3 is working.

It’s hard to tell what’s going on seeing code out of context.

Tom [import]uid: 6119 topic_id: 1200 reply_id: 3207[/import]

Sorry I removed the print statements from the code I put in the post but yea it definitely is changing after the second line.

I can zip up the code and email it to you if that would be any use? [import]uid: 5833 topic_id: 1200 reply_id: 3208[/import]

I can take a look. Send it to myfogview at gmail dot com [import]uid: 6119 topic_id: 1200 reply_id: 3209[/import]

Sent, thank you :slight_smile:


Graham [import]uid: 5833 topic_id: 1200 reply_id: 3210[/import]

Graham,

I think I found the problem. The value of “tempPos” returned by getGridPosition was a pointer to the tile position table and was updated when you tried to swap tile positions. My fix was returning a copy of the table x/y values that wasn’t tied to the current tile position.

Tom [import]uid: 6119 topic_id: 1200 reply_id: 3213[/import]