• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Can't change weapon when interacting with weapon box

Artie_Kyle

Member
Hey GMC,

The issue is as the title suggests, which is bizarre considering how easy this one should be.

Basically, I'm holding a gun, and I have a box that has 20 images that cycle through, how I want it to work is that when I collide with the box, depending on then image, a specific weapon gets assigned, yet for some reason, the boxes get destroyed and nothing happens.

This slice of code explains how I'm approaching this one. For the record, this is part of a script where image_index = _image_index and obj_gun = _weapon_change.

Code:
if image_index == 11 {
    with obj_weapon_parent_boy {
        instance_change(obj_gun, true);
    }
}
What am I missing here?

If I'm missing information that could help you elaborate what I'm messing up, please inform me and I'll add it straight away.

As always, thanks for the help, folks!
 

Let's Clone

Member
Not sure if this is the issue, but 'image_index' has the potential to be a fraction.
Try this perhaps?

Code:
if floor(image_index) == 11 {
    ...
}
I've never played with 'instance_change' so I'm not sure if that is the issue. But reading the documents makes it seem like it should work fine for you, as long as 'obj_weapon_parent_boy' is what you intend to change.
 

Artie_Kyle

Member
WOW! That fixed it completely!

It's so simple, yet I never even thought of it for a moment... you're a genius, Abrexas! Thank you so much!

Out of curiosity, if you never tinkered with instance_change, how would you approach this issue?

EDIT: Perhaps you can help me with another issue. It's to do with IDs.

Basically, I have two weapons, one equipped and the other is mounted on the character's back, and depending on which weapon I pick up, if its equivalent is active or not, it is either equipped as primary or secondary, but when I use with (obj_weapon_parent_boy) to trigger it, it changes both weapons, so how do I assign each weapon's ID to a variable again?

var _weapon_specific = obj_weapon_parent_boy.id maybe?
 

Let's Clone

Member
WOW! That fixed it completely!

It's so simple, yet I never even thought of it for a moment... you're a genius, Abrexas! Thank you so much!

Out of curiosity, if you never tinkered with instance_change, how would you approach this issue?

EDIT: Perhaps you can help me with another issue. It's to do with IDs.

Basically, I have two weapons, one equipped and the other is mounted on the character's back, and depending on which weapon I pick up, if its equivalent is active or not, it is either equipped as primary or secondary, but when I use with (obj_weapon_parent_boy) to trigger it, it changes both weapons, so how do I assign each weapon's ID to a variable again?

var _weapon_specific = obj_weapon_parent_boy.id maybe?
Anytime, I've had quite a few battles with animations/sprite-manipulation haha.
I always deleted/created objects manually, but after seeing this I will definitely try to implement some instance_change into my next project.

The issue is probably because you are calling a change on the parent, which gives access to all of its children.
I would suggest something like this:

If your player is to have a fixed number of weapons, then I would simply save them as variables that the player has access to. Such that you can be very particular when calling which one to change.

If your player is to have many/unknown number of weapons on hand then I would store those weapons in an array. You can either create a pointer to look at which weapon is being held, or you can shift the entire array every time you perform a weapon swap (pointer is ideal imo).

Either of these ensure that you are only looking at the weapon to be swapped, so you don't have to worry about messing with the wrong children. You could even use a 2D array and store the items value of equivalence, if the weapons do not contain that information on their own. Best part of coding is that you can solve anything in a number of ways haha.
 

Artie_Kyle

Member
I'm too far in development to rewrite the whole thing from scratch with arrays (not to mention my inexperience using them, plan on tackling it tho). Instead, I created a middle parent that is basically separating secondary and primary weapons, so that solves it for the time being.

Like you said; quite a number of ways to solve a problem!
 
Top