GML Help With "instance_create" (Solved)

W

wozzlewuzzle

Guest
Hello,
I'm fairly new to game maker and gml code with this being my first post, so this may be a simple question to people more experienced.
Code:
instance_create(x+61,y+34,obj_bullet)
The code here is used in my platformer game and I want to create an object called obj_bullet at a specific place on my player's sprite. I know in the drag-and-drop icon for "Create Instance" has a box to check off if you want to create the object relative to another object but i want to know how to do it using code. The way I have it above doesn't seem to be working so any help would be appreciated, thanks.
 
M

mahre

Guest
Hello,
I'm fairly new to game maker and gml code with this being my first post, so this may be a simple question to people more experienced.
Code:
instance_create(x+61,y+34,obj_bullet)
The code here is used in my platformer game and I want to create an object called obj_bullet at a specific place on my player's sprite. I know in the drag-and-drop icon for "Create Instance" has a box to check off if you want to create the object relative to another object but i want to know how to do it using code. The way I have it above doesn't seem to be working so any help would be appreciated, thanks.
If that instance_create isn't inside the player, you should use instance_create(x.obj_player+61, y.obj_player+31, obj_bullet) instead
 
A

anthropus

Guest
The instance create code should be in the object that is creating the bullet; and it should be conditional--should happen only when u want it to happen, like when u push the shoot button THEN create the bullet. You'll probably also want a variable and alarm to control the rate of fire also in the object that is firing the bullet.
and in the bullet object you should have the code for attributes if the bullet like it's speed, damage, collision, etc
 
A

arirish

Guest
Just to round out the answers here: the fact that you're using 'x+61' - the 'x+' IS the 'relative' part. It's 61 pixels relative to the x of the calling instance. Same with the y+.
 
F

FadenK

Guest
To read values from an outside object to make it relative to, you must use an Identifier, followed by a dot (.), followed by the variable name. It will look something like this:
Code:
instance_create(obj_player.x+61,obj_player.y+34,obj_bullet)
However, I would advise you to not use hard numbers in your function code. Create local variables instead. Like this:
Code:
var bullet_offset_x = 61;
var bullet_offset_y = 34;
instance_create(obj_player.x+bullet_offset_x,obj_player.y+bullet_offset_y,obj_bullet)
A rule of thumb I use, is that if I'm going to use the same value twice in a code, I make a local variable. Doing it like this will help if you need those values in your code again or if you need to change something it makes it easier to read.


For more information of types of variables, read your manual. While in the GameMaker IDE press "F1" click on the "Search" tab on the left side, and copy and paste the following topic name. It will come up as the last one, but will match exactly.
Variables And Variable Scope

For more information about the identifiers, read:
Addressing Variables in Other Instances

Edit:
You can also set the values after creating the instance. I wont show you how to do this with your code because it may confuse you, but it also may become helpful to you in the future. This works because the function "instance_create" actually returns the id of the instance created. So you can do things like this:
Code:
var new_instance = instance_create( x , y , obj_ball );
new_instance.speed = 8;
new_instance.direction = irandom( 359 );
It's important to understand, that when you instance_create, the "Create" event trigger immediately, then the rest of your code. So if you set speed to 4 in the obj_ball Create event, then had that code, the ball speed would effectively start as 8 ( it would be set 4 from the Create event being triggered with instance_create, then set to 8 almost immediately afterwards by new_instance.speed = 8;
 
Last edited by a moderator:
Top