Randomized Location of Items

Started by cdavenport, Sat 01/06/2019 18:23:24

Previous topic - Next topic

cdavenport

Are there any old threads or pointers that come to mind if you wanted to randomize the locations of where a set number of different objects were found throughout the game?

If I had 9 different objects, and at the start of every new game, I wanted to assign them to appear in randomized location before the game starts, is there an efficient way to do that without a lot of complicated coding? Or is there an old thread that someone remembers that references a mechanic like this I can take a look at...so far I'm not finding anything.

Thanks for any help or suggestions.

Khris

Here's one way to do this:

Code: ags
#define TREASURE_COUNT 15  // index 0 - 14
InventoryItem* treasure[TREASURE_COUNT];

  // in game_start
  treasure[0] = iGold;
  treasure[1] = iIngot;
  treasure[2] = iCrown;
  treasure[3] = iNothing;
  treasure[4] = iMask;
  treasure[5] = iNothing;
  // ...

// in global script
void GetTreasure(String containerName) {
  int randomIndex;
  while (true) {
    int randomIndex = Random(TREASURE_COUNT - 1);
    if (treasure[randomIndex]) break;
  }
  if (treasure[randomIndex] == iNothing) Display("The %s is empty!", containerName);
  else {
    Display("You find a %s!", treasure[randomIndex].Name);
    player.AddInventory(treasure[randomIndex]);
  }
  treasure[randomIndex] = null;
}


In your interaction handlers, do

Code: ags
  if (Game.DoOnceOnly("golden cabinet in green maze room")) GetTreasure("golden cabinet");
  else Display("I've checked that already.");

cdavenport

Quote from: Khris on Sun 02/06/2019 20:15:26
Here's one way to do this:

In your interaction handlers, do

Code: ags
  if (Game.DoOnceOnly("golden cabinet in green maze room")) GetTreasure("golden cabinet");
  else Display("I've checked that already.");


Thanks Khris I'll try this out, I don't think I'm understanding how to implement the Game.DoOnceOnly - so the containerName would be the object the player clicks on to see what's inside (e.g. old wooden chest)? So if in a room you have an object named oOldWoodenChest, and the player clicks on it to interact, the above code would go there?

Khris

Yes, exactly. The  GetTreasure  function relies on being only called exactly X times during the game, where X is the number of containers.
Otherwise it'll enter an infinite loop because the treasure array is already filled with  null.

To ensure that, the simplest way is to use  Game.DoOnceOnly  when the player checks a container while supplying a unique string each time.
The text passed to  GetTreasure  is only used to create a suitable "x is empty" message, in case the container turns out to be empty from the start.

SMF spam blocked by CleanTalk