Item Randomizer [SOLVED]

Started by Stranga, Thu 24/09/2020 08:00:41

Previous topic - Next topic

Stranga

Hello everyone,

I'm looking at making an item randomizer to change up replay-ability. I have an idea in mind but unsure how to execute it. I'm wondering if there's a way to check if the player already has that item then give them a different random item. I'm designing certain sections where you progress differently and thought this may be something cool to add to the game. 

I found some code (which I've tweaked a little) in an older forum which gives the player random items but doesn't really check if they already have that item and give them a different one.

Code: ags


#define ITEM_COUNT 15  // index 0 - 14
InventoryItem* item[ITEM_COUNT];

function game_start()
{
  item[0] = iRoom4Key;
  item[1] = iRoom2Key;
  item[2] = iStoreKey;
}


void GetItem( ) {
  int randomIndex;
  while (true) {
    randomIndex = Random(ITEM_COUNT - 1);
    if (item[randomIndex]) break;
  }
    Display("Item: %s found!", item[randomIndex].Name);

    player.AddInventory(item[randomIndex]);
    item[randomIndex] = null;
}




Any help will be greatly appreciated :)


Stranga

#2
Thanks Kris, but where would I add this to the code here? I wanted to avoid adding it on every interaction if possible.

My apologies, I definitely overlooked this one!

Here's the code if anyone's interested.

Code: ags

#define ITEM_COUNT 15  // index 0 - 14
InventoryItem* item[ITEM_COUNT];

function game_start()
{
  item[0] = iRoom4Key;
  item[1] = iRoom2Key;
  item[2] = iStoreKey;
}


void GetItem( ) 
{
  int randomIndex;
  while (true) 
  {
    randomIndex = Random(ITEM_COUNT - 1);//Pick an item at random
    if (item[randomIndex]) break;
  }
    if (!player.HasInventory(item[randomIndex]))//Check if we don't already have the item
    {   
      Display("Item: %s found!", item[randomIndex].Name);//Display the item found
      player.AddInventory(item[randomIndex]);
      item[randomIndex] = null;
    }
}

Khris

Sorry for being brief before; I was in the middle of something.

If you have a fixed set of items and you're clearing the array element after giving it to the player, I don't think you need to check whether they already have it anyway.
Because a) they might have lost it in the meantime and b) the array element being null already means they've received it previously.

So as far as I can tell, your original code was fine; did it not work as intended?

Stranga

Not a problem, Khris :)

It sorta worked, it was giving me random items as intended but when I added all items to the inventory for testing and it was still giving me items.

Khris

If you can get those same random items somewhere else you can just use the same function, and therefore the same array. This will make sure you don't get any of the items twice.
If you can get iRoom2Key somewhere else, just set item[1] = null at the same time, and again, duplicate item prevented.

The fact that you get them twice after doing Ctrl-S doesn't mean that'll happen during non-debug gameplay. Checking the inventory is less reliable than checking a variable, and you already have the variables in place.

SMF spam blocked by CleanTalk