• 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!

Spawning Randomly

Status
Not open for further replies.
C

Cmi

Guest
So this code worked for me but after an update it started messing up. I want to randomly spawn an object with in the room but it spawns in the same pattern every time. here is the code:
// Spawn pie (random(x),random(y),obj_enemy
instance_create(random(608),random(-32),obj_enemy)

that's my code it just spawns in the same pattern every time even when I restart the game. Can you guys help me fix the code or get a better to spawn them better and randomly?

SOLVED :D

Create:
// Wait 2 seconds and than trigger the alarm
alarm [1] = 60;
// Randomize the spawn
randomize();

Step:
// Spawns the object
instance_create(irandom_range(0,608),irandom_range(0,-32),obj_test)
// Repeat this code
alarm[1] = 60;
 
Last edited by a moderator:
A

anomalous

Guest
you need to use randomize(); somewhere, look up in manual, default is same seed. Randomize will generate new seed each time.
 
C

Cmi

Guest
Welcome to GMC~♫

What is the first line of your code supposed to do? It isn't valid GML syntax.

Define "same pattern". Do the enemy instances show up in a line? If yes then there's some code forcing it to change position. Post the complete code.
By same pattern I mean they spawn the same every time. Like when I restart the game they spawn in the same locations like they did when I start the game
 
A

anomalous

Guest
Just put that line in the create of your controller before any random functions are called. It's worth a quick read, like 30 seconds in the manual.

Yes, random functions in GMS are defaulted to use the same random seed for debug purposes. You will see the same random on purpose.
If you instead put randomize(); in an event before that, it will pick a new seed each time, and you'll get more random results.
 
C

Cmi

Guest
Just put that line in the create of your controller before any random functions are called. It's worth a quick read, like 30 seconds in the manual.

Yes, random functions in GMS are defaulted to use the same random seed for debug purposes. You will see the same random on purpose.
If you instead put randomize(); in an event before that, it will pick a new seed each time, and you'll get more random results.
So I did it but they don't spawn within the ranges that I set, I was wondering if you can fix it. it's just am new to GM thats why I may sound like a newbie :(
here is the link: https://mega.nz/#!7I8mzTjS
 
C

Cmi

Guest
Welcome to the GMC~♫

What is the first line of your code supposed to do? It isn't valid GML syntax.

Define "same pattern". Do the enemy instances show up in a line? If yes then there's some code forcing it to change position. Post the complete code.

Edit: No. Call it once at the start of the game. Read about it here:

http://docs.yoyogames.com/source/dadiospice/002_reference/maths/real valued functions/randomize.html
Do you mind taking a look at it and fixing if you can?
Link: https://mega.nz/#!7I8mzTjS
 
A

Aura

Guest
Sadly I (most of us) don't have enough time to download a completely unknown file to inspect for helping you. But I'm more than happy to help if you post all the code that relates with spawning the instances. For example, what does the script pie() do? So share that sort of information then we'd be able to help. ^^

Also: Doesn't calling randomize() help?
 
C

Cmi

Guest
Sadly I (most of us) don't have enough time to download a completely unknown file to inspect for helping you. But I'm more than happy to help if you post all the code that relates with spawning the instances. For example, what does the script pie() do? So share that sort of information then we'd be able to help. ^^

Also: Doesn't calling randomize() help?
Sorry for the late reply I finally figured it out. I will post what I was trying to do.
 
A

{Anonymouscoder1}

Guest
Hi I'm new.

Every time I have this code in my enemy the game lags a ton what could I do about this?
 
Last edited by a moderator:
A

{Anonymouscoder1}

Guest
So this code worked for me but after an update it started messing up. I want to randomly spawn an object with in the room but it spawns in the same pattern every time. here is the code:
// Spawn pie (random(x),random(y),obj_enemy
instance_create(random(608),random(-32),obj_enemy)

that's my code it just spawns in the same pattern every time even when I restart the game. Can you guys help me fix the code or get a better to spawn them better and randomly?

SOLVED :D

Create:
// Wait 2 seconds and than trigger the alarm
alarm [1] = 60;
// Randomize the spawn
randomize();

Step:
// Spawns the object
instance_create(irandom_range(0,608),irandom_range(0,-32),obj_test)
// Repeat this code
alarm[1] = 60;


Hi could you put this into code form please
also my game starts to lag when I put this code in anything I could do to stop this from happening
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Necro-bumping a 2 year old thread won't get you much help... Please make your own topic and in it explain exactly what the issue you have is, what code you have that you think is causing the issue, and any examples of what you would like to happen over what is happening.
 
Status
Not open for further replies.
Top