Need some Coding Help



livin' in the past
Jul 18, 2008
Heys guys and gals.

I'm making a game in GameMaker Studio that uses its own GML for its scripting language (which is mostly a derivative of Java.)

Without going into details as to why this is the case, I wrote a script to generate grass patches in random spots every few seconds on the map. The map is somewhat grid-like where each 'space' is a 48 x 48 pixel section in which the grass patch object spawns.

The code I have for it is the following:

xspawn = 48 * irandom_range(4, 9) ;
yspawn = 48 * irandom_range(3, 7);

instance_create(xspawn, yspawn, obj_grass_patch);

The limits for the range just represents the edges of the spawn area.

Ideally, I want to make it so that if a grass patch appears on a space, I don't want another one to appear on top of it when the script runs again, so I want to modify it to recognize the xspawn and yspawn value from the previous execution of the script and then exclude it in future runs until the the object is destroyed.

I'm a noob at programming, so this might be a really easy problem, but can anyone show me how this might be possible?Again, GML, from what I understand, was written primarily in Java, so a Java solution would probably apply here as well. And even if it doesn't, I can figure out the syntax once I get the logic behind it. If anyone can help with this, I'd really appreciate it.


Jul 26, 2013
  • Kingdom Hearts χ[chi]
Mmmm... So how many instances of obj_grass_patch are we talking about here? Is the script going to be stopped after a set number of instances exist in the room (given the size of your area, mmmmaybe five or six?) or will it run until there are no empty positions in the spawn area? Off the top of my head, I can't think of how I'd deal with the second situation (run until all filled) yet. Otherwise--

I'd have the script set the xspawn and yspawn in a do statement and test whether there is already an instance at position xspawn,yspawn. When there's not, create a new instance at xspawn,yspawn. So to modify your code:
do {
     xspawn = 48 * irandom_range(4, 9)
     yspawn = 48 * irandom_range(3, 7)
} until (instance_place(xspawn, yspawn, obj_grass_patch) == noone)
instance_create(xspawn, yspawn, obj_grass_patch)
I'd probably put that in a controller object's alarm event, as opposed to one of GM's scripts, since you mention doing this "every few seconds"; alarms are perfect for that. When you need to run the code, you'll just need to set the controller object's alarm[whatever] to anything non-negative and let the alarm do its thing.

...I don't know how appropriate this garble-dee-gook is without a better understanding of what you're going for though? I know how the code I'm fleshing out in my mind should work, but I don't know if it's still in-line with the way you intend this part of your game/code to function?

*disclaimer: instance_place and instance_position always mess me up; I forget what the best way of checking for an empty space is with GM. It might even be place_meeting instead of those other two; I just rely on trial and error with some of GM's built-in functions. :/


livin' in the past
Jul 18, 2008
To give you a better sense of context, the game is about being a landscaper during a time when the planet is invaded by a botanical alien species that spreads its seeds in the ground. The idea is to cut the grass patches as they pop up before they spawn into alien creatures and chase the player around. Think of it like a whack-a-mole kind of game, except that you can have a mole pop up from the same hole as another one when the other one is still there.

If a grass patch is still there (because it stays on screen and grows for roughly 10-15 seconds), another one can spawn right on top of it because those spawn locations will sometimes repeat when it goes through the RNG every time it runs indiscriminately. I don't have an end condition for the game at this early point in development, so the script runs indefinitely.

The issue I'm having is not so much about getting the script to stop as it is making sure that for each time it runs, it determines if the particular space it picks is taken, and skips it for another place that's available. (Hope that helps shed some light on the situation.)

Thank you for your help btw.