New user - problem with Space Rocks DnD tutorial Part 3

D

Dave-E

Guest
I'm just getting started with Gamemaker 2, and I'm not sure what I'm doing wrong.

I'm stuck at the end of Part 3 of the tutorial (
)

I'm not getting the correct asteroids to create when a bullet hits them. When a bullet hits a large asteroid I get a 2 new ones, but of any size: large, small, or medium. The same thing happens when a bullet hits a medium asteroid, but the small ones work fine. Also, every large or medium steroid in the room changes at the same time I hit a different instance of it.
 
Yeah, that should be alright with D'n'D (don't do it if you are sharing code in the future though, use the [ code][/code] tags and copy+paste any actual code you share).
 
D

Dave-E

Guest
Here's the section that I THINK is wrong:

// GameMaker Language Preview (Read-Only)

// Destroy Instance
instance_destroy();

// Apply To
with(other) {
// Destroy Instance
instance_destroy();

// If Variable
if(sprite_index == spr_asteroid_huge)
{
// Repeat
repeat(2)
{
// Create Instance
var newAsteroid = instance_create_layer(x + 0, y + 0, "Instances", obj_asteroid);

// Assign Variable
newAsteroid.sprite_index = spr_asteroid_medium;
}
}

// If Variable
if(sprite_index == spr_asteroid_medium)
{
// Repeat
repeat(2)
{
// Create Instance
var newAsteroid = instance_create_layer(x + 0, y + 0, "Instances", obj_asteroid);

// Assign Variable
newAsteroid.sprite_index = spr_asteroid_small;
}
}

// Repeat
repeat(10)
{
// Create Instance
instance_create_layer(x + 0, y + 0, "Instances", obj_debris);
}
}
 
Nothing looks wrong there (also, you ignored what I said about using the [ code][/code] tags). Can you show the code in the Create Event of the Asteroids?
 
D

Dave-E

Guest
I didn't ignore you, I'm sorry... I didn't know how to reply since I don't know what that means. Where would I go about using the [ code][/code] tags?
 
D

Dave-E

Guest
I really appreciate your taking the time to help me out. I just learned how to get a Live Preview of the GML code. Here's the Create Event for the Asteroid:

// GameMaker Language Preview (Read-Only)

// Choose
with(obj_asteroid) {
sprite_index = choose(spr_asteroid_small, spr_asteroid_medium, spr_asteroid_huge);
}

// Get Random Number
direction = floor(random_range(0, 359 + 1));

// Get Random Number
image_angle = floor(random_range(0, 359 + 1));

// Set Speed
speed = 1;
 

FrostyCat

Redemption Seeker
I didn't ignore you, I'm sorry... I didn't know how to reply since I don't know what that means. Where would I go about using the [ code][/code] tags?
You use it as part of your post to denote code. Content between [code] and [/code] tags are formatted in monospace as per convention, appear in a box of its own and have all other formatting disabled.
Code:
show_message("foo");
By the way, your problem is this part of your Create event:
Code:
with(obj_asteroid) {
  sprite_index = choose(spr_asteroid_small, spr_asteroid_medium, spr_asteroid_huge);
}
This changes the sprite of EVERY instance of obj_asteroid, not just the current instance, and it is clearly not shown in the video. Get rid of the with and just set sprite_index the same way you did for the other properties. You need to start recognizing the difference between objects and instances.
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.

Corollary: NEVER set or use an instance's own variables with object.variable. An instance's own variables can be referenced as-is without dot prefixes. DO NOT use self. If multiple instances of the object exists, you might end up setting the value for all instances or for some other instance (depending on the export).
 
D

Dave-E

Guest
Whew... i'm still not quite sure how to proceed. I want to change the create event in obj_asteroid, and I don't want to use 'with'.

I can't figure out how to do that in DnD. I can see the 'with' code in the GML preview, but I don't see 'with' attached to a DnD element. Could someone spell out exactly how the asteroid creation should look?
 

FrostyCat

Redemption Seeker
If there is a with statement in the GML preview, that means you've touched the "Applies To:" dropdown (the caret next to the X in the D&D action). It's probably NOT what you were told in the video. Set that back to self and the with statement should go away.
 
Top