pickleplayer Posted January 15, 2016 Share Posted January 15, 2016 So I've been learning how to use tables (something I really should have done a loooong time ago) And I'm re-working a redoing the entire hitbox system with tables, Except now not all of the hurtboxes are being removed when this function is called. I set up some print functions to help me figure out what was going on, but I'm getting VERY conflicting outputs. function Hurtboxes:ResetHurtboxes() for k,v in pairs(self.hurtboxtable) do print("STOP YOUR LIES", v, k) end for k,v in pairs(self.hurtboxtable) do print("HURTBOX TABLE", v, k) table.remove(self.hurtboxtable, k) v:Remove() end end When the code runs, the first print output shows that there are 5 objects in the table. But then the second one runs, and suddenly it shows that there are only 3 objects in the table, and only those 3 get deleted. Leaving the other two on the field to get deleted next time ResetHurtboxes() runs again. If I comment out the part that actually removes the objects from the table, then they all get removed just fine. But then the game gets real laggy REAL fast. Am I not using the tables correctly? Why do this print out two different values for the exact same table? I have the exact same system set up for hitboxes and it seems to work just fine. Link to comment https://forums.kleientertainment.com/forums/topic/62525-my-tables-are-lying-to-me/ Share on other sites More sharing options...
Mobbstar Posted January 15, 2016 Share Posted January 15, 2016 Good question. You are not allowed to remove them in this loop. Here's why: table.remove() changes the keys of other elements. Let's say the loop picks element #3 first. It removes it, #4 becomes #3, #5 becomes #4. Now, there is no fifth element anymore, so the loop won't even try to access it. Why would it? That thing is gone and no longer in the table! Solution? Try "table[k] = nil" or "table = {}" Link to comment https://forums.kleientertainment.com/forums/topic/62525-my-tables-are-lying-to-me/#findComment-709582 Share on other sites More sharing options...
pickleplayer Posted January 15, 2016 Author Share Posted January 15, 2016 37 minutes ago, Mobbstar said: Good question. You are not allowed to remove them in this loop. Here's why: table.remove() changes the keys of other elements. Let's say the loop picks element #3 first. It removes it, #4 becomes #3, #5 becomes #4. Now, there is no fifth element anymore, so the loop won't even try to access it. Why would it? That thing is gone and no longer in the table! Solution? Try "table[k] = nil" or "table = {}" oooohhhhhh okay I see. Cool! now it works! Thanks! Link to comment https://forums.kleientertainment.com/forums/topic/62525-my-tables-are-lying-to-me/#findComment-709615 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now