SOLVED Help with the creation of an obj

philipIII

Member
Hey! I want to place an enemy in each floor of the room, here's my script (i'm kinda new so it's a simple script):

///@arg collision
///@arg obj
function random_platform()
{
var collision = argument0;
if instance_exists(collision)
{
instance_create_layer(collision.x + random_range(-60,60),collision.y - 15,"Instances",argument1)
}
}

The collision is the actual oFloor and the obj is the oEnemy.
The problem is that just 1 enemy spawn in the same floor (in a random x point of THAT floor, that's fine) and i want that every floor has an enemy :(
 

Binsk

Member
Please read this:
https://forum.yoyogames.com/index.php?threads/how-to-properly-put-your-code-in-a-thread.59648/

I few things to clear up here. There is a vast difference between 'objects' and 'instances'. You can think of an object as a blueprint while an instance is an active entity built from that blueprint. A good tutorial on the differences:
https://forum.yoyogames.com/index.php?threads/whats-the-difference-objects-and-instances.29005/

When you specify an OBJECT and try to get its x/y value, this actually makes no sense at all. However, GameMaker is nice (albeit somewhat problematic) in that it will try to find an instance created from your specified object and modify that if you specify the name of an object and not an instance id.

So let's say you have created 10 instances of oFloor and you call your script:
Code:
random_platform(oFloor, oEnemy);
You are thinking it will call this for each oFloor instance but it won't because oFloor isn't an instance, it is an object so GameMaker will just find the first instance created as an oFloor and use that. This is what is happening when your script then calls collision.x, etc, it is just grabbing the first instance it can find that inherits from oFloor.

So, all this waffling aside, how to you go over EVERY oFloor instance in the room? GameMaker has a statement called 'with' which takes an object and will execute a piece of code for every instance that inherits from it. Your script may then look something like this:
Code:
function random_platform(collision, instance){
   with (collision){ // Execute the following code on behalf of every instance of oFloor:
      instance_create_layer(x + random_range(-60, 60), y - 15, "Instances", instance);
   }
}
Note that I don't have to check if oFloor exists because if there are 0 in the room then 'with (oFloor)' will simply execute 0 times. Also, when the code from a 'with' statement is executed, it is executed as if the instance itself is executing it, thus why I can just use 'x' and 'y' inside the code.
 

philipIII

Member
Please read this:
https://forum.yoyogames.com/index.php?threads/how-to-properly-put-your-code-in-a-thread.59648/

I few things to clear up here. There is a vast difference between 'objects' and 'instances'. You can think of an object as a blueprint while an instance is an active entity built from that blueprint. A good tutorial on the differences:
https://forum.yoyogames.com/index.php?threads/whats-the-difference-objects-and-instances.29005/

When you specify an OBJECT and try to get its x/y value, this actually makes no sense at all. However, GameMaker is nice (albeit somewhat problematic) in that it will try to find an instance created from your specified object and modify that if you specify the name of an object and not an instance id.

So let's say you have created 10 instances of oFloor and you call your script:
Code:
random_platform(oFloor, oEnemy);
You are thinking it will call this for each oFloor instance but it won't because oFloor isn't an instance, it is an object so GameMaker will just find the first instance created as an oFloor and use that. This is what is happening when your script then calls collision.x, etc, it is just grabbing the first instance it can find that inherits from oFloor.

So, all this waffling aside, how to you go over EVERY oFloor instance in the room? GameMaker has a statement called 'with' which takes an object and will execute a piece of code for every instance that inherits from it. Your script may then look something like this:
Code:
function random_platform(collision, instance){
   with (collision){ // Execute the following code on behalf of every instance of oFloor:
      instance_create_layer(x + random_range(-60, 60), y - 15, "Instances", instance);
   }
}
Note that I don't have to check if oFloor exists because if there are 0 in the room then 'with (oFloor)' will simply execute 0 times. Also, when the code from a 'with' statement is executed, it is executed as if the instance itself is executing it, thus why I can just use 'x' and 'y' inside the code.
Thank you very much, it worked! and thanks for the explanation too!
 
Top