Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[C#] Pools and lists

This topic is 3167 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I have a pool of decals, which can be obtained as follows:
        public T GetNewValidObject()
        {
            if (numberOfInvalidObjects > 0)
            {
                return objects[--numberOfInvalidObjects];
            }
            else if (isWrappable)
            {
                numberOfInvalidObjects = objects.Length;
                return objects[--numberOfInvalidObjects];
            }

            return null;
        }

When obtaining an object from the pool and then storing it in a separate list, is there any way of checking the list for the object faster than as follows:
Decal decal = Game1.Instance.pool_Decal.GetNewValidObject();
                    int index = decals.IndexOf(decal);

                    if (index == -1)
                    {
                        decals.Add(decal);
                    }
                    else
                    {
                        decals[index] = decal;
                    }

Share this post


Link to post
Share on other sites
Advertisement
I wish there were some way of avoiding this altogether; this is what I find is the problem with pools.

If I want to create a separate list from the objects as they are created and the pool "IsWrappable" then I have to check the list each time for the decal index rather than just replacing the index directly based off the pool index.

The pool index decrements as the list index increments.

[Pool]
objects[--numberOfInvalidObjects];


    decals[++index];

    Is there any way I can set the change the list based off the pool index? A numerical trick that I'm missing perhaps?

    EDIT:

    This covers wrappable pools:


    public T GetNewValidObject(out int index)
    {
    index = objects.Length - numberOfInvalidObjects;

    if (numberOfInvalidObjects > 0)
    {
    return objects[--numberOfInvalidObjects];
    }
    else if (isWrappable)
    {
    numberOfInvalidObjects = objects.Length;
    return objects[--numberOfInvalidObjects];
    }

    return null;
    }



    [Edited by - Spa8nky on April 12, 2010 10:47:48 AM]

Share this post


Link to post
Share on other sites
I've rewrote the code to cover wrappable pools with correct indexing.

Ignore the previous method, it was rubbish ;)


/// <summary>
/// Creates a new valid object to fill the spot of an invalidated
/// object if possible. Returns null if all objects are valid.
/// </summary>
/// <returns>A reference to the next valid object, or null if all space is being used.</returns>
public T GetNewValidObject()
{
if (numberOfInvalidObjects > 0)
{
// Reset the wrap index to oldest object when there are invalid (unused) objects
wrapIndex = objects.Length;
return objects[--numberOfInvalidObjects];
}
else if (isWrappable)
{
if (wrapIndex == 0)
{
wrapIndex = objects.Length;
}

// Overwrite the oldest object in the pool if there are no free objects
return objects[--wrapIndex];
}

return null;
}



This problem can be considered solved if there are no objections.

Thanks guys.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!