GameMaker I'm not understanding something about instances...

C

Cybert99

Guest
Terrible programmer here.

I'm sure I could figure this out on my own, but I have to leave for work in a few minutes. Grrrrrr.

I have a ship and I'm trying to add a few guns to it. I have one object (obj_gun) and I'm creating multiple instances of that object called (gun_1, gun_2, etc.):

CREATE EVENT OF OBJ_SHIP:
var gun_1;
gun_1 = (instance_create_depth(x,y,0,obj_gun))
with gun_1 {
image_angle = other.direction;
initialAngle = 0;
offset_x = -15;
offset_y = 0;
anchor_dir = point_direction(0,0,offset_x,offset_y);
anchor_dist = point_distance(0,0,offset_x,offset_y);
}

STEP EVENT OF OBJ_GUN:

if (instance_exists(gun_1)) {
x = gun_1.x + lengthdir_x(anchor_dist, anchor_dir+other.image_angle);
y = gun_1.y + lengthdir_y(anchor_dist, anchor_dir+other.image_angle);
image_angle = gun_1.image_angle + initialAngle;
}

This is giving me a variable does not exist error. What am I doing wrong?
 
B

Blazing

Guest
The variable gun_1 is not a global variable so it does not transfer over to the obj_gun.

You can either change the variable to global.gun_1 or declare var gun_1; gun_1=other.gun_1 in the with statement.
 

TheouAegis

Member
Why are you even coding like that? If gun_1 is an instance of obj_gun, then it doesn't matter if gun_1 exists in the room or not.

Just make an array inside the ship holding all the positions of each gun object relative to the ship. Create your guns using a FOR loop and pass the offsets for each gun using that loop. Then in the End Step event of each obj_gun update its speed based on the saved offsets.

Your code is just weird and shows a misunderstanding of instances in general.
 
Top