Objects table(active/inactive) - removing and adding

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.
Strutyk
Prole
Posts: 13
Joined: Fri Apr 22, 2016 7:21 pm

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

Post 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
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post by zorg »

[wiki]love.math.random[/wiki]
table.insert, table.remove
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3641
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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?
Strutyk
Prole
Posts: 13
Joined: Fri Apr 22, 2016 7:21 pm

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

Post 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
User avatar
pgimeno
Party member
Posts: 3641
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
Strutyk
Prole
Posts: 13
Joined: Fri Apr 22, 2016 7:21 pm

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

Post 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?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post 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.
Last edited by ivan on Sun May 08, 2016 12:10 pm, edited 1 time in total.
Strutyk
Prole
Posts: 13
Joined: Fri Apr 22, 2016 7:21 pm

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

Post 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 :)
Strutyk
Prole
Posts: 13
Joined: Fri Apr 22, 2016 7:21 pm

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

Post 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?
Strutyk
Prole
Posts: 13
Joined: Fri Apr 22, 2016 7:21 pm

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

Post 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 :)
Post Reply

Who is online

Users browsing this forum: immortalx and 3 guests