Random generation help

Started by Glenjamin, Sat 16/07/2016 16:55:46

Previous topic - Next topic

Glenjamin

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!

Retro Wolf

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

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()
  }
}

dayowlron

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.
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

SMF spam blocked by CleanTalk