Countable object?

Started by Bijulinus, Thu 14/06/2007 16:46:53

Previous topic - Next topic

Bijulinus

Hello, I was wandering how to make items countables, like money, like in MI.
An item called "n dollars" and when you use it, n gets lower. How can it be done?

I hope I did not post in the wrong place. THanks
I'm too lazy at the moment to find a good signature.

strazer

There are many ways, but you could use an inventory item like this:

1.) Create an inventory item called Dollars. Its script-o-name will be iDollars.

2.) Creating custom functions:

Code: ags

// main global script

function GiveMoney(int amount) {
  player.InventoryQuantity[iDollars.ID] += amount; // add dollars
  iDollars.Name = String.Format("%d dollars", player.InventoryQuantity[iDollars.ID]); // update inv item name
  UpdateInventory();
}

function LoseMoney(int amount) {
  if (player.InventoryQuantity[iDollars.ID] - amount < 0) {
    player.InventoryQuantity[iDollars.ID] = 0; // avoid negative value
  }
  else player.InventoryQuantity[iDollars.ID] -= amount; // substract dollars
  iDollars.Name = String.Format("%d dollars", player.InventoryQuantity[iDollars.ID]); // update inv item name
  UpdateInventory();
}

function GetMoney() {
  return player.InventoryQuantity[iDollars.ID];
}


Code: ags

// main global script header

// Make functions usable in room scripts:
import function GiveMoney(int amount);
import function LoseMoney(int amount);
import function GetMoney();


3.) Using the functions:

Code: ags

  GiveMoney(100);


Code: ags

  if (GetMoney() < 100) {
    Display("You don't have enough money!");
  }
  else {
    LoseMoney(100);
    player.AddInventory(iThing)
    Display("You've bought the thing!");
  }


(Untested)

Bijulinus

Ah, great! I'm no good with scripts at this time...
... sorry for wrong place posting. THanks for help
I'm too lazy at the moment to find a good signature.

Khris

For future reference, and because Strazer's method isn't feasible if somebody wants to display multiple inv items multiple times, it can be done using a global variable, too:

Code: ags
// main global script

int money;

function GiveMoney(int amount) {
  money += amount; // add dollars
  iDollars.Name = String.Format("%d dollars", money);
}

function LoseMoney(int amount) {
  if (amount > money) return false;
  else money -= amount; // substract dollars
  iDollars.Name = String.Format("%d dollars", money);
}

(code borrowed from Strazer)

Using it:
Code: ags
if (LoseMoney(100)) {
  player.AddInventory(iThing);
  Display("You've bought the thing!");
}
else Display("You don't have enough money!");

Meystari F

Let say I am going to bank and I have only 5 dollar with me in the inventory and I want to withdraw 500 dollars out from the bank I would have then 505 dollar.

I just want to know what is the code for raising and lowering money.

I am also trying to make a GUI wish is suppose to display how much amount of money I have left.  And I don't know how to do it.

It could be useful for everyone if they also learn how to do this!  ;D


Ashen

#5
Topics merged, because they're pretty similar. Fribbi, as it suggests in the BFAQ, using an Inventory Item is the easiest way to track money. However, strazer and Khris' code could be adapted to have money without using an Item; just delete the iDollars.Name lines, and have GetMoney return the money int (to be displayed in GUIs, etc).
I know what you're thinking ... Don't think that.

Meystari F

So there is my thread. Lucky me I was browsing this thread.  I was already wondering why my new thread I made today was deleted. But I thought I had said something wrong or the admins don't like my threads here.  :P Stupid me thinking like that hehehehe! ;)

But thanks anyway. ;D

Meystari F

#7


I have a problem with this little GUI.

As you can see on this image I have GUI on the left bottom of the screen with a number "0" .  That number are suppose to display the amount of the money the player has. But I don't know how to do let it happen.

I tried that code below and it only works with the inventory. But I need to know how I can display number ..I mean how much money the player has in the GUI.

Should I use button  for the number or label for that ?

About the smiley.
I  want to make the smiley show some emotion in each time you get richer or poorer. But if I have low money I want that smiley display unhappy face and when the player are richer I want to let the smiley face smile. I still don't know how to do it so I need a little help with that too.

btw Kr is a short word for kronur wich in your language means coins.

Sorry about my english if there is something you didn't understand let me know about it too.

strazer

If you use a GlobalInt to store the amount of money you have, you can simply set the GUI label's text to @GIx@ where x is the number of the GlobalInt, for example: @GI7@

Guess where you can find out how to use GlobalInts? ;)

Ashen

Or, since strazer's original post had this function:
Code: ags

function GetMoney() {
  return player.InventoryQuantity[iDollars.ID];
}

(And like I suggested, you could alter that to return the int if you're not using Inventory at all.)

You could just format the Label text to display the amount, as with any int amount:
Code: ags

lblShowCash.Text = String.Format("%d", GetMoney()); // replace 'lblSowCash' with the right Label name


Put that in repeatedly_execute, or add it to the Get/LoseMoney functions (make sure they're declared after GetMoney), to keep the display up to date.
I know what you're thinking ... Don't think that.

Meystari F


Meystari F

#11
God dammit! Not again! Why is it always happening to me? Unfortunatly I had to format my hard disk with all my stuff in because I could not go in my computer otherwise so yes everything was deleted including my game. My DVD writer didn't work so I could not take a backup. The error had corrupted so many files so I had no other choice than format my hard disk and install my windows xp again. And well it worked. But yeah it is all gone. Just my unluck again. :'( That's my fourth-five time it happens to me when I am so close to the finish. God or whatever who is letting this happens to me are really evil to me. >:(

So I have sad news no Business Quest by me are coming out. ....but maybe I could be wrong about that. I have heard you could compile ags games into ags files.  If that is true it would be wonderful. I can then again continue on my work on my game. But if that is wrong well I am trully sorry about this. I was so close in this time finishing my first game. It had only one background. For cry sake I could not finish The Black Sky, The wrong Game 1 and 2, Leisure suit Larry 3 VGA and Leisure suit Larry 9 and now I can't even finish shortest game I have ever made wich toke only one background. Please don't laugh at me. I hate this too. But once again only a demo of my game will remain here in silence. You can stil download business Quest in demo section page. But I will remove that of course and replace another game after I have got my computer repaired. ::)

Put please don't delete this wonderfully and valuable thread because I am going to use it again. ;)

SMF spam blocked by CleanTalk