Tim Cooley
Tim Cooley

Reputation: 759

How to use two objects from a pooled list in one script?

I am trying to understand object pooling. I am able to get the script to pull one object at a time, but I need to be able to pull three or more from the list at the same time.

My object pooling script is big, so I don't really want to share the whole thing unless it is necessary.

I need to be able to change the location of the spawn of a flame, so I created a script to do that:

 private void CreateWavesForFlames(GameObject flame, float xBase, float xDisplacement, float dropHeight)
 {
    flame.transform.position = new Vector3(xBase + xDisplacement, dropHeight, 0);
    flame.SetActive(true); //this turn the pooled object on
} 

So I need to spawn three flames at the same time and change their spawn locations

The wave call would look something like this:

void Wave1() {
    Debug.Log("Wave1");
    tempGOHolder = gm.GetLargeFire();


    CreateWavesForFlames(tempGOHolder, 0, 0, 12);
    CreateWavesForFlames(tempGOHolder, 10, 0, 12);
    CreateWavesForFlames(tempGOHolder, 15, 0, 12);

}

What happens is only one fire flame is created and it uses the last CreatWavesForFlames. I need the three to be different.

Any suggestions on how to do this would be awesome.

Upvotes: 1

Views: 186

Answers (2)

Programmer
Programmer

Reputation: 125325

I know this has been answered but I think there is a better solution. The answer above is great. Assuming you want to make your code shorter by not repeatedly calling the GetLargeFire() function, you can use the method below.

Lets say that your GetLargeFire() function in your pool script looks like this:

GameObject GetLargeFire()
{
    return availableGameObject;
}

You can create an overload of the GetLargeFire() function that fills up an array that is passed to it. I wouldn't recommend returning array because that allocates memory, therefore making your pooling script useless. Filling up array that is passed in is better.

public void GetLargeFire(GameObject[] gOBJ, int amountToReturn)
{
    for (int i = 0; i < gOBJ.Length; i++)
    {
        if (i < amountToReturn)
        {
            gOBJ[i] = GetLargeFire();
        }
        else
        {
            //Fill the rest with null to override what was inside of it previously
            gOBJ[i] = null;
        }
    }
}

Now, to use it like the example you have in your question, you should declare tempGOHolder as an array and choose a number you you think is enough for it. We will use 3 for this example.

GameObject[] tempGOHolder;

void Start()
{
    tempGOHolder = new GameObject[3];
}

void Wave1() {
    Debug.Log("Wave1");
    gm.GetLargeFire(tempGOHolder, 3);
    CreateWavesForFlames(tempGOHolder[0], 0, 0, 12);
    CreateWavesForFlames(tempGOHolder[1], 10, 0, 12);
    CreateWavesForFlames(tempGOHolder[2], 15, 0, 12);
}

You can even use loop to create Waves with CreateWavesForFlames with less code.

Upvotes: 2

Sushant Poojary
Sushant Poojary

Reputation: 363

Well.. that is what is expected from your code. if you want 3 different flame objects, Then you are going to have to do this(assuming "gm" is your pool manager object):

tempGOHolder = gm.GetLargeFire();
CreateWavesForFlames(tempGOHolder, 0, 0, 12);

tempGOHolder = gm.GetLargeFire();
CreateWavesForFlames(tempGOHolder, 10, 0, 12);

tempGOHolder = gm.GetLargeFire();
CreateWavesForFlames(tempGOHolder, 15, 0, 12);

Upvotes: 1

Related Questions