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

Legacy GM [SOLVED] Moving Instance + Random Chance + Alarm?

M

MintyPretzel126

Guest
I would prefer to be taught in GML, but Drag 'n Drop is okay. So I have an object in the game I want to have make moving instances after about 3-4 seconds, and make the object random. I'm not really used to alarms or random chances. Also how would I make the game know when I have a certain amount of objects? (And make it display them on a border on screen?) Thanks a bunch, even if you just read this post.
 
Last edited by a moderator:

YoSniper

Member
So, I'll start easy. To create a random objects every 3 seconds, you could do something like this:

Alarm 0 Event
Code:
var obj_index;
obj_index = choose(obj1, obj2, ...);
instance_create(x, y, obj_index);

//Reset the timer for 3 seconds later
alarm[0] = 3 * room_speed;
For the sake of simplicity, I would highly recommend that all of the possible objects you create have a common parent. Let's call it "obj_parent."

You could then count the number of such instances in the room with the call
Code:
instance_number(obj_parent)
As for drawing all of the instances on the border, you could have something like this:
Code:
var ii;
ii = 0;
with(obj_parent) {
    draw_sprite(sprite_index, image_index view_xview + ii * HORIZONTAL_INCREMENT, view_yview);
    ii += 1;
}
Note that that would draw ALL of the existing objects on the border, and you could well run out of room.

Anyway, this should get you started.
 
M

MintyPretzel126

Guest
So, I'll start easy. To create a random objects every 3 seconds, you could do something like this:

Alarm 0 Event
Code:
var obj_index;
obj_index = choose(obj1, obj2, ...);
instance_create(x, y, obj_index);

//Reset the timer for 3 seconds later
alarm[0] = 3 * room_speed;
For the sake of simplicity, I would highly recommend that all of the possible objects you create have a common parent. Let's call it "obj_parent."

You could then count the number of such instances in the room with the call
Code:
instance_number(obj_parent)
As for drawing all of the instances on the border, you could have something like this:
Code:
var ii;
ii = 0;
with(obj_parent) {
    draw_sprite(sprite_index, image_index view_xview + ii * HORIZONTAL_INCREMENT, view_yview);
    ii += 1;
}
Note that that would draw ALL of the existing objects on the border, and you could well run out of room.

Anyway, this should get you started.
Thanks! I haven't tested it out yet, but I'll reply back tomorrow if I have any trouble. Also does the alarm restart automatically or do I have to have that symbol there?
 

YoSniper

Member
Thanks! I haven't tested it out yet, but I'll reply back tomorrow if I have any trouble. Also does the alarm restart automatically or do I have to have that symbol there?
Don't know which symbol you mean, but yes, the alarm should reset itself each time. That's what the last line
Code:
alarm[0] = 3 * room_speed;
does. Without that line, the alarm does not reset, and would have to be set again externally.
 
M

MintyPretzel126

Guest
Don't know which symbol you mean, but yes, the alarm should reset itself each time. That's what the last line
Code:
alarm[0] = 3 * room_speed;
does. Without that line, the alarm does not reset, and would have to be set again externally.
Okay, I got that part. Now I want to make a moving instance of obj_index with a direction of 270 (down I think,) and a speed of 5. Also is there a way I can choose which object gets chosen more?
 

YoSniper

Member
Okay, I got that part. Now I want to make a moving instance of obj_index with a direction of 270 (down I think,) and a speed of 5. Also is there a way I can choose which object gets chosen more?
Sure. Remember that instance_create also returns the ID of the new instance. You can then use that to manipulate the objects.
As for making some objects more frequent than others, you might want to make up a system like I show below. Some would recommend just repeating certain arguments in the choose function to increase the likelihood of some outcomes, but choose() can only take up to 16 arguments. Let's make this as maleable as possible.

Code:
var obj_index, chance;
chance = random(100);
if chance < 30 {
    //30% chance of choosing obj1
    obj_index = obj1;
} else if chance < 60 {
    //30% chance of choosing obj2 (60 - 30 = 30)
    obj_index = obj2;
} else if chance < 80 {
    //20% chance of choosing obj3
    obj_index = obj3;
} else {
    //Remaining 20% chance of choosing obj4
    obj_index = obj4;
}

var new_inst;
new_inst = instance_create(x, y, obj_index);
new_inst.direction = 270;
new_inst.speed = 5;
 
M

MintyPretzel126

Guest
Sure. Remember that instance_create also returns the ID of the new instance. You can then use that to manipulate the objects.
As for making some objects more frequent than others, you might want to make up a system like I show below. Some would recommend just repeating certain arguments in the choose function to increase the likelihood of some outcomes, but choose() can only take up to 16 arguments. Let's make this as maleable as possible.

Code:
var obj_index, chance;
chance = random(100);
if chance < 30 {
    //30% chance of choosing obj1
    obj_index = obj1;
} else if chance < 60 {
    //30% chance of choosing obj2 (60 - 30 = 30)
    obj_index = obj2;
} else if chance < 80 {
    //20% chance of choosing obj3
    obj_index = obj3;
} else {
    //Remaining 20% chance of choosing obj4
    obj_index = obj4;
}

var new_inst;
new_inst = instance_create(x, y, obj_index);
new_inst.direction = 270;
new_inst.speed = 5;
Nice! This worked perfectly. Thanks alot! Now for this part here:
Code:
var obj_index, chance;
chance = random(100);
Can I type in a greater number than 100? And also, is there an unlimited amount of objects I can add or no?
 
Last edited by a moderator:

YoSniper

Member
Nice! This worked perfectly. Thanks alot! Now for this part here:
Code:
var obj_index, chance;
chance = random(100);
Can I type in a greater number than 100? And also, is there an unlimited amount of objects I can add or no?
I use the 100 there to represent chances out of 100 percent. The variable chance gets a random value within the range [0, 100), so you pick what % chance you want for each object index, and write the code from there. It's just a reference point; nothing more.

As for the number of possible object indexes to choose from, there is no limit as far as I could tell. You would just have to calculate the % chance of each one, and make sure that they all add up to 100%.
 
Top