Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Glenjamin on Sat 16/07/2016 16:55:46

Title: Random generation help
Post by: Glenjamin on Sat 16/07/2016 16:55:46
I'm working on a game that has mechanics based upon random outcomes.

Essentially, every time the game is opened, it will have a small chance to randomly spawn an entity.

So basically every time the game loads there's a chance it will be a slightly different experience.

What is the simplest way I can execute this?

Thanks guys!
Title: Re: Random generation help
Post by: Retro Wolf on Sat 16/07/2016 17:22:08
Let's say I have a bad guy that I want to spawn randomly with random traits and position, I would do something like this:

Code (ags) Select

function spawn_baddy()
{
  int rand = Random(10);
 
  if (rand == 0)// one in ten chance
  {
    cBaddy.ChangeRoom(player.Room);// cBaddy is a character I've created
    cBaddy.x = Random(Room.Width);// places it randomly in my room
    cBaddy.y = Random(Room.Height);
    badGuyAggressiveness = (1 + Random(9));// will always be at least 1, this is a variable I would have made earlier.
  }
}

function on_event (EventType event, int data)
{
  if ((player.Room == 10) && (event == eEventEnterRoomBeforeFadein))
  {
    spawn_baddy()
  }
}
Title: Re: Random generation help
Post by: dayowlron on Sat 16/07/2016 22:57:40
What Retro Wolf said should work depending on what you are needing. All I have to add is if you want to do this at the start of each game I would put it in the game_start function.