Game Development Community

BOOK: How to create use item events? Event handler doesn't have any player info

by Brandon · in Torque X 2D · 01/18/2009 (1:17 pm) · 3 replies

I can't figure out how to use this inventory manager. The code for chapter 9 calls the Use item, but does not have any examples of actually using items.

If I pick up a weapon for example and I want to use it how do I create the event that is called on use?

I see this code:
new TorqueEvent<string>(item.UseItemEventName);
                    TorqueEventManager.Instance.MgrPostEvent<string>
                    (useEvent, item.UseItemEventData);

So what do I do with this? Is the actual use item code supposed to be in this function and just use an if then structure and do some code based on what the event is or is there a way to create torque use events?

#1
01/18/2009 (6:39 pm)
Events are like specialized delegates. Basically you will have a publishing class that declares and raises the event. You will have a class that subscribes to this event, which will declare the event handler. All that is left is to declare a delegate (which has the same signature as your event handler) and register the code you want to call with the event.

The code you have typed is part of the whole picture. Just read up on events and delegates, then see how Toque X has implemented them (TorqueEvents).
#2
01/19/2009 (7:06 am)
Ok I found where this is actually used. The weapon component has a fireweapon event registered, so the pickup item then has that onUse event name set to fireweapon.

It seems to call the function correctly in the weapon component when I use the item, but now for whatever reason my projectile template in the weapon component is null, but I have it set.

If I call the fire weapon from my player it works fine. So I think it must be an ownership issue. Maybe in the fire weapon function of the weapon component I need to pass the TorqueObject Owner to it and find the component. Not sure how I'd do that though.
#3
01/19/2009 (9:13 am)
I tried adding the weapon component to the pickup item, but on use still has a null projectile.

Do I need to add a weapon component to the item itself? Or to the player?

Or maybe in my weapon component fire weapon function I need to pass it the name of the projectile, and player and get the scene objects by TorqueObjectDatabase.instance.findobject<t2dsceneobject>(projectile)


Edit: Nope nothing I'm trying is working.. need help plz