Help with weapon sprite change

E

Endless

Guest
I've been using GM for several months, but i'm stuck at this one for almost 3 days. Help will be very much appreciated. And sorry for my bad english.

The problem is when i pick up obj_m16 and obj_pistol which are completely similar with different sprites and some options. Both of the weapons sprites are visible at the same time. I'm trying to make it to be visible only the one that is needed to be visible when i change weapons on the right click. I tried the sprite_delete, but that just deletes the sprites. And i've been trying something with arrays, but without succes. Here are the pictures. The CanBePickedUp script is at the end of the both obj_m16 and obj_pistol.

http://prntscr.com/bm4lan

http://prntscr.com/bm4m44
 

NightFrost

Member
If hiding the sprite is all you need, you can do visible = false, and then visible = true to bring it up again. This only hides the sprite, so any code the instance has will still be run as normal.
 

chorrus

Member
I would suggest something more simple for this.

Create a variable for the weapon, when the user has the pistol the var could be weapon = spr_pistol and when the user has the m16 use weapon = spr_m16, do this with all the weapons you use.

Instead of creating objects for the weapon, just draw them, in draw event use draw_sprite(x, y, weapon). Adjust x and y values and don't forget to use draw_self() so the player is drawn too.
 
E

Endless

Guest
Thank you guys for the answer!!! Chorrus, how exactly can i do that? I don't have much experience. This is very much mixed code from various tutorials.
 
S

Snail Man

Guest
For chorrus's solution, have the variable 'weapon' set to the weapon Sprite you want, then in the draw event:
Code:
draw_self();
draw_sprite(weapon, 0, x, y);
...but I'd recommend NightFrost's suggestion in your case, because that's basically the way you have it set up already, and some pretty major structural changes would have to to be implemented for chorrus's to work out
 

chorrus

Member
I have just taken a quick look at your code, in your case, you could use something like this. My answer was just to give you an idea and not very clear, I'll explain well ;)

In create event, you should create something that manages the sprite, for example I think you use SelectedWeaponIndex. You can have there SelectedWeaponIndex = spr_pistol (The weapon you want to start with)

In step or wherever you want depending how you manage that, something to manage the SelectedWeaponIndex value.

In draw event as you have an sprite that rotates you can't use draw_sprite, you would have to use draw_sprite_ext() https://docs.yoyogames.com/source/d... sprites and backgrounds/draw_sprite_ext.html

In your case the code in draw event would be:

draw_sprite_ext(SelectedWeaponIndex, 0, x, y, image_xscale, image_yscale, image_angle, image_blend, image_alpha);//This draws the weapon SelectedWeaponIndex holds.

draw_self()//This would draw the player, the weapond would be drawn first and the player second, so it would look like in your image.
 
E

Endless

Guest
Thank you Chorrus very much, i tried to stick to your solution because that seemed better and more efficient solution, but i didn't make it, i am not so good for now with GM, and i am trying easier solutions. Then i started with NightFrost solution. Switching sprites while switching weapons is working, but i have another problem. When i pick up one weapon, other weapon sprite that has to still be picked, changes to sprite without weapons. It's working all good, it can be picked up, the only problem is the sprite that changes. Here are the screenshots...

http://prntscr.com/bm86gt
http://prntscr.com/bm87oq
http://prntscr.com/bm87tb
http://prntscr.com/bm87yk
http://prntscr.com/bm886d
 
S

Snail Man

Guest
It probably has to do with the sprite_delete you have in there: since the Sprite no longer exists, it just draws whatever Sprite it finds first. Try removing the sprite_delete

Edit: no wait, if found the real problem (although you should still get rid of the sprite_delete): obj_m16sprite_index = !visible doesn't make sense as code, because visible is a property of the object, not a Sprite constant. just replace
Code:
sprite_index = spr_player_pistol
obj_m16.sprite_index =!visible
with
Code:
visible = true;
with(obj_m16){
if instance_exists(Owner){
visible = false;
}
}
What you're doing now is changing all the sprites of all the existent m16s in the room to !visible, which is equal to 0, and defaults to the player sprite
 
Last edited by a moderator:
E

Endless

Guest
THANK YOU Snail Man! Don't know how to thank you! I'm so happy about this right now because i'm trying to solve this problem for 3 days!
I just added sprite_index = spr_spr_player_pistol and it works great! Thanks again!
 
Top