Tables not working right

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
LuaMaster
Prole
Posts: 13
Joined: Tue Jan 01, 2013 2:33 am

Tables not working right

Post by LuaMaster »

Okay, right off the bad, please do NOT point out crashes, bugs, or inefficient things unless they are DIRRECTLY related to the issue I'm having. I know where the crashes are. I will get to them later.

Anyhow, so I'm trying to delete object 2. But if there is a 3rd object there, it will ALWAYS delete the 3rd, even though I tell it to delete the 2nd.
How to reproduce:
Make three objects by puting the cursor in the positon then pressing p
Press D
Click deletemode
Press 2
Press enter
Wait
Then see that it deleted the object with the three above it.
Use the program I've attached in the post below and put the FOLDER named archive.love in your love folder.
LuaMaster
Prole
Posts: 13
Joined: Tue Jan 01, 2013 2:33 am

Re: Tables not working right

Post by LuaMaster »

Sorry I couldn't upload it, but you can use pastebin
Steps:
Get the program from pastebin.com/Gwc5Zxme
Paste it into a file called main.lua
Zip main.lua
Call it Archive.love
Then get this:
http://www.sendspace.com/file/iq6x8v
Unzip it, then place the contents in your LOVE folder.
Thanks for helping.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Tables not working right

Post by Ranguna259 »

I don't really recomend on using table.remove, use this instead:

Code: Select all

for i=numbertodeletefromtable,#table do
  table[i]=table[i+1]
end
Exemple usage:

Code: Select all

table={}
table[1]=1
table[2]=2
table[3]=3


for i=2,#table do
  table[i]=table[i+1]
end
EDIT: Don't worry that code wont go slow on large table, I managed to get 900+ FPS on a table of 800+ arrays, so it's good.

Why in hell's name don't you just post the love file, is it that hard ?
http://www.mediafire.com/download/7z6n6 ... elGUI.love
Guys use this file if you want to check out the OP's code ^
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
LuaMaster
Prole
Posts: 13
Joined: Tue Jan 01, 2013 2:33 am

Re: Tables not working right

Post by LuaMaster »

I had to upload the archive.love so you can actually run it
And what's the difference between the two?
Also, I really don't care about slowness. The program I'm using is meant to get a job done, but nothing else. Not to run fast or to look nice, etc. etc.
EDIT: I've changed the way it does it, now it gives me another error-- but it doesn't make any sence because I have set delNum to 2 yet it says it isn't a number. Help.(link, in love file: http://www.sendspace.com/file/0iugo5 )
Nevermind, I got it working. I just changed the variable name in a few spots but not all and it works fine now. And yes, I know, it's going to get slower and slower as I do that kind of stuff. It's okay.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Tables not working right

Post by Robin »

Ranguna259 wrote:I don't really recomend on using table.remove, use this instead:
Whyyyyy? What reason can you possibly have to do that?
Help us help you: attach a .love.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Tables not working right

Post by Ranguna259 »

Robin wrote:
Ranguna259 wrote:I don't really recomend on using table.remove, use this instead:
Whyyyyy? What reason can you possibly have to do that?
From here:
because it will move items several times when only one is needed. For instance if you remove every other entry in the array then a quadratic number of moves will be performed if you use table.remove
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Tables not working right

Post by micha »

Ranguna259 wrote:
because it will move items several times when only one is needed. For instance if you remove every other entry in the array then a quadratic number of moves will be performed if you use table.remove
Your code
Ranguna259 wrote:

Code: Select all

for i=numbertodeletefromtable,#table do
  table[i]=table[i+1]
end
Does the same. It moves several items around. And if you need to keep the order in the array, then I don't see a simple way around (maybe you can leave gaps).

If you don't care about the order, then you can replace the deleted entry by the last one.

Code: Select all

function remove(table,index)
  table[index] = table[#table]
  table[#table] = nil
end
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest