Not understanding a line of LUA code

Hello,
I am pretty new to the Defold platform and have been folowing the “Getting Started” tutorial.
When the first code was introduced (the code for moving the ground), I had trouble understanding two lines of code

 local pieces = { "ground0", "ground1", "ground2", "ground3",
                "ground4", "ground5", "ground6" } 1

function init(self) 2
self.speed = 6
end

function update(self, dt) 3
for i, p in ipairs(pieces) do 4
    local pos = go.get_position(p)
    if pos.x <= -228 then 5
        pos.x = 1368 + (pos.x + 228)
    end
    pos.x = pos.x - self.speed
    go.set_position(pos, p) 6
end
end

Here, I don’t understand the relevance of adding (pos.x+228) to 1368.
Also, if the game object is on the left most edge of the screen, we bring to 1368, which is the rightmost edge so even then why do we subtract self.speed in the X axis?
Please explain.

Here’s some nice ascii art for you:

Imagine that this is your screen:

   +----------------+
   |                |
   |                |
   |                |
   |                |
   +----------------+
   |<---- 1368 ---->|

And here are the seven platforms on the screen, each being 228 pixels wide:

   +----------------+
   |                |
   |                |
   |                |
   111222333444555666777
   +----------------+

When pos.x <= -228 for a platform then we know that it has passed completely out of the left edge of the screen:

   +----------------+
   |                |
   |                |
   |                |
111222333444555666777
   +----------------+

If we simply moved it 1368 pixels to the right we’d end up with it overlapping the seventh platform:

   +----------------+
   |                |
   |                |
   |                |
   222333444555666XXX
   +----------------+

So we need to move it 228*7=1596 pixels (or 1368+228) to the right:

   +----------------+
   |                |
   |                |
   |                |
   222333444555666777111
   +----------------+

@sicher: I think we should improve the tutorial to define a constant for the width of a platform and perhaps use the width multiplied by the number of entries in pieces when we move the platforms.

2 Likes

Good point! I’ll look into that.

Thank you. I understood the concept clearly now but still I have doubts about it:
Even after moving it by 1596, we move it by self.speed (6) to the left. Why is that? And what does 1368+(pos.x+228) mean? I mean why don’t we simply say 1596?

And I did what you asked. I set the pos.x=1596 in the IF statement. However this doesn’t work and this leaves a big gap in the ground.
I am not getting this at all. Please help.

You have to do pos.x = pos.x + 1596 otherwise you’ll most likely get a gap.

All ground pieces should move at a constant speed every frame, even the one that is moved to the end of the line. If you’re not consistently doing this you’ll get a gap or overlap. You always want all ground pieces to match up one after the other without a single pixel’s gap between them. For this to happen you need to move all of them at the same speed every frame and the one that moves outside the left edge of the screen must be positioned as the rightmost ground piece.

1 Like

pos.x + 228 Is there to highlight that we take the width of the tile into account.

You need to offset by pos.x unless the speed is a multiple of the width of the tile, otherwise you will move it to a place where you get gaps.

1 Like