Counting items

B

Black Mike

Guest
I need a piece of code to create instances of an object. This is the line of code I'm using

instance_create(irandom_range(1376,1664),672,obj_diamond);

This creates a diamond along a limited area. I want it to happen 10 times, but when I try various types of loop, instead of 1 item being place 10 times, I get 10 items at a time without end. Can anyone help please.
 
J

jackhigh24

Guest
instance_number is what you want, have a look in the manual for further info
 

TheouAegis

Member
I would expect you'd want the diamonds to not overlap, which means you'd want to snap them to whatever grid your game should be using.

If you have 16x16 diamonds, then you have room for 18 diamonds in that range.
If you have 24x24 diamonds, then you have room for 12 diamonds in that range.

This ain't the most efficient, but it'd work well enough and be easily customizable:

Code:
var min = 1376,
      max = 1664,
      spawn_limit = 10,
      i = spawn_limit;
      list = ds_list_create(),
      width = sprite_get_width(spr_diamond);
repeat (max - min) div width
    ds_list_add(list, i-- > 0);
ds_list_shuffle(list);
for (i=0; i<spawn_limit; i++)
{
    if ds_list_find_value(list, i)
        instance_create(min + i * width, 672, obj_diamond);
}
ds_list_destroy(list);
 
B

Black Mike

Guest
Thank you both for your help. I've copied and pasted the code into my game. It wouldn't accept the variables max and min, so I changed them to hi and lo. It wouldn't compile the line width = sprite_get_width(spr_diamond); so I changed it to width = 32, which is the width of the diamond sprite. The code then worked OK, except that it creates 10 diamonds each time instead of 1. I can't see how to change that I'm afraid. Can you help please?
 

Bingdom

Googledom
Change the spawn_limit variable to 1.
sprite_get_width should work just fine, did you make sure spr_diamond is the same sprite name you're using for obj_diamond?
 
B

Black Mike

Guest
When I change the spawn_limit variable to 1, no diamonds are spawned at all
 
D

DevNorway

Guest
Code:
repeat (10) {
    instance_create(irandom_range(1376,1664),672,obj_diamond);
}
 
B

Black Mike

Guest
Thanks for the suggestion DevNorway. I tried it and it works except that it spawns 10 diamonds at a time instead of one
 
W

Wild_West

Guest
Thanks for the suggestion DevNorway. I tried it and it works except that it spawns 10 diamonds at a time instead of one
Have you tried applying a break in your loop? So it makes one then stops.
I guess to then make the loop go on you'd have to pair it with some kind of timer.

I'm not even really sure how to make that work off the top of my head, but just using a normal timer setup to make one, then reset, then make another could work too.
 
C

CrazyOmar

Guest
Did you put the code into the Create Event? If it's in the Step Event for example, the loop would activate each step, resulting in 10 instances being created each time. (Except you have some kind of switch or are just checking for a pressed button etc.)
 
Last edited by a moderator:

TheouAegis

Member
Okay, since your first post wasn't too clear on the matter, i wrote one code. I left it pretty open-ended, though.

Code:
//// CREATE EVENT ////
spawn_limit = 10;
rand_list = ds_list_create();
lo = 1376;
width = sprite_get_width(spr_diamond);
var 
      hi = 1664,
      i = spawn_limit;      
repeat (hi - lo) div width
    ds_list_add(rand_list, i-- > 0);
ds_list_shuffle(rand_list);
spawn_count = 0;

//// STEP EVENT or whenever you want to spawn a diamond ////
if spawn_count < spawn_limit
    if ds_list_find_value(rand_list, spawn_count)
        instance_create(lo+ i * width, 672, obj_diamond);
 
Top