End game when player has certain items, picked up in a random order

winusoft

I'd imagine this has been asked before, but please could someone point me in the right direction?

I'd like to print a message once a player has picked up 10 certain items. They can be collected in any order, so I can't attach an 'if' to the last item, as I don't know which one the last one will be.

I assume I could add an 'if' to each of the 10 items, to check if the remaining 9 are in the inventory, but that seems a bit long winded. Is there a cleaner way to do this?


Doctor Agon

If you add an attribute to the player object, lets say call it "item". Set it as an integer.
Then for each of the 10 items on your list, in the script for 'After taking the object' put in this:

player.item = player.item + 1

It adds 1 for each item picked up, a similar thing (- 1) for dropped items.
Add a turn script to be enabled at the start of the game:

if (player.item = 10) {
  msg ("Congratulations.")
  finish
}

Or similar congratulatory message. Hope this helps.


hegemonkhan

you can use the boolean logic operators:

https://en.wikipedia.org/wiki/Truth_table (for what you want: you use the 'and' boolean logic operator, see below)

this design requires that you have all ten items in your inventory (within your 'player' Player Object) at the same time to win:

if (Contains (player, NAME_OF_ITEM_1) and Contains (player, NAME_OF_ITEM_2) and Contains (player, NAME_OF_ITEM_3) and Contains (player, NAME_OF_ITEM_4) and Contains (player, NAME_OF_ITEM_5) and Contains (player, NAME_OF_ITEM_6) and Contains (player, NAME_OF_ITEM_7) and Contains (player, NAME_OF_ITEM_8) and Contains (player, NAME_OF_ITEM_9) and Contains (player, NAME_OF_ITEM_10)) {
  msg ("You collected the ten items and won the game!")
  msg ("GAME OVER")
  finish
}

when you have a lot of items (we never want to type more than we have to! laughs. Let the computer do it for you, instead of you manually typing in every single code line/block for every item/combination/scenerio/situation) ... and/or for a better design, you would use iteration (foreach) and List/Dictionary Attributes, but this is a bit more advanced...

foreach (object_variable, game.items_needed_to_win_game_objectlist_attribute) {
  if (Contains (player, object_variable)) {
    list remove (game.items_needed_to_win_game_objectlist_attribute, object_variable)
  }
}
if (ListCount (game.items_needed_to_win_game_objectlist_attribute) = 0) {
  msg ("You collected the ten items and won the game!")
  msg ("GAME OVER")
  finish
}

winusoft

That's great, thank you guys!

I've tested it on one item and that's working. I've updated it for all items and now need to go round and collect them all to test it.

Cheers!


Log in to post a reply.

Support

Forums