Game Development Community

Anyone used delegates?

by Brandon · in Torque X 2D · 01/19/2009 (5:13 pm) · 4 replies

I'm still trying to figure out the best way to use items. I was thinking i should create an individual component for each item that handles it's use and each one of them register for an event so that then inventory manager can just fire the event on use.

The book touches on this on page 187 but then says "(More on this later)" but the code is commented out and it doesn't really go into it later.

But anyway the commented out code seems to lead to the fact that you have to create a use delegate somewhere.

Does the event need this to get the proper owner of the component that it is going to fire?

Right now I can fire an event fine, but all the component data is null or else the default value because i guess it doesn't know who owns it or who is calling it.

Can anyone shed any light on this at all? Maybe I'm looking at it the wrong way.

#1
01/19/2009 (9:09 pm)
If I'm reading this right (and I may not be -- not much sleep last night) I had the same problem. It all boils down to is that you need to know which player index is raising the fire event but there is no real way to do this from the weapon component.

I got around this by making use of UseItemEventData. What I did was attach an index to each of my players. Now this is a bit more involved than just assigning them a number as you'll want to assign them the index associated with their controller (the XNA site has more on this), but to start you can just assign player one an index of zero.

Now, in your inventory manager component item pickup is processed in AddItem which clones the item your player just collided with. One of the properties of each pickup item is UseItemEventData. Just dump your player index into this property.

If you followed the book you are already sending UseItemEventData when you fire an event:

TorqueEventManager.Instance.MgrPostEvent<string>(useEvent, item.UseItemEventData);

So your weapon component now has the player index in your event handler. Mine looks like this:

protected void FireEventHandler(string eventName, string eventData)
{
   // int.Parse parses a string into an int - string "10" becomes int 10
   FireWeapon(int.Parse(eventData));
}

FireWeapon then looks like this:

public void FireWeapon(int playerIndex)
{
   if (_projectileTemplate != null)
   {
      T2DSceneObject player = PlayerManager.Instance.GetPlayer(playerIndex).ControlObject as T2DSceneObject;
      // Rest of firing code here
   }
}

As for creating projectiles, you need to clone the projectile template ala:

T2DSceneObject projectile = _projectileTemplate.Clone() as T2DSceneObject;

I'd highly recommend downloading the source code from torquexbook.com as that's what I used to figure all this stuff out.
#2
01/20/2009 (6:30 am)
Thanks. I have 1 player and 1 NPC right now so I just tried to get the player control in the fire weapon function, but for whatever reason when i get the weapon component it takes some configurations from the player and others from the npc..

i'm confused.

here is what i added to my fire weapon

T2DSceneObject player = PlayerManager.Instance.GetPlayer(0).ControlObject as T2DSceneObject;
               WeaponComponent weapon = player.Components.FindComponent<WeaponComponent>();
               System.Diagnostics.Debugger.Break();
               T2DSceneObject projectile = weapon._projectileTemplate.Clone() as T2DSceneObject;

When I do the break my weapon._projectileTemplate is always null, but my ShotSpeed is correct (I changed it in the TXB to 1600) however my hitpoints come from the NPC weapon component!!! I changed it to 50 and weapon.HitPoints = 50!??

soo..

Something is not right.
#3
01/20/2009 (7:29 am)
Ah I figured out the problem. The fire event handler was changing my projectile template to null.

I think the rest is just reworking the code to tie into everything else correctly and my odd cross weapon component problem will go away.

Thanks for the help.
#4
01/20/2009 (9:33 am)
Sweet! Don't you just love it when you figure something like this out?