1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

[solved]Snowbat

Discussion in 'Programming' started by Twilight Raven, Jan 26, 2020.

  1. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    I have a character that the end of an attack animation is supposed to throw a snowball.

    At the end of the animation I'm trying to see if the ending animation is the attack animation.
    So have the snippet of code:
    Code:
    
    if (sprite_index == SnowBatAttack)
    {
    Is this the proper way to compare the current sprite index and a sprite?
     
  2. Cat

    Cat Member

    Joined:
    Jun 22, 2016
    Posts:
    87
    Assuming SnowBatAttack is the name of your sprite, yes.
     
  3. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    Well my step event says this:
    Code:
    if (mouse_check_button_pressed(mb_right))
    {
        show_debug_message("Right button, sprite_index = SnowballAttack")
    sprite_index =  SnowballAttack;
    audio_play_sound(SnowballAttack, 20, false);
    target_x = mouse_x;
    target_y = mouse_y;
    
        show_debug_message("SnowbatObject Target x is " + string(target_x));
        show_debug_message("SnowbatObject Target y is " + string(target_y));
    
    //inst = instance_place(target_x, target_y,BrainParentObject);
    }
        
        
        
    
    And my animation end code is:
    if (sprite_index == SnowBatAttack)
    {

    show_debug_message("Animation End. Creating snowball");
    snow_inst = instance_create_depth(x, y, -12000, SnowballObject);

    snow_inst.source_x = x-20;
    snow_inst.source_y = y-20;

    snow_inst.target_x = target_x;
    snow_inst.target_y = target_y;

    }

    Its getting hung up on sprite_index. For some reason it its not picking up that sprite_idnex == SnowBatAttack
     
  4. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    You are setting sprite_index to SnowballAttack in the Step Event, but checking for SnowBatAttack in the Animation End event.
     
  5. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    Thanks rIKmAN. Its still not spawning a snowball. All it does is play the snowball sound.

    (edit)
    I had this stupid snowbat fixed last week and my archive crashed and now I can't remember how I fixed it. just fyi :mad:
     
  6. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    The code you have posted doesn't spawn a snowball, when the right mouse button is presses it changes the sprite_index, plays a sound and sets targetx and targety to the mouse coordinates.

    If you don't do the correct check in the Animation End Event then none of that code will run.
    You can check if it is being run by seeing if "Animation End. Creating snowball" is output to the log.

    Break it down and step through the code logically to find which part is not doing what you think it should be doing and which parts are being run (or not).
     
  7. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    The code I am posting should be spawning a snowball.

    Code:
    And my animation end code is:
    if (sprite_index == SnowBatAttack)
    {
    
    show_debug_message("Animation End. Creating snowball");
    snow_inst = instance_create_depth(x, y, -12000, SnowballObject);
    
    Breaking things down (giving as much info as I can):

    In the step event of Snowbat changes the sprite to SnowBatAttack.
    Then in the animation end I do a check to see if the sprite is snowbattck.
    The code snippet above in the animation end.
    So then it creates an instance of the snowball create depth named snow_inst
    (Edit)
    So sprite_index == SnowBatAttack doesn't work. I get a whole bunch of snowballs but if I set that sprite_index == SnowBatAttack I don't get any snowballs
     
    Last edited: Jan 26, 2020
  8. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    922
    Do you see the message that you have in the Animation End event being displayed? If not, have you tried setting your image_index to 0 when you change to the SnowBatAttack sprite? Try also putting a show_debug_message before if statement in your Animation End event so you can see if it even gets into that event. If it doesn't go there then it is possible that you have not set image_index to 0 so the animation would actually start, or you have an image_speed of 0 which means it won't play the animation at all.
     
  9. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    I only see the message in Animation Event end if I comment out the sprite_index == SnowBatAttack

    Ok. Trying image_index = 0 didn't do anything. I tried putting a debug message before the if statement and it showed up ok. I have an if statement afterwards and it doesn't show up.


    Code:
    if (mouse_check_button_pressed(mb_right))
    {
        show_debug_message("Right button, sprite_index = SnowballAttack")
    sprite_index =  SnowBatAttack;
    image_index = 0;
    audio_play_sound(SnowballAttack, 20, false);
    target_x = mouse_x;
    target_y = mouse_y;
    
        show_debug_message("SnowbatObject Target x is " + string(target_x));
        show_debug_message("SnowbatObject Target y is " + string(target_y));
    
    //inst = instance_place(target_x, target_y,BrainParentObject);
    }
    (Edit)
    In the initial code the audio_play_sound plays. So it Is executing the sprite_index = SnowBatAttack and image_index = 0
     
  10. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    Set a breakpoint just before checking for the mouse click and run the game in debug mode.

    Execution will pause at the breakpoint, then step through the code line by line using the buttons at the top of the debugger window and follow what is happening.

    Also just to check - this Step Event and Animation End Event are in the same object?
     
  11. HeWhoShallNotBeNamed

    HeWhoShallNotBeNamed Member

    Joined:
    Nov 9, 2018
    Posts:
    90
    Two thoughts:

    1) In the example code you posted in post #3, you're setting the sprite index to SnowballAttack, but in the code from post #9, its SnowBatAttack. I'd double check in your actual code to make sure that the sprite index you're setting in the step event is the same index you're checking for in your Animation End event. It might be a simple typo.
    2) Is there some code in your snowball object that might be destroying it as soon as its created? Maybe on collision with the object creating it?
     
  12. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    WHere is sprite_index in the debugger?
     
  13. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    Yeah they are in the same object.
     
  14. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    922
    Put this at the vary top of your Animation End event (before your if statement), and then show us the output that it produces (it will spam the Output window every time that the animation ends):
    Code:
    var currSpriteAnimName = sprite_get_name(sprite_index);
    show_debug_message("Expecting Sprite Id: " + string(asset_get_index("SnowBatAttack")));
    show_debug_message("Sprite Animation Ended: " + string(sprite_index) + ", " + currSpriteAnimName);
    Also, try cleaning the project cache (broom icon) just in case the project cache is corrupted or holding on to out of date information.
     
  15. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    Code:
    Expecting Sprite Id: 125
    Sprite Animation Ended: 123, SnowBatStill
    So, that's not what I expected. It says SnowBatStill is the ending animation.SnowBatStill is the stationary animation. I don't know what 125 is.
     
  16. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    I assume that is a 1-2 frames idle animation for when isn't moving?

    Do you have code somewhere that sets this every step and so it's overwriting your sprite_index assigment you make when you click the right mouse button?

    sprite_index in the debugger is under "built-in" when you list the instances and click the little + to open up the instances from the list.
     
  17. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    922
    If you read the code I provided you would see that this is the id of the SnowBatAttack sprite. So, as @rIKmAN says, I also suspect that you have something in your Step Event that is setting the sprite_index to SnowBatStill and that is happening before the SnowBatAttack sprite was able to animate and therefore the if statement in your Animation End is never going to be true as the sprite has already been changed.
    Do a global search for SnowBatStill and see where this might be getting set into the sprite_index. Once you find what is setting that sprite on the object then you should at least be one more step closer to finding what you need to fix.
     
    Twilight Raven likes this.
  18. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    Global search is CTRL+SHIFT+F.
     
  19. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    What do you mean? That is changed my sprite_index for the snowbat goes so quickly that it is doing the BatAttack animation but then does the snowbatstill animation
    so fast I don't see the BatAttack? That would explain things except the snowball isn't getting spawned.
     
  20. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    I mean that your Animation End code is never running because the sprite_index when it is checked is not the sprite_index you are checking for.
    This was shown by the code that @BaBiA Game Studio asked you to run and post the output of.

    This means that somewhere, somehow the sprite_index is being changed to SnowBatStill, so as you've been advised you should do a global search for SnowBatStill and look through the results to see where the sprite_index is getting set to this sprite and if it is overwriting the sprite_index every frame.

    Because the sprite is never the one you are checking for when you get to the Animation End event - it must be getting set somewhere else after the mouse click code, but before it gets to the Animation Event code.
     
  21. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    This means that somewhere, somehow the sprite_index is being changed to SnowBatStill, so as you've been advised you should do a global search for SnowBatStill and look through the results to see where the sprite_index is getting set to this sprite and if it is overwriting the sprite_index every frame.


    Because the sprite is never the one you are checking for when you get to the Animation End event - it must be getting set somewhere else after the mouse click code, but before it gets to the Animation Event code.

    rIKmAN, Today at 6:06 PM Report
    I mean that your Animation End code is never running because the sprite_index when it is checked is not the sprite_index you are checking for.
    This was shown by the code that @BaBiA Game Studio asked you to run and post the output of.

    Gotcha

    Easier said then done. The snowbat throws snowballs, right? Well every enemy that gets hit with a snowball turns into a SnowBatStill. That's a lot of snowballs to check.
     
  22. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    Every bat is just an instance of the bat object(s), right?

    So you should only have to check the bat object(s) code and fix the bug, because every instance of that object is running that same code so changing the code in the bat objects events will then apply it to every instance you create.

    Unless in your game every enemy (even if it's not bat) gets assigned SnowBatStill as it's sprite_index when hit with a snowball, in which case I would advise you to focus on a single enemy type and work on just that object to get things working. You can then apply that fix to all the enemy objects once you know what he issue is.

    Did you do the global search that we asked you to do?
    Did you find any code that was setting the sprite_index outside of the right mouse click code in the step event?
    Did you use the debugger to step through the code from the mouse click and follow it to see where else the sprite_index gets changed?

    You are getting back into the habit you had previously with your Ted account where you are tying yourself in knots trying to fix something without logically breaking things down, following the code and trying to isolate the problem as much as possible. Randomly changing things in the hope that it works will just make things worse and can cause other unrelated bugs which will waste more of your time.

    Make a new room if you have to and add just the objects required to get the interaction you require, this will eliminate anything else from the equation.

    If you still can't work out what's going on, post all the code for every event in your relevant objects here using spoilers and code tags so we can take a look at what is happening.

    I'm still sure that you are setting the sprite_index somewhere else after the click, but before the Animation End Event - because it wouldn't be a different sprite otherwise, it would still be the one assigned when you clicked the mouse.

    This is why stepping through the code with the debugger is so useful, you can literally follow the code execution line by line and see what is being run and spot any sprite_index assignments that happen before you get to the Animation End Event.
     
  23. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    1) Yes. Every enemy when hit with a snowball turns into a SnowBatStill
    2) Global Search. Yes. That was the first thing I did.
    3) No. I saw a lot of SnowBatStills, I'm not sure, may have missed one. I'll go through them again.
    4) I went through the debugger. I didn't check sprite_index because I couldn't find where that value was in debugger.
    5) I don't know how I can be any more logical than I can more now.
    6) I think the issue is the same as you. Temporarily I wrote in some code to get it to work but I'm going to find the stray SnowBatStill again.
    7) I saw you posted so I figured I'd return the favor.
     
  24. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    If you double click the results that appear in the window, it will take you straight to that piece of code in whatever object it has been found in.

    Just go through them line by line and check what sprite is being assigned, what object it is in, what event etc and work out whether it is correct or not in releation to the bug.
    I already told you where it was in post #16, however you don't need to check the value of sprite_index in the debugger because we already know what it changes to from using the code BaBiA Game Studio provided earlier. We want to find WHEN it changes.

    As I mentioned previously, add a breakpoint to the code inside the if block where you check for the right mouse button click, run in debug mode and click - the game will pause and the debugger window will open.

    Step through the code line by line looking at the flow of execution - it will advance 1 line every time you click to step through.

    At some point from here before you get to the Animation End Event you will a line of code see where sprite_index is getting changed for this instance - note down the object this is an instance of, the event it is in and line number it is on.

    If it is being set every step to the same sprite reported in the output from BaBiA Game Studio 's code then you have found the issue - or at least one occurence of it - so try commenting it out and see what happens.

    If the issue isn't fixed repeat the above process until you get from the mouse click breakpoint, to the Animation End Event for the same instance where you check for the sprite that was assigned when you clicked the mouse.

    Depending on how many instances you have this could take a lot of clicks, which is another reason why I advised making a new room with just the relevant objects in - less instances executing code means less clicks stepping through the debugger and allows you to isolate the code as much as possible.

    You can also press F11 (I think?) to step through - hover the button in the debugger and it should tell you the correct key as I'm just going from memory, but that may be preferable and quicker than a mouse click for you.
    Reply whenever you want there's no rush, I just checked the forum before bed so decided to reply but if you don't want to reply for a week that's all good - it's your code! :)
     
    Last edited: Jan 27, 2020
  25. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    Thanks for all the help. @rIKmAN. Once this snowball problem is solved I *could* use some help with something. A grid overlay of cells RTS style. I won't go into detail now but I could use some help.
     
  26. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    That's what the forums are for, also plenty of tutorials and Youtube videos on grids.
     
    Twilight Raven likes this.
  27. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    @rIKmAN Works like a charm. I did another search and for good measure renamed the SnowbatStill and the TurnedIntoASnoman sprites so that that mix up doesn't happen again. You're probably asleep, I don't - I'm a vampire. So, wanna work on grids tomorrow? ;)
     
  28. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    No not asleep yet, also a night owl.

    So what was the actual issue in the end?
    I'd like to know what it was after a 27 post forum thread!

    For grids, work out what you want to do and then spend some time looking at tutorials, relevant manual pages for the functions needed to do what you want and give it a go.
    Start a fresh project first and play around with just the grid functionality until you have a handle on it, don't just try adding it directly to your existing project as I have a feeling that will lead to trouble for you.

    Always isolate to allow you to get a better understanding of things and remove any other possible issues from the equation while you learn and test whatever it it is you are trying to implement.
     
  29. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    Yeah well I can't believe I overlooked this. After all it was this bit of code:
    Code:
    //GLOBAL SNOW FROZEN ENEMY
    if (!instance_exists(global.current_collision_snow))
    {
        if (flight)
        {
            sprite_index = SnowBatWalk;
        }
        else
        {
             sprite_index = SnowBatStill
        }
    }
    
    
    
    It was the the else that was the culprit

    I'm not sure how I want to set up the grid yet anyway. I'm thinking a hexagonal cells with the characters snapping to the grid.
     
  30. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    Which is exactly why we asked you at the start to do a global search for the sprite so that you could see where it was being assigned! :)

    Honestly, hexagonal grids aren't the easiest things to get your head around compared to regular grids, so I would definitely advise you to spend an hour or two doing some research on how they work, the maths involved and reading / watching any tutorials and Youtube videos you come across in full before you start.

    Make notes if you need to so you can refer back to to things later without having to remember the video or article a piece of information was from.
    Make a new project and add functionality bit by bit until you have a system that works and more importantly - that you understand!

    This would be a decent starting point: https://www.redblobgames.com/grids/hexagons/

    There is also a Marketplace Asset with decent reviews which seems like it would do what you want which can be found here: https://marketplace.yoyogames.com/assets/349/grids although even if you decide to use it I would advise you take the time to read any documentation and try and understand the code rather than just blindly copy / pasting and not really understanding what it is doing - that is a recipe for disaster.

    Not sure if it will work with GMS2, you'd have to contact the author and ask the asset is something you think might be worthwhile.
     
    Last edited: Jan 27, 2020
  31. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    I think I'd rather trudge through it on my own. I seem to have more problems with the marketplace than what its worth.

    For example, I downloaded a lightning asset that the guy is really nice but its taking some time to learn his code and I think I bothered him too much. There were things I wanted to do with
    his asset that I couldn't just do out of the box.
     
  32. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    I think a large part of the issues you have is that you are trying to do things that are beyond your current ability, understanding and experience level and then trying to wing it as you go without having a firm grasp of the fundamentals to build upon.

    Rome wasn't built in a day, and certainly not on unsteady foundations.

    Time spent learning now will be saved tenfold down the road as your skills and coding chops improve with experience. There's a reason that people recommend building classic games when learning and it's not because Pong, Space Invaders or Pacman is everyones dream game - it's to gain valuable skills that are required to make all games without the complications of more advanced topics like hex grids and lighting systems.

    I'm not saying don't try and learn and play with these systems, I'm saying start slowly, take your time and make sure you understand the code, diving in the deep end without knowing how to swim usually ends with drowning.
     
  33. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    I think I'm also rushing through things because I'm the sole programmer out of three people building something I'm really still not familiar with me. I mean, I have a M.S. in Computer Science and I've been programming since high school and I'm 37. I'm not lacking in skill, I just want to get this game done. And sometimes I have to step back and look the big picture and that can be hard for me.
     
  34. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,041
    We'll have to agree to disagree, because none of that counts for anything when you are asking some of the questions you ask and struggle with some of the things you have struggled with which are generally basic fundamentals.
    Don't be to proud to go back to basics and make sure you understand how the engine and other features work before attempting more advanced topics.

    The manual is a goldmine of information on how the IDE and all it's features work so read it, re-read it and read it again! Use it as a reference along with other tutorials, guides and videos. Make new projects just to test out specific functions and features and get your head around how they work.

    I'm not saying you need to be an expert and know the manual off by heart, but being aware of the existence of a function or section in the manual will jog your memory later when you come across something you don't understand and need to code.

    Spend some time to learn how things work - and I include the IDE and debugger in that - knowing your tools will allow you to find errors like this one quickly and easily, and experience should lead to you not making them in the first place.

    This is a 30+ post thread where the problem was you setting the sprite_index every step - which was suggested in post #16 - and you could have caught that in less than 5mins if you were able to apply your knowledge of using the tools you have available to track these things down.

    Good luck with your game!
     
    Last edited: Jan 27, 2020

Share This Page