Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump.lua: 619: attempt to index field 'rects' (a nil value) #35

Closed
wellslight opened this issue Apr 2, 2017 · 4 comments
Closed

bump.lua: 619: attempt to index field 'rects' (a nil value) #35

wellslight opened this issue Apr 2, 2017 · 4 comments

Comments

@wellslight
Copy link

As the title says, when I go to run this in LOVE 0.10.2 I get "bump.lua: 619: attempt to index field 'rects' (a nil value)." I have copied the function mentioned below. I am new to programming so I don't know how to fix this or work around it. Any help is appreciated!

function World:add(item, x,y,w,h)
local rect = self.rects[item] --this is line 619 and contains the "rects" referred to
if rect then
error('Item ' .. tostring(item) .. ' added to the world twice.')
end
assertIsRect(x,y,w,h)

self.rects[item] = {x=x,y=y,w=w,h=h}

local cl,ct,cw,ch = grid_toCellRect(self.cellSize, x,y,w,h)
for cy = ct, ct+ch-1 do
for cx = cl, cl+cw-1 do
addItemToCell(self, item, cx, cy)
end
end

return item
end

@DanielPower
Copy link

Can you post an example of your code which causes this error to occur?

@wellslight
Copy link
Author

Thanks for the quick reply! Yeah, I'm just trying to make a simple tiled based top down game where I can walk the player around the screen. I want to have objects and buildings that the player won't walk through, but will bump into.
here's the gist of my code (it has a problem with the line just below "player.img" and just above "objects={}", where it calls for world.add(...)

`
local bump = require 'bump'

function love.load()
local world= bump.newWorld(32)
map = love.graphics.newImage("courtyard.png")
player = {}
player.speed = 120
player.x = 230
player.y = 580
player.w = 32
player.h = 32
player.dir = 0
player.move = true
player.img = love.graphics.newImage("char.png")
world.add(player, player.x, player.y, player.w, player.h)

objects= {}
objects.building = {}
objects.building.x = 416
objects.building.y = 256 - 32
objects.building.w = 96
objects.building.h = 128
objects.building.img = love.graphics.newImage("building.png")
world.add(objects.building, objects.building.x, objects.building.y, objects.building.w, objects.building.h)
end

function love.draw()
love.graphics.draw(map, 0,0)
--draw player
love.graphics.draw(player.img, up2, player.x, player.y)
end

function love.update(dt)
if player.x < 0 then player.x = 0 end
if player.y < 0 then player.y = 0 end
if player.x > 590 then player.x = 590 end
if player.y > 590 then player.y = 590 end

if player.move == true then
if love.keyboard.isDown("up") then player.y = player.y - player.speed * dt --and player.dir = 0
elseif love.keyboard.isDown("down") then player.y = player.y + player.speed * dt --and player.dir = 2
elseif love.keyboard.isDown("left") then player.x = player.x - player.speed * dt --and player.dir = 3
elseif love.keyboard.isDown("right") then player.x = player.x + player.speed * dt --and player.dir = 1
end

end
`

@kikito
Copy link
Owner

kikito commented Apr 3, 2017

Hi there, the problem is that you wrote world.add(...). It should be world:add(...) (with : instead of .). In general, when you use bump, you should always write world:<method> and never world.<method>.

@kikito kikito closed this as completed Apr 3, 2017
@wellslight
Copy link
Author

Oh wow, such a simple fix. Thanks for catching that! I re-read that a thousand times and still didn't see the mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants