Inventory Item +1 - Multiple identical inventory items.

Started by Chicky, Sat 19/12/2009 19:08:13

Previous topic - Next topic

Chicky

Hello far superior coders!

I have a simple problem that i don't really know what the best solution would be. I have 9 coins scattered across multiple locations, i simply want to have a number in the top right hand corner of the inventory graphics that increases when you collect each coin.

Would it be better to make 9 separate inventory items (each with unique graphics) and some sort of +1 script or is there an elite raw draw text scripting alternative? The coins can be collected in any order.

Thanks in advance!

monkey0506

#1
Elite raw draw scripting alternative you say?

Code: ags
DynamicSprite *coinSprite; // we'll be needing this since our sprite is after all dynamic

// player picks up coin:
player.AddInventory(iCoin); // give the player the coin
if (coinSprite != null) { // if our dynamic sprite has been set, we need to release the old one first
  if (iCoin.Graphic == coinSprite.Graphic) iCoin.Graphic = 0; // make sure we remove the reference to the sprite first
  coinSprite.Delete(); // release the sprite from memory
}
coinSprite = DynamicSprite.CreateFromExistingSprite(REGULAR_COIN_SPRITE); // copy the "raw" sprite
DrawingSurface *surface = coinSprite.GetDrawingSurface(); // get the surface to draw onto
String count = String.Format("%d", player.InventoryQuantity[iCoin.ID]); // store the number of coins the player has in a string
int width = GetTextWidth(count, Game.NormalFont) + PADDING_RIGHT; // get the total width used for aligning the text
surface.DrawingColor = COIN_COUNT_TEXT_COLOR; // set the text color
surface.DrawString(coinSprite.Width - width, PADDING_TOP, Game.NormalFont, count); // draw the number on the sprite
surface.Release(); // release the surface, updating the sprite
iCoin.Graphic = coinSprite.Graphic; // set the item's graphic
UpdateInventory(); // this may be necessary since the sprite graphic has changed


Values that need to be replaced are as follows:
REGULAR_COIN_SPRITE: The sprite slot number of the coin sprite without any number applied.
PADDING_RIGHT: How far from the right edge of the sprite the text should be.
PADDING_TOP: How far from the top edge of the sprite the text should be.
COIN_COUNT_TEXT_COLOR: The color the text should be drawn in.
And there you have it.

Edit: Haha Chicky is too proud to actually post here that he is using 2.72...but I'm not! :D

We'll have to use the old-school RawDraw techniques instead!

Code: ags
DynamicSprite *coinSprite; // we'll be needing this since our sprite is after all dynamic

// player picks up coin:
player.AddInventory(iCoin); // give the player the coin
if (coinSprite != null) { // if our dynamic sprite has been set, we need to release the old one first
  if (iCoin.Graphic == coinSprite.Graphic) iCoin.Graphic = 0; // make sure we remove the reference to the sprite first
  coinSprite.Delete(); // release the sprite from memory
}
RawSaveScreen(); // save the current room background
RawClearScreen(63519); // clear the screen to magic pink
RawDrawImage(0, 0, REGULAR_COIN_SPRITE); // draw the regular image onto the background so we can draw on top of it
String count = String.Format("%d", player.InventoryQuantity[iCoin.ID]); // store the number of coins the player has in a string
int width = GetTextWidth(count, Game.NormalFont) + PADDING_RIGHT; // get the total width used for aligning the text
RawSetColor(COIN_COUNT_TEXT_COLOR); // set the text color
RawPrint(Game.SpriteWidth[REGULAR_COIN_SPRITE] - width, PADDING_TOP, count); // draw the number on the sprite
coinSprite = DynamicSprite.CreateFromBackground(GetBackgroundFrame(), 0, 0, Game.SpriteWidth[REGULAR_COIN_SPRITE], Game.SpriteHeight[REGULAR_COIN_SPRITE]);
RawRestoreScreen(); // restore the normal background graphic
iCoin.Graphic = coinSprite.Graphic; // set the item's graphic
UpdateInventory(); // this may be necessary since the sprite graphic has changed


SMF spam blocked by CleanTalk