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

[Solved] Lua Instance Help!

Started by LeDark Lua, 12 June 2015 - 07:33 AM
LeDark Lua #1
Posted 12 June 2015 - 09:33 AM
Hey PRO's I made a simple OO in Lua: http://pastebin.com/3fhthkxt
Well this thingy has a problem, I dont know how to do like this fo ex I have 10 instances:

1: Inst
2: Inst
3: Inst
4: Inst
5: Inst
And so on…

And for ex I delete 3 instance and so that I would save the PC from lag I need to make thoose 4 and 5 instances be 3 and 4
Well this is my delete instance code: http://pastebin.com/AjP8Pann
Soo yeah I want to know how I could realocate the instances from 4 5 to 3 4!

Oh one thing, if I had 500 instances and I deleted some instances from the middle, I would try to update the middle 150-250 instances but they would be "nil" and I would skip them, and thats where the lag comes in, thats why I need to delete adn realocate the instances If you didnt get it!
Edited on 13 June 2015 - 10:01 AM
Creator #2
Posted 12 June 2015 - 10:03 AM
Why would you relocate them?
LeDark Lua #3
Posted 12 June 2015 - 10:05 AM
@Creator simply to save memory. If I will have 100 instances then my pc will lag and I have deleted 50 instance, the instance count is still 100 and not 99.
Edited on 12 June 2015 - 08:07 AM
Bomb Bloke #4
Posted 12 June 2015 - 10:36 AM
It's barely been an hour, no need to bump. :P/>

Anyway, it sounds like you're after table.remove().

http://www.lua.org/pil/19.2.html
LeDark Lua #5
Posted 12 June 2015 - 04:59 PM
Table remove works but with bugs, like one of the instances will delete randomly…
Edited on 13 June 2015 - 05:51 AM
MKlegoman357 #6
Posted 12 June 2015 - 05:51 PM
Could you give us a code example of how you're using table.remove?
Lignum #7
Posted 12 June 2015 - 06:22 PM
Honestly, I wouldn't worry about lag. Any modern computer will execute thousands of iterations in no time. If I recall correctly, using pairs() should skip the nil values anyway.

But if you insist on doing it anyway, please post your code as MKlegoman357 said.
LeDark Lua #8
Posted 12 June 2015 - 07:22 PM
I use it like: table.remove(objs.insts, t.id)
Creator #9
Posted 12 June 2015 - 07:57 PM
table.remove(table,key)
LeDark Lua #10
Posted 12 June 2015 - 09:09 PM
objs.insts[num]=inst_id
This is what I do when I create the instance!
When I remove it:
local temp_id=t.id
table.remove(objs.insts, temp_id)
MKlegoman357 #11
Posted 12 June 2015 - 11:06 PM
You shouldn't be worrying about memory issues here. Lua is optimized enough to handle hundreds of tables without any lag issues.

Anyway, the problem is that it's not table.remove that is bugging but rather your code and the way your object IDs work. You are using those IDs to index your instance table, but whenever you remove an item from the table all the items above the table go down, thus you can no longer trust the ID. Here, I'll visualize what I mean:


--# Before table.remove:

t = {
  [1] = { id = 1 };
  [2] = { id = 2 };
  [3] = { id = 3 };
  [4] = { id = 4 };
  [5] = { id = 5 };
}

table.remove( t, 3 )

--# After table.remove:

t = {
  [1] = { id = 1 };
  [2] = { id = 2 };
  [3] = { id = 4 };
  [4] = { id = 5 };
}

You see what happens here? All the other items move - change their index inside the table, but IDs don't change. There's even more problems with this:


# continuing the above example

c = table.getn( t ) --> 4

n = c + 1 --> 5

t[n] = { id = n }

# table after inserting a new item:

t = {
  [1] = { id = 1 };
  [2] = { id = 2 };
  [3] = { id = 4 };
  [4] = { id = 5 };
  [5] = { id = 5 };
}

# Now you have two items with the same ID

What you should be doing is making the ID something more unique, maybe a string like "Player" or "Enemy". That way you will get more precise control over what item you want to alter. And unless you're going to be having millions of tables, don't worry about performance issues.




You can use the length operator (#) instead of table.getn:


local t = { 1, 2, 3 }

print( #t ) --> 3

For default values, you can have them like this:


local function add ( a, b )
  a = a or 2
  b = b or 1

  return a + b
end

print( add( 2 ) ) --> 3
print( add( nil, 5 ) ) --> 7

Also, 'repeat' is very similar to 'while' except that it first runs the body and only then checks the condition. It's very helpful when you need to get a certain event to fire:


print( "Press [Enter] to continue..." )

repeat
  local event, key = os.pullEvent( "key" )
until key == keys.enter or key == keys.numPadEnter

print( "You pressed [Enter] key!" )

The for loop in here:


function instanceDestroy(t)
  if(t.inst==true) then
    local temp_id=t.id
    objs.insts[temp_id]=nil
    for k in pairs(t) do <-- right here
      t[k] = nil
    end
  end
end

Shouldn't be needed. Lua handles with tables that are not referenced anywhere by itself, no need to do it yourself.
Edited on 12 June 2015 - 09:08 PM
LeDark Lua #12
Posted 13 June 2015 - 07:47 AM
Ok, I made a short program in Love2D ( cuz bandicam is bad at recording minecraft tbh and it would crash on my pc ), my instancing works in Love2D but I get the same result as in CC. Soo yeah, you can clearly see that one of thoose instances gets destroyed randomly. HELP!
Video showing of the bug:
Video Here
Edited on 13 June 2015 - 05:50 AM
Bomb Bloke #13
Posted 13 June 2015 - 09:44 AM
Funny, I don't see the script you used to make that video. That makes it rather difficult to comment on it.
LeDark Lua #14
Posted 13 June 2015 - 10:10 AM
You dont see the video or you dont see the script of that program I made in that video? Or you want my new instance script?
Edited on 13 June 2015 - 08:19 AM
Bomb Bloke #15
Posted 13 June 2015 - 10:51 AM
I see the video. I don't see the script. I suggest showing both the full Love2D and the full ComputerCraft scripts you're using.
LeDark Lua #16
Posted 13 June 2015 - 11:34 AM
New Instance code here: http://pastebin.com/8szPgRFK
Tell me whats wrong?

Oh sorry, there is moveToDirection code…
Edited on 13 June 2015 - 09:54 AM
Bomb Bloke #17
Posted 13 June 2015 - 11:52 AM
Well, MKlegoman357 has already explained what's wrong; I guess you're asking for another explanation on how to fix it.

"Quick and dirty" would be to just do this:

function instanceDestroy(t)--Instance destroy
        if(t.created==true) then--If im created
                local temp_id=t.id--get the instances id
                objs.insts[temp_id].created=false--Say im not created
                for k in pairs(t) do--Reset the table
                        t[k]=nil
                end
                table.remove(objs.insts, temp_id)--Remove 't' from the table

                for k = temp_id, #objs.insts do  -- Go through the following objects,
                	objs.insts[k].id = k     -- ... and correct their "id" keys to match their new table positions.
                end
        end
end

… but personally, I'd do away with the id keys altogether, and devise a system whereby you refer to your instances by eg their position in the table instead of trying to embed identification information into them.
LeDark Lua #18
Posted 13 June 2015 - 11:59 AM
Thanks Bomb Bloke, your code worked!

Expect this API in CC :)/>
Edited on 13 June 2015 - 10:08 AM