0
\$\begingroup\$

This is my first question on a forum so please forgive any mistakes I may have made.

I am trying to make a game similar to the classic "Asteroids". I am trying to split the asteroids in two when a laser hits them. The following code is in an event in the asteroid object that is triggered when the laser collides with the asteroid.

inst1 = instance_create(x, y, medium1)
with (inst1) {
    speed = random_range(5,8)
    direction = laser.direction + random_range(0, 90)
}

inst2 = instance_create(x, y, medium1)
with (inst2) {
    speed = random_range(5,8)
    direction = laser.direction - random_range(0 ,90)
}

My issue occurs seemingly randomly. The game loads normally and the asteroids will split several times (sometimes 3-4, sometimes I can almost go through an entire game without encountering it) as I want them to, then this error pops up.

ERROR in
action number 1
of Collision Event with object laser
for object big1:

Error in code at line 4:
direction = laser.direction + random_range(0, 90)
^
at position 26: Unknown variable direction

Any suggestions on how to fix this issue? Thank you very much in advance!

\$\endgroup\$
  • \$\begingroup\$ What is laser? A variable or an object's name? \$\endgroup\$ – Dmi7ry Apr 7 '18 at 4:56
  • \$\begingroup\$ it is an object's name, says so in the post \$\endgroup\$ – TNTFreaks Apr 7 '18 at 12:41
1
\$\begingroup\$

You are using the with statement, which makes code be executed by a given instance but you must be aware of instance reference variables: at line 4 the direction variable is inst1's, but the reference laser is not the laser object it collided with, but the reference to all laser instances. It means, if there's at least one laser instance, its direction will be referenced correctly, otherwise the script is trying to read an unexisting variable (since there are no lasers).

You can easily fix your code by using the special keyword other:

// Create asteroid fragments
inst1 = instance_create(x, y, medium1);
inst2 = instance_create(x, y, medium1);

// Update fragment variables
inst1.speed = random_range(5,8);
inst1.direction = other.direction + random_range(0, 90);

inst2.speed = random_range(5,8);
inst2.direction = other.direction - random_range(0, 90);

Since we are executing code inside the Collision Event, the special keyword other lets us reference the other instance that our current instance has collided with. So, other.direction is not any laser's direction, but the very laser that hit our asteroid.

The reason I'm not using the with construction is because the keyword other can be used there too, but with a different meaning:

with (inst1) {
    speed = random_range(5,8);
    direction = other.direction + random_range(0, 90);
}

Here, other references to the asteroid instance, because it is the one invoking the with construction. If we want to reference the laser we are colliding with, we must write other.other, so its direction will be other.other.direction, but that's quite confusing (and maybe wrong too).

\$\endgroup\$
  • \$\begingroup\$ I thought that you don't need to reference other.other, because other meaning has swapped. so you could call the laser object directly. \$\endgroup\$ – Steven May 9 '18 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.