Jump to content


Photo

How to make objects spawn randomly


  • Please log in to reply
10 replies to this topic

#1 GameVC

GameVC

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8.1

Posted 28 November 2012 - 07:12 PM

In my game objects fall randomly and the player has to avoid them. I need the objects to only spawn in one specific room. I don't know how to program, however I need this for my coursework.

I found the following code on another forum post:

Alarm[0]=100 //it is about 4 seconds


instance_create(object0,random(room_height),0) // if you want that new object appears at random position
Alarm[0]=100 //so it will create new object every 4 seconds



speed=4


if y>room_height
instance_destroy()


Edit: I have made the objects fall vertically, however even when I put the repeat action in, the event only happens once?

Also, the only way to make the objects spawn is to place one in the room, which just stays stationary. :/

Edited by GameVC, 28 November 2012 - 07:31 PM.

  • 0

#2 vidokas

vidokas

    GMC Member

  • GMC Member
  • 293 posts
  • Version:GM8

Posted 28 November 2012 - 07:36 PM

In my game objects fall randomly and the player has to avoid them. I need the objects to only spawn in one specific room. I don't know how to program, however I need this for my coursework.

I found the following code on another forum post:


Alarm[0]=100 //it is about 4 seconds


instance_create(object0,random(room_height),0) // if you want that new object appears at random position
Alarm[0]=100 //so it will create new object every 4 seconds



speed=4


if y>room_height
instance_destroy()


Edit: I have made the objects fall vertically, however even when I put the repeat action in, the event only happens once?

Also, the only way to make the objects spawn is to place one in the room, which just stays stationary. :/


make create event
and make action in it
set Alarm[0]=100

make Alarm[0] event
and write action
instance_create(object0,random(room_height),0) // if you want that new object appears at random position
Alarm[0]=100 //so it will create new object every 4 seconds
and after code
add action set Alarm[0]=100
  • 0

#3 GreatExarch

GreatExarch

    GMC Member

  • GMC Member
  • 78 posts
  • Version:GM8.1

Posted 28 November 2012 - 07:42 PM

One thing you could do is have a separate object that does the spawning. This object wouldn't be visible, but would create instances of the falling rocks (or whatever).

The spawner would do this:

//Goes in Create event:
alarm[0]=90 //in 30 FPS, this is 3 seconds

//Goes in alarm[0]:
instance_create(obj_rock,random(room_width),0)

alarm[0]=90

Meanwhile, the falling object would work like this:

//Goes in create event:
vspeed=4

//Goes in step event:
if (y>room_height)
   instance_destroy()

  • 0

#4 GameVC

GameVC

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8.1

Posted 28 November 2012 - 07:42 PM


In my game objects fall randomly and the player has to avoid them. I need the objects to only spawn in one specific room. I don't know how to program, however I need this for my coursework.

I found the following code on another forum post:


Alarm[0]=100 //it is about 4 seconds


instance_create(object0,random(room_height),0) // if you want that new object appears at random position
Alarm[0]=100 //so it will create new object every 4 seconds



speed=4


if y>room_height
instance_destroy()


Edit: I have made the objects fall vertically, however even when I put the repeat action in, the event only happens once?

Also, the only way to make the objects spawn is to place one in the room, which just stays stationary. :/


make create event
and make action in it
set Alarm[0]=100

make Alarm[0] event
and write action
instance_create(object0,random(room_height),0) // if you want that new object appears at random position
Alarm[0]=100 //so it will create new object every 4 seconds
and after code
add action set Alarm[0]=100



Thanks for the reply. This works, however how do I make the objects spawn randomly in the x direction?
  • 0

#5 GreatExarch

GreatExarch

    GMC Member

  • GMC Member
  • 78 posts
  • Version:GM8.1

Posted 28 November 2012 - 07:49 PM

Change room_height to room_width.
  • 0

#6 Rtyp06

Rtyp06

    GMC Member

  • GMC Member
  • 1858 posts
  • Version:GM:Studio

Posted 28 November 2012 - 07:51 PM



