Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: cdavenport on Sat 01/06/2019 18:23:24

Title: Randomized Location of Items
Post by: cdavenport on Sat 01/06/2019 18:23:24
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.
Title: Re: Randomized Location of Items
Post by: Khris on Sun 02/06/2019 20:15:26
Here's one way to do this:

#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

  if (Game.DoOnceOnly("golden cabinet in green maze room")) GetTreasure("golden cabinet");
  else Display("I've checked that already.");
Title: Re: Randomized Location of Items
Post by: cdavenport on Mon 03/06/2019 15:24:35
Quote from: Khris on Sun 02/06/2019 20:15:26
Here's one way to do this:

In your interaction handlers, do

  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?
Title: Re: Randomized Location of Items
Post by: Khris on Tue 04/06/2019 11:14:14
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.