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

table index is nill #18

Closed
babablackshep opened this issue Nov 14, 2015 · 3 comments
Closed

table index is nill #18

babablackshep opened this issue Nov 14, 2015 · 3 comments

Comments

@babablackshep
Copy link

tried to add some rectangles to the world and this error appeared

@kikito
Copy link
Owner

kikito commented Nov 14, 2015

I need more information in order to be able to help you. Can you provide of the code showing the issue?

@babablackshep
Copy link
Author

sure thing:

local bump = require 'lib/bump'
require "var"

local world = bump.newWorld(50)

function love.update(dt)
if love.keyboard.isDown('d')then px = px + psped * dt end
if love.keyboard.isDown('a')then px = px - psped * dt end
if love.keyboard.isDown('s')then py = py + psped * dt end
if love.keyboard.isDown('w')then py = py - psped * dt end
if love.keyboard.isDown('escape')then love.quit() end
end

function love.draw()
love.graphics.setColor(255,255,255)
player = love.graphics.rectangle("fill", px,py,50,50)
enemy = love.graphics.rectangle("line",ex,ey,50,50)
end

world:add(player, px, py, 50, 50)
world:add(enemy, ex, ey, 50, 50)

print(world:getRect(enemy))

sorry for being a bit late
and if it helps the error is at line 600 in bump.lua(i probably should have said that to begin with)

@kikito
Copy link
Owner

kikito commented Nov 15, 2015

Hi there,

The problem is, fortunatelly, quite simple: you are not using love.graphics.rectangle correctly.

In LÖVE, the drawing-related functions (like love.graphics.rectangle, or love.graphics.circle) don't "create" anything. They just "change the colors of the current canvas in a certain way" (when drawing a rectangle, they change the colors so they look like a rectangle. When drawing a circle, they change the colors to look like a circle, etc). But once they are done changing the colors they don't "store that". They don't "create a rectangle" or a circle. They return nothing (which in Lua is the same as returning nil).

For this reason, player and enemy are nil (nothing). When you try to add them to the world, there is an error, because you are "adding nothing".

A way to get around this is using a Lua table to represent player and enemy, and then using that table to both add it to the world and drawing it. In addition to that, you were not updating the world after moving the player, and the order in which you were doing initializations and checks was a bit ... too original.

Here is a version that should work (I have not tested it for typos and such).

local bump = require 'lib/bump'

local world = bump.newWorld(50)
local player = {x = 10, y = 10, w = 50, h = 50, speed = 100}
local enemy = {x = 100, y = 100, w = 50, h = 50}

world:add(player, player.x, player.y, player.w, player.h)
world:add(enemy, enemy.x, enemy.y, enemy.w, enemy.h)

function love.update(dt)
  if love.keyboard.isDown('d') then player.x = player.x + player.speed * dt end
  if love.keyboard.isDown('a') then player.x = player.x - player.speed * dt end
  if love.keyboard.isDown('s') then player.y = player.y + player.speed * dt end
  if love.keyboard.isDown('w') then player.y = player.y - player.speed * dt end

  world:move(player, player.x, player.y)

  if love.keyboard.isDown('escape') then love.quit() end

  print(world:getRect(enemy))
end

function love.draw()
  love.graphics.setColor(255,255,255)
  love.graphics.rectangle("fill", player.x, player.y, player.w, player.h)
  love.graphics.rectangle("fill", enemy.x, enemy.y, enemy.w, enemy.h)
end

Since this is not an issue with bump, I am closing this issue. Feel free to ask more questions (if you have questions about LÖVE, however, I would recommend you to ask them in the LÖVE forums, there's lots of people which can help you there besides me, and my schedule has been a bit packed lately).

Regards!

@kikito kikito closed this as completed Nov 15, 2015
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

2 participants