[SOLVED] Spawn objects

E

Edeyz

Guest
I am making a game set in space and don't wont to have to place every star manually.
So I have created a "Star density map" to show where the [object] obj_star 's need to go (darker parts more stars).

Here is an example of what I mean (Not my actual one, but that's hardly better as i'm not yet finished it.):
Untitled.png

I know how to create instances at coordinates and assign all the variables of them and the size, colour ect. but I don't know how to get them to spawn in accordance with the map.
I was hoping I could do something like this:

>game start
>Load room "rm_map"

>create instance of "obj_starSpawn"
>create instance of "obj_map"

>in obj_starSpawn"
>>numberOfStarsToSpawn = 100
>>for numberOfStarsToSpawn
>>>choose random position on map giving higher priority to darker areas
>>>create instance obj_star
>>>with obj_star
>>>>assign variables

>Save Game
>Load room "rm_mainMenu"

I would also if possible like to ensure that GameMaker spawns this the exact same way (stars in same spots) every time the game is run. (even when not running through GameMaker itself)


Thank you for your help
-Edeyz



Also, I didn't quite know what to call this thread and what tags to add, so If there is a proper name for this or more tags I should add can you please tell me?, thanks.
 
Last edited by a moderator:

makas

Member
Everytime I think something is not possible to do in gamemaker, someone here just come with a brilliant solution or hidde function, I dont know of a existence of a function that check for darker colors, but I will give you an alternative for this, I suggest you to use your galaxy image as a background, and the create simple square sprites, of different color, each of this represent areas of your room that will have different star density, then in your room editor, add those objects in the areas you consider correct, then just run your star generation code, with random positions, then at the create event of each star check if is in collision with a density area object, if is a low density area, add a code that gives you 80% of chances to destroy the object, if is a mid density area make it 50%, and if is a high density area give a 10% of chances to destroy it, this will create a natural and still random map with diferent stars based on your high density areas... I hope is clear enough for you.... if not I can put some code in here
 
Last edited:

obscene

Member
You can use draw_get_pixel to get the colors from your image.
https://docs.yoyogames.com/source/dadiospice/002_reference/drawing/colour and blending/draw_getpixel.html

You'd need to run a loop checking every pixel starting at 0,0 and continuing until sprite_width,0 and then move to 0,1 and repeat, each time putting the value into a grid or array or something. The function is slow, so I couldn't even guess how long it might take to do this but you'd end up with a little bit of loading time before you game ran for sure. I think I'd use show_debug_message along with it so you know your game isn't froze as it might seem like it is for a bit. You might want to consider making a program specifically for this that saves the data to an INI file and then you just include that in your game instead. Then of course you have to use that data to feed some kind of equation where the lower values (darker) equals a higher chance of spawning an object.

When you are using randomness to generate you can use random_set_seed() with some specific value directly before running the generation script and that will ensure the results are always the same.
 
Last edited:
E

Edeyz

Guest
Thank you everyone. I'm in the process of making the generator using @obscene 's suggestion. I will make sure I update you when I'm finished.
Edeyz
 
E

Edeyz

Guest
Okey so I think I got it working, although I have yet to test it with a big "map" (only a 5 * 2). It still outputs results that are random and from what I can see, they are as they should be.
Here is a copy of the ini file.

[jS-3542u]
3planet5="2.000000"
2planet5="0.000000"
1planet5="1.000000"
0planet5="2.000000"
3planet4="1.000000"
2planet4="1.000000"
1planet4="1.000000"
0planet4="15.000000"
3planet3="0.000000"
2planet3="4.000000"
1planet3="5.000000"
0planet3="8.000000"
3planet2="0.000000"
2planet2="2.000000"
1planet2="1.000000"
0planet2="3.000000"
3planet1="0.000000"
2planet1="4.000000"
1planet1="2.000000"
0planet1="5.000000"
7="5.000000"
6="1.#QNAN0"
5="1.000000"
4="0.665192"
3="9818111.000000"
2="1.000000"
1="1.000000"
0="jS-3542u"
[ORVCOSS-67915T]
3planet2="1.000000"
2planet2="4.000000"
1planet2="3.000000"
0planet2="13.000000"
3planet1="0.000000"
2planet1="1.000000"
1planet1="1.000000"
0planet1="9.000000"
7="2.000000"
6="1.#QNAN0"
5="1.000000"
4="0.519124"
3="7323135.000000"
2="1.000000"
1="4.000000"
0="ORVCOSS-67915T"
[7432762-5488N]
0="7432762-5488N"
1="2.000000"
2="1.000000"
3="16775419.000000"
4="1.150365"
5="1.000000"
6="1.#QNAN0"
7="1.000000"
0planet1="3.000000"
1planet1="0.000000"
2planet1="0.000000"
3planet1="1.000000"

1 and 2 are the x and y cords respectively.
I'm going to try with a big map now and see if it works. But will probably take a few hours, so don't wait up.
 
E

Edeyz

Guest
Ok, this isn't the best example as its mainly greyish and doesn't have too many things on it. But generally it seams to have at least some correlation between value and the occurrence. It took about 3 hours to run altogether. I may tweek it a bit more and try different maps. (the actual image size is 1920 * 1080 but I had to downsize it so I could upload it) test.png
 

obscene

Member
Oh wow... 3 hours!

You should probably try a small image that is just black and white (boolean) and see if you can get spawns where you want. Then add some grey. Those dots are so spread out it's hard for me to tell really if the map had an effect but the problem could be your generator rather than our "scanner."

Also I hope you saved the data from the scan separately so you don't have to repeat that to adjust your random generator?
 
E

Edeyz

Guest
The problem is almost definitely with the generator. I am trying smaller images (288 * 162) and its running in a minute or two. I have been trying to use a power function for its adherence to value. But it seams to have a very high association with white. its not as evident in those pictures. But when I generate a large amount of objects it becomes very clear (like before when I generated in the order of 7000 by mistake).
test.pngtest.png
The code for the program is basically (with the rules I used for those images):
>Start game
>totalValue += value from each pixel
>spawnChance = amountToGenerate / valueTotal <<<<<<<<<<<<<<< spawn chance is therefor the amount it needs to spawn per each increment of value
>For each pixel
>>if random(4) <= (spawnChance * power(current pixel value, 2)) <<<<<<<<<<The power of 2 gives higher priority to higher values, and the random 4 is so it does not always generate on high values.

I am going to try the 4 as a 5 and see how that works out. Because of the way this works it has been generating about 700 for amountToGenerate == 200. This has already been reduced by getting the sqr root of amountToGenerate (to count the power of 2).
 
E

Edeyz

Guest
Also this is taking a while because my computer decided during the first time I ran the generation (for the 3 hour one) about 2:40 in it had to have a windows update and could not delay it.
 

obscene

Member
Wow that actually looks impressive, it looks like it's doing exactly what it should and you just gotta tweak your formula\ to get the results you want.

To speed it up you might want to just read dots every so far apart and somehow interpolate the data in between instead of reading every pixel, but if you are making this a stand along thing anyway I guess speed doesn't matter a whole lot. You'd probably spend more time trying to speed it up than just waiting on the results.
 
Last edited:
E

Edeyz

Guest
Here is the image from the 5. The last one uses a power of 3 instead of 2. (there are almost 900 in it though as opposed to the other one with about 700, because its quite fiddly)

I was thinking of using something like what you mentioned (although more to ensure that things are spaced better and dont overlap) But i'm worried this may make it seam as if its on a grid.

The center right on the new images is the point of the highest value.
test.pngtest.png test.png
 
Top