This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
pilotofsomething's profile picture

Can't get Items list to show

Started by pilotofsomething, 02 February 2016 - 04:18 AM
pilotofsomething #1
Posted 02 February 2016 - 05:18 AM
I'm working on an RPG, but when making the items list in battle it just won't show up.

Screenshot of what it looks like:
It should be showing the 2 items you start with.


Link to the Code
Bomb Bloke #2
Posted 02 February 2016 - 08:07 AM
Line 113:

if not player.inventory[i] == " " then

"not" inverts a value, turning false to true and most anything else to false. This line is hence equivalent to:

if false == " " then

You meant to do:

if player.inventory[i] ~= " " then
pilotofsomething #3
Posted 03 February 2016 - 02:26 AM
Line 113:

if not player.inventory[i] == " " then

"not" inverts a value, turning false to true and most anything else to false. This line is hence equivalent to:

if false == " " then

You meant to do:

if player.inventory[i] ~= " " then

Thanks, that fixed it.
This is what it looks like now, but is there anyway I could just have the empty slots not show?
Bomb Bloke #4
Posted 03 February 2016 - 06:32 AM
Well, you're currently setting your empty slots to "———-" and printing anything that isn't " ". Since nothing is " ", everything gets printed.

If a value in a table is undefined, then attempting to retrieve it gets you nil. In a comparison check, nil counts as false, and anything besides nil/false counts as true. Hence you could do this:

if player.inventory[i] then

You could then change the table declaration at line 25 to:

    inventory = {}

And any line which attempts to remove an item from the inventory to:

player.inventory[i] = nil

You'd also change the current line 80 to:

if not player.inventory[i] then
pilotofsomething #5
Posted 04 February 2016 - 12:57 AM
Well, you're currently setting your empty slots to "———-" and printing anything that isn't " ". Since nothing is " ", everything gets printed.

If a value in a table is undefined, then attempting to retrieve it gets you nil. In a comparison check, nil counts as false, and anything besides nil/false counts as true. Hence you could do this:

if player.inventory[i] then

You could then change the table declaration at line 25 to:

	inventory = {}

And any line which attempts to remove an item from the inventory to:

player.inventory[i] = nil

You'd also change the current line 80 to:

if not player.inventory[i] then

Thanks, but after a while of thinking I think I will show empty slots as "———–" since it looks nicer.