Game Development Community

Item Pickup and gui problem

by Caleb · in Technical Issues · 05/24/2006 (8:11 am) · 4 replies

I've got a problem interfacing my collision code and my gui.

I'm trying to add a simple gui display that will show how much silver I've collected. Every thing works fine until I run into a piece of silver. The object deletes like it should but when I look at my silver counter it always says 1 and it will never go higher. It's probably something simple but I can't seem to figure it out, so here is the code incase someone out there knows what's wrong.
datablock ItemData(Silver)
{
   category = "Silver";
   shapeFile = "~/data/shapes/customobj/coins/silvercoins/silvercoin.dts";
};

function ItemData::create(%data)
{
   %obj = new Item()
{
   datablock = %data;
   static = true;
};
}

//spot of problems
function Silver::onCollision(%this, %obj, %col)
{
  if (%col.getClassName() $= "Player")
   {
      %silver++;
      SilverCount.setText("Silver:" SPC %silver);
      %obj.delete();
   }
}

#1
05/24/2006 (8:29 am)
A while since I've scripted but I'm pretty sure %silver++ will be making a new LOCAL variable called silver and incrementing it by one,

You need to use $silver for a global variable that will remember its value outside the function.

Hope that helps

Nick
#2
05/24/2006 (8:34 am)
Better would be something like
%silver = SilverCount.getValue();
%silver++;
SilverCount.setValue(%silver);
#3
05/24/2006 (8:48 am)
Yes the Local variable % was the problem,
Yes the Globel variable $ fixed the problem
and yes I fell like an idiot.

Thanks Nick.
#4
05/24/2006 (9:16 am)
Just curious Anthony...why is that a better way? Better not to directly manipulate globals?