• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Random objects at random, yet fixed, spawns

B

BladeGamez

Guest
Hey there. Total noob to the community, so I am guessing this is the appropriate place for asking about code. I have a party-like game where players go around an arena for a set amount of time and try to gather items and then are taken to a place where they can equip them and then fight to the death. I was wondering how would I go about programming it so that a random object would be selected out of all of the objects and then set in one of five locations at random (If you can also tell me how to put this in random intervals of time under ten seconds as I have a timer that moves on when a certain time goes by).

Any feedback will help!
 

Tthecreator

Your Creator!
Alright, first of all you would have to setup a list of items.
Have you looked into arrays in game maker? If you haven't I highly suggest you do! Without knowledge of arrays you won't be able to follow my explanation.
So setup a array as a list of objects:
Code:
items[0]=obj_sword
items[1]=obj_sword
items[2]=obj_my_sanity
Now you want to use the irandom(); function to get a random number between 0 and the highest entry in the array.
This happens to be 2 in my example, but if you want to get this programatically you can use array_length_1d() which will count the amount of items in an array, including 0, thus returning 3 for the examplearray. You will have to substract one of it giving you this code:
Code:
randomobject=items[irandom(array_length_1d(items)-1)]
Now you can use instance_create() for this.


From here, you have a few options on how to exactly implement it, but I'd suggest you use invisible spawning objects.
These objects would be invisible in your game, easy to put inside levels in the room editor and could contain the timers itself.
Practially put the code shown above in that spawner object.

To make it run at random times, you can again use the irandom() function together with some knowledge about the room_speed (look into this topic if you haven't already) and about alarms (look into what these do too).
Then in create you could say something like:
Code:
alarm[0]=irandom(room_speed*10)//room_speeds gives the amount of ticks in a single second. The ticks are practically how many times per second the step and draw events execute. Multiplying it by 10 will give you the amount of ticks in 10 seconds.
Then you'd spawn a randomly selected object using code shown above and instance_create()

Hope this puts you in the right direction.
 

TheouAegis

Member
I'd make a global array holding all the possible items that could spawn.
I'd make a global array holding all the possible positions to spawn them in.
In the Room Creation Code for each room in your game, modify the values of those arrays, then spawn the objects.
 
B

BladeGamez

Guest
Alright, first of all you would have to setup a list of items.
Have you looked into arrays in game maker? If you haven't I highly suggest you do! Without knowledge of arrays you won't be able to follow my explanation.
So setup a array as a list of objects:
Code:
items[0]=obj_sword
items[1]=obj_sword
items[2]=obj_my_sanity
Now you want to use the irandom(); function to get a random number between 0 and the highest entry in the array.
This happens to be 2 in my example, but if you want to get this programatically you can use array_length_1d() which will count the amount of items in an array, including 0, thus returning 3 for the examplearray. You will have to substract one of it giving you this code:
Code:
randomobject=items[irandom(array_length_1d(items)-1)]
Now you can use instance_create() for this.


From here, you have a few options on how to exactly implement it, but I'd suggest you use invisible spawning objects.
These objects would be invisible in your game, easy to put inside levels in the room editor and could contain the timers itself.
Practially put the code shown above in that spawner object.

To make it run at random times, you can again use the irandom() function together with some knowledge about the room_speed (look into this topic if you haven't already) and about alarms (look into what these do too).
Then in create you could say something like:
Code:
alarm[0]=irandom(room_speed*10)//room_speeds gives the amount of ticks in a single second. The ticks are practically how many times per second the step and draw events execute. Multiplying it by 10 will give you the amount of ticks in 10 seconds.
Then you'd spawn a randomly selected object using code shown above and instance_create()

Hope this puts you in the right direction.
Sorry for the late reply! I've been working all day. Back to the main issue, will the array be inside of a script? Or would it be in the player object? Thanks in advance
 
N

Newt

Guest
Sorry for the late reply! I've been working all day. Back to the main issue, will the array be inside of a script? Or would it be in the player object? Thanks in advance
I'm not the guy who you responded to, but, the array should go (yet doesn't have to) into an separate object, so you could make an object called "obj_SpawnControl," put the array in the create event of it, and you are set. Make sure to put the line "globalvar items;" in order to initialize the array, and so that it can be accessed from any object in the game. You can think of arrays as just variables.
 
Top