Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Shavit on Wed 23/10/2013 21:39:22

Title: How to display hw much money I have?
Post by: Shavit on Wed 23/10/2013 21:39:22
Is there a way to display in the inventory - below the money image I have, the number of coins I have?

When I managed to do is when I'll look at they  I'm displaying the number of coins but it's not proffesional enough.

Thank you!
Title: Re: How to display hw much money I have?
Post by: Khris on Wed 23/10/2013 22:14:19
This one's a bit more complicated.

Code (ags) Select
DynamicSprite *inv_money_sprite;
int inv_money_sprite_sprite;

void updateMoneySprite() {
  // change these if necessary
  int w = invCustomInv.ItemWidth, h = invCustomInv.ItemHeight;  // invCustomInv: inventory window
  InventoryItem *i = iMoney;   // iMoney: inventory item representing money
  int m = money;               // money: global variable storing amount of money
  int font = eFontFont0;        // font used for amount

  if (inv_money_sprite == null) {
    inv_money_sprite = DynamicSprite.Create(w, h);
    inv_money_sprite_sprite = i.Graphic;
  }
  DrawingSurface *ds = inv_money_sprite.GetDrawingSurface();
  ds.Clear(0);
  ds.DrawImage(0, 0, inv_money_sprite_sprite);
  ds.DrawingColor = 15;
  int ww = GetTextWidth(String.Format("%d", m), font);
  ds.DrawString(w/2-ww/2, h - 12, font, "%d", m);
  ds.Release();
  i.Graphic = inv_money_sprite.Graphic;
  UpdateInventory();
}


Put this at the top of the global script.
Look at the first few lines, they use some values I guessed at. Change them if necessary.
In order to use this function, just call it after you have changed the amount of money the player has.
Example:
Code (ags) Select
  // player finds 10 dollars:
  money += 10;
  updateMoneySprite();


If you need it in room scripts, add this line to GlobalScript.ash:
Code (ags) Select
import updateMoneySprite();
Title: Re: How to display hw much money I have?
Post by: Shavit on Sun 27/10/2013 19:53:58
I'm sorry for the delayed response - I had some problem with my computer...

Thank you very much!

It opened my eyes to the possibilities in AGS :shocked: