How to set limit to a table ?

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
noah.l
Prole
Posts: 2
Joined: Tue Oct 10, 2017 9:41 am

How to set limit to a table ?

Post by noah.l »

Hi guys !
I've 2 problems, I want to create a system inventory, so I have created an image which contains 48 places for items, and I want to say "If you have more than 48 items, you can't take this one !". Si I tried many things ...
Here's my code :

Code: Select all

if hero.inventory[48] ~= nil then
  canGetItem = false
else
  canGetItem = true
end
But the player can get more than 48 items ... I also tried 47 and 49 but it didn't change anything.
Y second problem is that the player doesn't get only 1 item but a random number with this code :

Code: Select all

if collisionPlayer(hero, musicSheet) == true then
    musicSheet.isVisible = false
    if canGetItem == true then
      table.insert(hero.inventory, musicSheet)
    end
  end
But the player doesn't take only 1 item, but a random number (I don't know why) ...
I tried to replace the table.insert by :

Code: Select all

      hero.inventory[hero.inventory.i] = musicSheet
      hero.inventory.i = hero.inventory.i+1
I also tried few things but I don't remember what, and nothing worked ...
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: How to set limit to a table ?

Post by grump »

Get the length of an array (a table with consecutive integer keys, starting at 1) with the # operator:

Code: Select all

canGetItem = #hero.inventory < 48
My guess for your second question is that you don't remove the musicSheet from the world and the player keeps colliding with it. Your use of table.insert is correct.
hamberge
Prole
Posts: 25
Joined: Wed Aug 16, 2017 2:55 pm

Re: How to set limit to a table ?

Post by hamberge »

Grump is probably right about the second one. The reason it's random probably has to do with the fact that your player is colliding with the invisible but still present musicSheet for a semi-random number of frames determined by the time the player moves over the musicSheet to the time the player moves out of collision with the musicSheet.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: How to set limit to a table ?

Post by Sir_Silver »

Just to provide an answer for the question that is your "how to set a limit to a table", one way you could do that is by using the __newindex metamethod, something like the following:

Code: Select all

local inventory = setmetatable({}, {
	__newindex = function(t, k, v)
		if type(k) == "number" and k <= 10 then
			rawset(t, k, v)
		end
	end
})

for i = 1, 100 do
	inventory[i] = i	
end

for k, v in pairs(inventory) do
	print(k, v)
end
If you run this code, you'll see that we are trying to insert 100 things into the inventory table, however, when we print out all of the pairs inside of the table we only have 10 pairs, effectively limiting the number of items that can be inserted into it.
Post Reply

Who is online

Users browsing this forum: No registered users and 87 guests