In my game objects fall randomly and the player has to avoid them. I need the objects to only spawn in one specific room. I don't know how to program, however I need this for my coursework.

I found the following code on another forum post:


Alarm[0]=100 //it is about 4 seconds


instance_create(object0,random(room_height),0) // if you want that new object appears at random position
Alarm[0]=100 //so it will create new object every 4 seconds



speed=4


if y>room_height
instance_destroy()


Edit: I have made the objects fall vertically, however even when I put the repeat action in, the event only happens once?

Also, the only way to make the objects spawn is to place one in the room, which just stays stationary. :/


make create event
and make action in it
set Alarm[0]=100

make Alarm[0] event
and write action
instance_create(object0,random(room_height),0) // if you want that new object appears at random position
Alarm[0]=100 //so it will create new object every 4 seconds
and after code
add action set Alarm[0]=100



Thanks for the reply. This works, however how do I make the objects spawn randomly in the x direction?


x = random(room_width);
  • 0

#7 GameVC

GameVC

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8.1

Posted 28 November 2012 - 08:00 PM

Change room_height to room_width.



Thanks, but the objects still fall from the same x position but in random y positions?
  • 0

#8 GameVC

GameVC

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8.1

Posted 28 November 2012 - 08:07 PM




In my game objects fall randomly and the player has to avoid them. I need the objects to only spawn in one specific room. I don't know how to program, however I need this for my coursework.

I found the following code on another forum post:


Alarm[0]=100 //it is about 4 seconds


instance_create(object0,random(room_height),0) // if you want that new object appears at random position
Alarm[0]=100 //so it will create new object every 4 seconds



speed=4


if y>room_height
instance_destroy()


Edit: I have made the objects fall vertically, however even when I put the repeat action in, the event only happens once?

Also, the only way to make the objects spawn is to place one in the room, which just stays stationary. :/


make create event
and make action in it
set Alarm[0]=100

make Alarm[0] event
and write action
instance_create(object0,random(room_height),0) // if you want that new object appears at random position
Alarm[0]=100 //so it will create new object every 4 seconds
and after code
add action set Alarm[0]=100



Thanks for the reply. This works, however how do I make the objects spawn randomly in the x direction?


x = random(room_width);


What about at the same y height but at different x points? Thanks for this help by the way, you've ended weeks of frustration :)

Also this seems to continue ad infinitum, and when I try to put an if statement before it to stop too many spawning it spawns very slowly and objects just build up in the x 0?
  • 0

#9 Pancratius

Pancratius

    GMC Member

  • GMC:Member
  • 55 posts
  • Version:GM8

Posted 28 November 2012 - 08:10 PM

Use this:

instance_create(object0,random(room_width),0) // if you want that new object appears at random position
alarm[0]=4*room_speed //so it will create new object every 4 seconds


instance_create() has the arguments: Object, xposition, yposition, and it creates the object specified at the coordinate (xposition, yposition) so the xposition argument needs to be made into your random number.

as for the alarm, I noticed that you used alarm[0] = 100, if you want the alarm to be 4 seconds all the time you should use 4*room_speed. This is because room_speed is the amount of times per second that the room is redrawn (aka frames per second.) so by using 4*room_speed the alarm will run every 4 seconds.

Edited by Pancratius, 28 November 2012 - 08:13 PM.


#10 GameVC

GameVC

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8.1

Posted 28 November 2012 - 09:05 PM

I have one final question, how do I limit the maximum number of objects on the screen at anyone time? Because currently it is impossible for the player to avoid them.
  • 0

#11 Rtyp06

Rtyp06

    GMC Member

  • GMC Member
  • 1858 posts
  • Version:GM:Studio

Posted 28 November 2012 - 11:31 PM

I have one final question, how do I limit the maximum number of objects on the screen at anyone time? Because currently it is impossible for the player to avoid them.


Check instance_number() when you create the objects and only allow to create more instances if there aren't too many already.

if instance_number( obj ) <=10 then
{
     instance_create(x,y,obj);
}

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users