patterncsharpMinor
Pooling System: is it worth improving or not for a simple implementation?
Viewed 0 times
simpleworthsystempoolingforimplementationnotimproving
Problem
I am using Unity3D and I made a super simply pooling system to reuse a specific object. My only concern is I am not sure if I am doing it the best possible way. If I were doing this in C++ there would be a bunch of pointers and be passing pointers to the objects.
Now in C# I understand many things are hidden as pointers. So, I am unclear whether the things if I should just return things, thinking it is by value or do it by reference in C#'s way of using 'ref'.
What I mainly want to achieve is preventing me from duplicating the objects, but rather passing the memory address of where my objects are.
Hoping someone could provide some insight.
Here is the code: (probably most interest would be in
```
using UnityEngine;
using System.Collections;
public class PoolingSystem : MonoBehaviour
{
[SerializeField]
// Keeping track of which units to create and put into a pool
private GameObject _object;
// Root container for the spawned objects given in the inspector
private GameObject _root;
[SerializeField]
// Number of objects to keep track for in the pool
private int _num_obj;
// Number of objects currently in use
private int _num_used;
// Keep track of _pooled objects
private GameObject[] _pool;
// Keep track of which index in the _pool is being used
private bool[] _in_use;
// Default vector to move objects away from the scene
private Vector3 _default;
///
/// Gets the object in which the pooling system is focusing on
///
/// The object.
public GameObject pool_obj
{
get { return _object; }
}
///
/// Gets the total amount of objects this pooling system should restrict to.
///
/// The total.
public int total
{
get { return _num_obj; }
}
///
/// Gets the current number of objects in use by the pool system for the specific object
///
/// The number_in_use.
public in
Now in C# I understand many things are hidden as pointers. So, I am unclear whether the things if I should just return things, thinking it is by value or do it by reference in C#'s way of using 'ref'.
What I mainly want to achieve is preventing me from duplicating the objects, but rather passing the memory address of where my objects are.
Hoping someone could provide some insight.
Here is the code: (probably most interest would be in
GetObject and ReturnObject methods```
using UnityEngine;
using System.Collections;
public class PoolingSystem : MonoBehaviour
{
[SerializeField]
// Keeping track of which units to create and put into a pool
private GameObject _object;
// Root container for the spawned objects given in the inspector
private GameObject _root;
[SerializeField]
// Number of objects to keep track for in the pool
private int _num_obj;
// Number of objects currently in use
private int _num_used;
// Keep track of _pooled objects
private GameObject[] _pool;
// Keep track of which index in the _pool is being used
private bool[] _in_use;
// Default vector to move objects away from the scene
private Vector3 _default;
///
/// Gets the object in which the pooling system is focusing on
///
/// The object.
public GameObject pool_obj
{
get { return _object; }
}
///
/// Gets the total amount of objects this pooling system should restrict to.
///
/// The total.
public int total
{
get { return _num_obj; }
}
///
/// Gets the current number of objects in use by the pool system for the specific object
///
/// The number_in_use.
public in
Solution
Classes, such as your
Structs and primitive types such as
Therefore you can remove the
GameObject are passed by reference (i.e. as a pointer) by default. No duplication happens and you do not need a keyword for that. C# handles it all for you.Structs and primitive types such as
Vector3 or int are value types which are passed by value by default and are therefore copied when you pass them into functions.Therefore you can remove the
ByRef keywords whenever you're passing your pooled object around for clarity at the least, and do not have to worry about duplicated objects unless you're passing a struct (which are meant to be small anyway) or a primitive type (which again, should be small).Context
StackExchange Code Review Q#46353, answer score: 2
Revisions (0)
No revisions yet.