Game Development Community

Using Arrays to improve a tutorial - I need help

by Infinitum3D · in Torque Game Engine · 02/08/2007 (6:14 pm) · 3 replies

I've been using Tim Newell's Inventory Pop-Up Tutorial.

Here's the PopInventory function

Quote:

function clientCmdPopInventory(%InvData) {
//String Hack since I couldn't get arrays to pass
$InvWordCount = getWordCount(%InvData);

//reset the Bottom Slot to default
$CurrentBottomSlot = 4;
%InvWdCount = 0;
if ($InvWordCount > 0) {
for (%i = 0; %i < ($InvWordCount/2); %i++) {
$WeaponTokens[%i] = getWord(%InvData,%InvWdCount);
%InvWdCount++;
$AmmoTokens[%i] = getWord(%InvData,%InvWdCount);
%InvWdCount++;
}
}
if ($InvWordCount > 0) { //Slot 1
Slot1.setBitmap("GameOneFPS/client/ui/Inv/Inv" @ $WeaponTokens[0] @ ".png");
Slot1Desc.setText($WeaponTokens[0] @ " Ammo: " @ $AmmoTokens[0]);
} else {
Slot1.setBitmap("GameOneFPS/client/ui/Inv/InvEmpty.png");
Slot1Desc.setText("Inventory Slot Available");
}
if ($InvWordCount > 2) { //Slot 2
Slot2.setBitmap("GameOneFPS/client/ui/Inv/Inv" @ $WeaponTokens[1] @ ".png");
Slot2Desc.setText($WeaponTokens[1] @ " Ammo: " @ $AmmoTokens[1]);
} else {
Slot2.setBitmap("GameOneFPS/client/ui/Inv/InvEmpty.png");
Slot2Desc.setText("Inventory Slot Available");
}
if ($InvWordCount > 4) { //Slot 3
Slot3.setBitmap("GameOneFPS/client/ui/Inv/Inv" @ $WeaponTokens[2] @ ".png");
Slot3Desc.setText($WeaponTokens[2] @ " Ammo: " @ $AmmoTokens[2]);
} else {
Slot3.setBitmap("GameOneFPS/client/ui/Inv/InvEmpty.png");
Slot3Desc.setText("Inventory Slot Available");
}
if ($InvWordCount > 6) { //Slot 4
Slot4.setBitmap("GameOneFPS/client/ui/Inv/Inv" @ $WeaponTokens[3] @ ".png");
Slot4Desc.setText($WeaponTokens[3] @ " Ammo: " @ $AmmoTokens[3]);
} else {
Slot4.setBitmap("GameOneFPS/client/ui/Inv/InvEmpty.png");
Slot4Desc.setText("Inventory Slot Available");
}

Canvas.setContent( InventoryGui );
}


I'd like to change it from (as Tim puts it) a string hack to a true array passing function. The problem is... I'm not a programmer. I understand what an array is, but I don't understand how to script it. I'd like it set up so that inventory is unlimited, rather than limited to four slots. I could copy/paste this a hundred times and my limit would be four hundred slots, but obviously that's inefficient.

Does anyone have any suggestions on how to improve Tim's code for TGEv1.5?

Thanks!

Tony

ps. Thanks Tim for the excellent tutorial!

#1
02/09/2007 (6:50 am)
Could you provide a link to the tutorial you are using?

I take it Slot1, Slot2, Slot3, and Slot4 are GUI controls? You can keep track of them using a global array, if they are just client side.

$invSlots[0] = Slot1;
$invSlots[1] = Slot2;
$invSlots[2] = Slot3;
$invSlots[3]= Slot4;
$invSlotCount = 4
function clientCmdAddNewSlot(%pic, %description)
{
      $invSlots[$invSlotCount] = new GuiElementType(){ 
                                  bitmap = %pic;
                                  text = %description;
                                  };
      $invSlotCount++;
}

GuiElementType is a placeholder term for whatever GUI type you used to create Slot1, Slot2, ect.

This is one of the simpler ways to to handle dynamic GUI creation and management in an Array. I personally would go the C++ route or limit the # of slots, but this should work for you.

Keep in mind you still need to set up the size and positioning of the new slots, but this being somewhat pseudo-code should get you going.
#3
02/10/2007 (5:57 am)
This is what SimSet is good for ;p

$InventorySlots = new SimSet();

function clientCmdAddNewSlot( %pic, %desc )
{
    %t = new GuiElementType() { bitmap = %pic; text = %desc; };

    $InventorySlots.add( %t );
}

The great thing about that is that you can pass your list around and even into C++ code, etc.

To traverse the list:

function doSomethingWithIt()
{
    %cnt = $InventorySlots.getCount();

    for( %i = 0; %i < %cnt; %i++ )
    {
         %e = $InventorySlots.getObject( %i );

         ... do something with element...
    }
}