Help with Scripting Inventory
by Nick Zafiris · in Torque Game Engine · 09/03/2004 (3:18 am) · 7 replies
Hi all, I need a little help for the inventory system I'm making. The GUI part of it is working, so far I have created a new GUI control guiItemButtonCtrl, which is basically a variation of guiBitmapButtonCtrl and with right-click functionality added.
The way I have I've scripted the GUI is that when you press "i" the inventory pops up but the GUI only displays items that the player is carrying. In other words I don't have empty boxes or squares showing up. Also, when you click on one item you get a little pop up menu with different options available. Here's a pic:
NOTE: I'm using Beffy's images temporarily for testing purposes.
So far I've managed to make it so that when you collide with an object, an image of the object shows up in the inventory as shown in the pic using an "addItem" function, which is being called from the item's onCollision function.
However, how do I keep track of the items collected? For example, when I right-click on the item I want a description to pop up for that specific item. I am not planning on having duplicate items or weapons in my game, only one of each except for health kits and ammo. And since most items will not be picked up from the ground but instead will given from other characters, I need to be able to set certain flags on them. It's a single player action/RPG.
So you think it's a good idea to use a separate datablock per item or to keep an array of instances/objects of the items?
I'm not asking for someone to design the core part of the inventory system, just to give me a push in the right direction and I think it will benefit others as well.
This is how I initialize the inventory:
In game.cs, after the player object has been created
Then in a separate file (invMgr.cs):
Any help will be appreciated.
Nick
Edit: Updated link
The way I have I've scripted the GUI is that when you press "i" the inventory pops up but the GUI only displays items that the player is carrying. In other words I don't have empty boxes or squares showing up. Also, when you click on one item you get a little pop up menu with different options available. Here's a pic:
NOTE: I'm using Beffy's images temporarily for testing purposes.
So far I've managed to make it so that when you collide with an object, an image of the object shows up in the inventory as shown in the pic using an "addItem" function, which is being called from the item's onCollision function.
However, how do I keep track of the items collected? For example, when I right-click on the item I want a description to pop up for that specific item. I am not planning on having duplicate items or weapons in my game, only one of each except for health kits and ammo. And since most items will not be picked up from the ground but instead will given from other characters, I need to be able to set certain flags on them. It's a single player action/RPG.
So you think it's a good idea to use a separate datablock per item or to keep an array of instances/objects of the items?
I'm not asking for someone to design the core part of the inventory system, just to give me a push in the right direction and I think it will benefit others as well.
This is how I initialize the inventory:
In game.cs, after the player object has been created
// Create the Inventory Class
%player.inv = new ScriptObject(){
class = InvManager;
};
%player.inv.init();Then in a separate file (invMgr.cs):
function InvManager::init(%this)
{
echo ( "///// Initializing InvManager" );
exec ( "~/client/scripts/inventoryDlg.cs" );
exec ( "~/client/ui/inventoryDlg.gui" );
exec ( "~/client/ui/itemDescDlg.gui" );
moveMap.bind ( keyboard, i, inventoryHud );
}
function InvManager::addItem(%this)
etc...Any help will be appreciated.
Nick
Edit: Updated link
#2
Also, am I right for creating the InvManager class as shown above and having all functions related to the inventory under that class or is it not necessary?
Thanks,
Nick
09/07/2004 (12:11 am)
Thanks, I think I'll go with the array. But I'm having trouble figuring out how I would index the array of ScriptObjects. Can you help me with the syntax a little? It's how to start that is more difficult that the problem itself.Also, am I right for creating the InvManager class as shown above and having all functions related to the inventory under that class or is it not necessary?
Thanks,
Nick
#3
Or do you have a more specific problem..?
09/07/2004 (2:31 pm)
$foo[2] = "something"; echo($foo[2]);
Or do you have a more specific problem..?
#4
How would you store and index ScriptObjects into an array?
Nick
09/07/2004 (10:36 pm)
Yes I meant the syntax to what you suggested above:Quote:...then stuff ScriptObjects containing the data I wanted to store into the array.
How would you store and index ScriptObjects into an array?
Nick
#5
Like that?
09/07/2004 (11:32 pm)
$foo[2] = new ScriptObject() { someField = "someData"; };
echo($foo[2].someField);Like that?
#6
The index "%item" refers to the GuiItemButtonCtrl name of that specific item. After working it a bit, I was able to link the item image in the inventory to the actual object. That way when I right-click on the item image gui, a description pops up specific to that item.
Thanks Ben for helping me on this. Don't know if I've done everything correctly but it works and I understand a lot more know.
Nick
09/08/2004 (1:17 pm)
Great! That's what I was looking for. I see arrays are pretty flexible. This is how I did it. I pass the fields I know I'm going to need from the item object picked up to a ScriptObject which is stored in the global array like this:$itemArray[%item] = new ScriptObject()
{
description = %obj.description;
invImage = %obj.invImage;
static = %obj.static;
};The index "%item" refers to the GuiItemButtonCtrl name of that specific item. After working it a bit, I was able to link the item image in the inventory to the actual object. That way when I right-click on the item image gui, a description pops up specific to that item.
Thanks Ben for helping me on this. Don't know if I've done everything correctly but it works and I understand a lot more know.
Nick
#7
09/09/2004 (9:50 am)
Glad you got it figured out. :)
Associate Kyle Carter