Page 2 of 3

Re: Objects table(active/inactive) - removing and adding

Posted: Sat May 07, 2016 10:51 pm
by Strutyk
I'll try to explain.
I have four characters. Every 3 seconds a random character is taken from

Code: Select all

mansPool
Then the chosen random character gets to

Code: Select all

mansArray
and removed from

Code: Select all

mansPool
Then a random character moves.
When it reaches a certain point - is removed from the

Code: Select all

mansArray
and is added back into the

Code: Select all

mansPool
to participate in the random sample.
That is - until the character on the screen, it does not participate in a random sample of

Code: Select all

mansPool

Re: Objects table(active/inactive) - removing and adding

Posted: Sun May 08, 2016 12:49 am
by zorg
[wiki]love.math.random[/wiki]
table.insert, table.remove

Re: Objects table(active/inactive) - removing and adding

Posted: Sun May 08, 2016 9:39 am
by pgimeno
Ok, so it's not just one active man at a time, but something more like this:

Code: Select all

local function activateMan()
    if #mansPool == 0 then return end
    mansArray[#mansArray+1] = table.remove(mansPool, love.math.random(1, #mansPool))
end

local function deactivateMan(man)
    for i = 1, #mansArray do
        if mansArray[i] == man then
            mansPool[#mansPool+1] = table.remove(mansArray, i)
            break
        end
    end
end
You would call activateMan every 3 seconds. You would call deactivateMan each time a man reaches the certain point, passing it which one did.

Makes sense now?

Re: Objects table(active/inactive) - removing and adding

Posted: Sun May 08, 2016 10:00 am
by Strutyk
Today, I feel better and understood :awesome:
But I have a question. Is this good practice use this function to check?

Code: Select all

function checkActive()
	rand = mansPool[math.random(#mansPool)]
    if rand.active == true then 
    	return checkActive()
    end    
    randName = rand.name
    rand.active = true
    table.insert(mansArray, rand)
end

Re: Objects table(active/inactive) - removing and adding

Posted: Sun May 08, 2016 10:33 am
by pgimeno
If rand.active is set when you remove the man from mansPool, there should be no element with .active set to true in it. It makes no sense to check it. But then that code does not remove it, so I'm confused again.

Re: Objects table(active/inactive) - removing and adding

Posted: Sun May 08, 2016 10:57 am
by Strutyk
Yes this function is not remove man from pool. In my project I have two pool. In the first pool objects are added and removed. In the second poll objects are active, inactive. function checkActive() used for the second pool. But I have my doubts, when using this function. Is there a better option for check object(s) on the activie/inactive?

Re: Objects table(active/inactive) - removing and adding

Posted: Sun May 08, 2016 11:13 am
by ivan
Good example by pgimeno.
Personally, I would change the code a little bit:

Code: Select all

local pool = {}
local active = {}

-- initialize pool
for i = 1, 100 do
  table.insert(pool, {})
end

local function activate(x, y)
  local t = table.remove(pool) or {}
  -- re-initialize
  t.x = x
  t.y = y
  -- activate
  table.insert(active, t)
  return t
end

local function deactivate(t)
  for i = 1, #active do
    if active[i] == t then
      table.remove(active, i)
      table.insert(pool, t)
      break
    end
  end
end
The idea being - take the last element from the pool and re-initialize its values.

PS. This approach is useful when you need to activate a lot of objects (such as particles) at once.
math.random(#mansPool)
Just be careful since math.random will raise an error when #mansPool == 0.
second poll objects are active, inactive
Seems like one pool of inactive objects should be enough.

Re: Objects table(active/inactive) - removing and adding

Posted: Sun May 08, 2016 11:37 am
by Strutyk
Yep, mansPool is manual control - the number of objects on the screen - less than the size of the pool.
Second pool for dynamically added objects :)

Re: Objects table(active/inactive) - removing and adding

Posted: Sun May 08, 2016 11:43 am
by Strutyk
Maybe I need to use debug (memory usage, graphics, etc.), and then it will be clear which code is more appropriate for my situation?

Re: Objects table(active/inactive) - removing and adding

Posted: Sun May 08, 2016 12:23 pm
by Strutyk
Thank you all very much for help with tips and code, you helped me alot! Hoping the practice will come to understand where best to use what code :)