IPairs and tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Lovingsoul1337
Citizen
Posts: 53
Joined: Fri Feb 21, 2020 1:26 pm

IPairs and tables

Post by Lovingsoul1337 »

Hey can you may help me with my Code ? For some reason it says:

Error

EntityComponent/world.lua:21: bad argument #1 to 'ipairs' (table expected, got nil)

Code: Select all

local system = require('EntityComponent.system')
local system = require('EntityComponent.system')
local positioncomponent = require('EntityComponent.Components.positioncomponent')
local spritecomponent = require('EntityComponent.Components.spritecomponent')
local system = system.new()
local entityList = {}

local world = {}
world.__index = world

function world.new()
  local self = {}
  setmetatable(self, world)
  return self
end

function world:update()
  self:addPlayer(spritecomponent('Assets/lovie_modern.png'), positoncomponent(250,250))
end

function world:draw()
  for key, value in ipairs(self.enityList) do
    if value.spritecomponent ~= nil or value.positioncomponent ~= nil  then
    love.draw(value.spritecomponent, value.position.x, value.position.y)
    end
  end
end

function world:addPlayer(playerComponents)
  for key, value in ipairs() do
    table.insert(entityList, value)
  end
end

return world

would be nice thanks !
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: IPairs and tables

Post by pgimeno »

And what does line 21 say?
Lovingsoul1337
Citizen
Posts: 53
Joined: Fri Feb 21, 2020 1:26 pm

Re: IPairs and tables

Post by Lovingsoul1337 »

Forgot to add my parameter did add it now but still not work.

Code: Select all

self:addPlayer(spritecomponent('Assets/lovie_modern.png'), positoncomponent(250,250))

function world:addPlayer(playerComponents) -- is this a table ? or have i do something else to do it a table ?
  for key, value in ipairs(playerComponents) do
    table.insert(entityList, value)
  end
end
Thanks for your help so far...
MrFariator
Party member
Posts: 510
Joined: Wed Oct 05, 2016 11:53 am

Re: IPairs and tables

Post by MrFariator »

What does spritecomponent('Assets/lovie_modern.png') return? According to your original error, what you are passing to ipairs() is a nil value (ie. in your case playerComponents does not have a defined value). As such, spritecomponent() seems to be returning nothing for addPlayer() to use.
Lovingsoul1337
Citizen
Posts: 53
Joined: Fri Feb 21, 2020 1:26 pm

Re: IPairs and tables

Post by Lovingsoul1337 »

Im new but i think it's return a table from a modul.

Code: Select all

local spritecomponent = {}
spritecomponent .__index = spritecomponent

function spritecomponent.new(path)
  local self = {}
  self.image = love.graphics.newImage(path)
  setmetatable(self, spritecomponent )
  return self
end

return spritecomponent
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: IPairs and tables

Post by 4vZEROv »

To call a table as a function like you do here:

Code: Select all

spritecomponent('Assets/lovie_modern.png')
You need to use the "__call" metamethod.

This should work :

Code: Select all

local spritecomponent = {}
spritecomponent .__index = spritecomponent
spritecomponent .__call = function(self, path) return spritecomponent.new(path) end

function spritecomponent.new(path)
  local object = {}
  object.image = love.graphics.newImage(path)
  return setmetatable(object, spritecomponent )
end

return spritecomponent
- 'self' is a lua keyword, i don't think it's good practice to write 'local self'
- The 'setmetatable(self, spritecomponent )' function return the table with the metamethods, so you can combine those 2 lines.
Lovingsoul1337
Citizen
Posts: 53
Joined: Fri Feb 21, 2020 1:26 pm

Re: IPairs and tables

Post by Lovingsoul1337 »

Thanks will remember this with the return of setmetatable ! I got rid of this problem:

But got another one for some reason my playscene never get's called i tryd to traceback(did put prints ) the problem but i dont find it...

--> https://snipboard.io/tqB5Ah.jpg

this is my output

--> https://snipboard.io/cge1kM.jpg

best regards
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: IPairs and tables

Post by 4vZEROv »

- When you do

Code: Select all

local joystick = require('xxxx')
joystick = joystick.new()
'joystick' is getting redefined and no longer contains the class.

Why do you use OOP and ECS ?
Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests