Zachary Ryans
Zachary Ryans

Reputation: 11

Having trouble with Lua's for loops (Love2d)

Although I've read few tutorials on Lua and I have an idea in my head about how to use Lua's for loops, I'm having trouble. Maybe it's because I'm used to Python's for-loops? Whenever the player moves in my game, the game checks to see if a wall on the map is in the way, and it checks to see if an NPC is in the destination's coordinates.

While I got wall-checking to function perfectly, this new NPC for-loop makes it so the player never moves. The checkSpace function looks through a list of NPCs (which there is only one currently) to see if the player can move to the destination.

player = {
    grid_x = 2,
    grid_y = 2,
    act_x = 32,
    act_y = 32,
    transit = false,
    direction = {0, 0}
}

npc = {
    grid_x = 4,
    grid_y = 3,
    act_x = 64,
    act_y = 48,
}

npcs = {npc}

function checkSpace(grid_y, grid_x, direction)
    if map[grid_y + (1 + direction[2])][grid_x + (1 + direction[1])] == 0 then
        -- if checkNPCs
        for _,v in pairs(npcs) do
            if grid_x == v.grid_x and grid_y == v.grid_y then
                return true
            end
        end
    end
end

function love.keypressed(key)
    if player.transit == false then
        if key == "up" then
            player.direction = {0, -1}
            if checkSpace(player.grid_y, player.grid_x, player.direction) == true then
                player.grid_y = player.grid_y - 1
                -- move player.direction before the if statement to make the player change direction whether or no there is space to move
                player.transit = true
            end
        end 
   end
 end

Edit: I fiddled around with the program a bit and I've made some progress. Instead of the keypressed program checking to see if checkSpace returns True, the programs been modified so that it returns false if there isn't an obstacle in the way.

    if key == "up" then
        player.direction = {0, -1}
        if checkSpace(player.grid_y, player.grid_x, player.direction) == false then
            player.grid_y = player.grid_y - 1
            -- move player.direction before the if statement to make the player change direction whether or no there is space to move
            player.transit = true
        end

I've gotten a very basic (and practically useless) for-loop working with my program, but if I try to do anything more advanced with it then I get the error where my player character will not move.

    for nameCount = 1, 1 do
        if grid_x + direction[1] == npc.grid_x and grid_y + direction[2] == npc.grid_y then
            return true
        else
            return false
        end
    end

I've posted the full code at this location: http://pastebin.com/QNpAU6yi

Upvotes: 0

Views: 873

Answers (1)

Oliver
Oliver

Reputation: 29571

I will assume you mean that you are having trouble with this:

for _,v in pairs(npcs) do
    if grid_x == v.grid_x and grid_y == v.grid_y then
        return true
    end
end

where npcs is

npcs = {npc} 

which is a table containing one item, namely npc, which is itself

npc = {
    grid_x = 4,
    grid_y = 3,
    act_x = 64,
    act_y = 48,
}

So the for loop involves only one pair (one iteration), whose key is discarded and whose value is npc, so grid_x == v.grid_x and grid_y == v.grid_y compares the grid X and Y of npc with the values given to checkSpace, returning true only if they match. The npcs = {npc} with loop is odd but I'll assume it is because you posted minimal example. So far so good, nothing wrong.

However, checkSpace compares the player's current position to that of the npc being looped over, whereas what you should be checking is whether the NPC is in the proposed destination:

if grid_x + direction[1] == v.grid_x and grid_y + direction[2] == v.grid_y then

It's hard to say from your post whether this will fix your problem, because I don't think the symptom you mention (checkSpace always returns true) is correct, but the loop is correct, the predicate is wrong and should something like I show above.

Upvotes: 0

Related Questions