using same inventory item a number of times before losing it

Started by barefoot, Wed 02/12/2009 17:07:56

Previous topic - Next topic

barefoot

Hi

a loaded gun with 6 bullets is found and added to inventory.  The gun therefore can be used 6 times before it is automatically withdrawn for the inventory.

What would be the best way to script this?

any help appreciates

Barefoot
I May Not Be Perfect but I Have A Big Heart ..

Matti

Eezee:

// pick up gun:

int bullets=6;

// use gun:

if(bullets>0){
  bullets--;
  //shooting script
}

monkey0506

Code: ags
// global script
int bullets = 6;

// fires gun...wherever that might take place
if (bullets) { // shorthand for "bullets > 0"
  // shoot gun and stuff
  bullets--;
}
else player.Say("I'm out of bullets.");



Something like that would work. If you want the number of bullets to be global you can use the Global Variables pane instead of defining it in the global script. Or you can use import/export. Or you could create six unique strings (string-literals) to use with Game.DoOnceOnly and put it in a large, cumbersome if-else if-else statement. Or you could store the bullets directly as inventory items. Or various other methods. Depends on personal preference mostly.

Got beaten by Matti. Revenge is sweet, eh? :=

Intense Degree

Declare a variable called e.g. Bullets with a value of 6.

Then every time the player uses the gun:

1. Bullets = Bullets - 1
2. check If Bullets == 0 then remove it from the inventory

(Sorry for the pseudo code which is a result of lazyness!)

EDIT: Ah I see 2 Ninjas got in before I did and explained it better!


monkey0506

Quote from: Intense Degree on Wed 02/12/2009 17:17:381. Bullets = Bullets - 1

Just to clarify, all three of the following are equivalent:

Code: ags
bullets = bullets - 1;

Code: ags
bullets -= 1;

Code: ags
bullets--;


Again, this is down to preference. If you want to modify the value by more than just one you can use either of the first two methods. There are also += and ++ shorthand operators for your convenience. ;)

Quote from: Intense Degree on Wed 02/12/2009 17:17:38EDIT: Ah I see 2 Ninjas got in before I did and explained it better!

Ninja? Me? What? I don't know what you're talking about? I've never heard of the Christmas Ninja....I mean met. Of course I've heard of him. I mean...who hasn't. I mean...clearly I'm not him though. ::)

SMF spam blocked by CleanTalk