• Advertisement
Sign in to follow this  

[C#] Delegates and passing parameters

This topic is 2771 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

I have the following delegate:


public delegate T CreateNewObjectGame(Game1 game);


and would like to use it in the following method:


public ResourcePool(int allocation, ValidateObject objectCheck, CreateNewObjectGame objectCreate, bool isWrappable)
{
this.isWrappable = isWrappable;
this.objectCheck = objectCheck;
numberOfInvalidObjects = allocation;
objects = new T[allocation];

for (int i = 0; i < allocation; ++i)
{
T t = objectCreate();
objects = t;
}
}



The problem is that objectCreate() require game to be passed into the method but this is already the case with this:


poolItem = new ResourcePool<Item>(200, Item.IsValid, Item.Instance(this), true);


The 'this' refers to the Game1 class being passed into the delegate. So how can I then access the class when calling objectCreate without having to pass in the class again via another argument?

Share this post


Link to post
Share on other sites
Advertisement
I am not quite sure what you are trying to do but I suspect the design to be wrong.

Please provide more context: what class is containing

poolItem = new ResourcePool<Item>(200, Item.IsValid, Item.Instance(this), true);

It looks like this is refering to yet another constructor of the ResourcePool class. How is this constructor related to this method?:

public ResourcePool(int allocation, ValidateObject objectCheck, CreateNewObjectGame objectCreate, bool isWrappable)

Many times when you notice that you are passing an object from method to method and you are adding it to each and every parameter list the design is flawed.


Share this post


Link to post
Share on other sites
The ResourcePool class contains the following variables:


/// A pre-allocated pool of objects with the ability to sort
/// and maintain objects as they become invalidated.
public class ResourcePool<T> where T : class
{
bool isWrappable;
int wrapIndex;
int numberOfInvalidObjects;
ValidateObject objectCheck;
T[] objects;



It pools objects of different classes.

The problem I face is that each class needs the Game1 class passed into it during the constructor stage.

ValidateObject and CreateNewObjectGame for an ammo class looks like this:


public static bool IsValid(IAmmo ammo)
{
return ammo.isAlive;
}

public static PlasmaAmmo Instance(Game1 game)
{
return new PlasmaAmmo(game);
}



That means that:


T t = objectCreate();


should be


T t = objectCreate(game);


but how do I get the Game1 game passed in correctly using delegates?

Share this post


Link to post
Share on other sites
Just pass your Game1 instance into your ResourcePool method

public ResourcePool(int allocation, ValidateObject objectCheck, Game1 game,
CreateNewObjectGame objectCreate, bool isWrappable)
{
this.isWrappable = isWrappable;
this.objectCheck = objectCheck;
numberOfInvalidObjects = allocation;
objects = new T[allocation];

for (int i = 0; i < allocation; ++i)
{
T t = objectCreate(game);
objects = t;
}
}




and call it like this

poolItem = new ResourcePool<Item>(200, Item.IsValid, Item.Instance, this, true);


assuming Item.Instance has a method signature that conforms to the CreateNewObjectGame delegate.

Share this post


Link to post
Share on other sites
Thanks Chaos, I was went down this path after reading through my code and changing it.

I was hoping that I didn't have to pass the Game1 parameter into the pool but I guess there is no way otherwise.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spa8nky
Thanks Chaos, I was went down this path after reading through my code and changing it.

I was hoping that I didn't have to pass the Game1 parameter into the pool but I guess there is no way otherwise.


Well, you could change your CreateNewObjectGame delegate to

public delegate T CreateNewObjectGame()


and then call your function like this


poolItem = new ResourcePool<Item>(200, Item.IsValid,
delegate() { Item.Instance(this); },
true);


or even easier with a lambda

poolItem = new ResourcePool<Item>(200, Item.IsValid,
() => Item.Instance(this),
true);


This depends on how CreateNewObjectGame will effect the rest of your code.

Share this post


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

  • Advertisement