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] Need Help with using Item Pick-Ups

Discussion in 'Programming' started by MechaMonst3r, Jan 17, 2020.

  1. MechaMonst3r

    MechaMonst3r Member

    Joined:
    Jan 17, 2020
    Posts:
    9
    Hey! I'm decently new to Game Maker Studio 2, and GML as a programming language but not unfamiliar with programming it's self (I'm a second year Computer Science student). Anyways, I'm attempting to make my first game and I'm having some issues with collision detection when picking up the item object. In-game the item will play the sprite and depending on what image index it's on will depend on what item will appear in your Item UI and what effect it does. Now: When you first start the game, your player can collide with the item and it appears in your Item UI. You can the press Space to "use" the item, the item disappears and the effect on the Player takes hold. All is well; until the Player collides with another item. This time the collision does not register at all and it just passes through the Player completely... and every item after that does the same. I have no idea why this is happening, I scoured my UI code and Item code that handles everything and I can't figure it out for the life of me. So I'm hoping you guys have some answers for me!

    Code for the item Step Event that handles collision with the Player:
    Code:
    direction = 180;
    speed = 4*global.comboSpeed;
    
    if(place_meeting(x - 1, y, obj_player))
      {
       if(image_index = 0)
        {
         global.is_red_item = true;
        global.is_green_item = false;
         global.is_blue_item = false;
        global.have_item = true;
         instance_destroy(self);
        }
    
       if(image_index = 1)
        {
         global.is_green_item = true;
         global.is_blue_item = false;
        global.is_red_item = false;
        global.have_item = true;
        instance_destroy(self);
        }
       
        if(image_index = 2)
          {
           global.is_blue_item = true;
           global.is_red_item = false;
           global.is_green_item = false;
           global.have_item = true;
           instance_destroy(self);
          }
      }
    
    Code for Step Event for the UI for Items:
    Code:
    if(global.have_item = false)
      {
       obj_item_hud.visible = false; 
      }
    
    if(global.have_item = true)
    {   
     if(global.is_blue_item = true)
      {
       image_index = 2;
       obj_item_hud.visible = true;
       
       if(global.using_item)
          {
           alarm[0] = 5;     
           global.use_blue_item = true;
           global.use_green_item = false;
           global.use_red_item = false;
           global.have_item = false;
          }   
      }
    
     if(global.is_green_item = true)
      {
       image_index = 1;
       obj_item_hud.visible = true;
       
       if(global.using_item = true)
          {
           global.use_green_item = true;
           global.use_blue_item = false;
           global.use_red_item = false;
           global.have_item = false;
          }   
      }
    
     if(global.is_red_item = true)
      {
       image_index = 0;
       obj_item_hud.visible = true;
       
       if(global.using_item = true)
          {
           alarm[1] = 5;     
           global.use_red_item = true;
           global.use_blue_item = false;
           global.use_green_item = false;
           global.have_item = false;
          }     
      }
    }
    
    Please excuse any rookie mistakes I have made, and thank you in advance!
     
  2. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    765
    Im pretty sure it's because you're changing global variables from within every instance of your item objects.

    If you're already setting the image index of an item when it spawns (and it stays the same image_index) then you can just use the image index as the reference )kinda like you are now but without setting/changing all those globals.
    Code:
    //When you detect collision with the player:
    
    obj_player.new_item = image_index
    Now you can use this new variable to determine what happens when space is pressed. Initialise it as -1 inside obj_players create event and then when it is changed by a collision new_item will be either 0, 1, or 2.
     
  3. MechaMonst3r

    MechaMonst3r Member

    Joined:
    Jan 17, 2020
    Posts:
    9
    Awesome! Thanks for the reply and I'll definitely give it a go in the morning. I was thinking it probably had to do with how many global variables I was using to get it to work. Anyways, I'll let you know how it turns out. :) Seriously though, very much appreciated.
     
  4. MechaMonst3r

    MechaMonst3r Member

    Joined:
    Jan 17, 2020
    Posts:
    9
    Hey! So i am still having issues. While Rob's suggestion has drastically reduced the amount of code I was using, as well as greatly improved my collision detection with my items, now for some reason I can't USE my items. I've tried to debug using the output log and it's running perfectly up until I press "space" to use the currently picked up item. Then it just doesn't work at all, the game is properly registering my space bar and everything.

    Code for my Item step event:
    Code:
    direction = 180;
    speed = 4*global.comboSpeed;
    
    image_index = index_chooser;
    image_speed = 0;
    
    if(place_meeting(x - 1, y, obj_player) && global.have_item = false)
      {
       audio_play_sound(snd_item_pickup, 20, false);    
       obj_player.new_item = image_index;
       global.have_item = true;
       instance_destroy(self);
      }
    
    Code for my Item UI step event that handles using/not using items:
    Code:
    if(global.game_start = true)
    {
    new_item = obj_player.new_item;
    
    if(itemKey)
      {
       using_item = true;
       show_debug_message("Space is being pressed!");  
      }
    
    else
       using_item = false;
    
    
    if(global.have_item = false)
      {
       obj_item_hud.visible = false;
      }
    
     if(global.have_item = true)
       {
        image_index = new_item;  
        obj_item_hud.visible = true; 
     
       //Everything registers fine up to this point, doesn't work after this.
        if(using_item)
          {     
           if(image_index = 2)
             {
              global.use_blue_item = true;
              global.have_item = false;
              show_debug_message("item is being used!");  
             }
    
           if(image_index = 1)
             {
              global.use_green_item = true;
              global.have_item = false;
              show_debug_message("item being used!");  
             }
    
           if(image_index = 0)
             {
              global.use_red_item = true;
              global.have_item = false;
              show_debug_message("item being used!");  
             }
           }
        }
    }
    
    else
      obj_item_hud.visible = false;
    
     
  5. TailBit

    TailBit Member

    Joined:
    Oct 16, 2019
    Posts:
    142
    When is itemKey set to true?

    Try check the values at:
    Code:
    //Everything registers fine up to this point, doesn't work after this.
    show_debug_message("using: " + string(using_item) + " index: " + string(image_index) )
     
  6. MechaMonst3r

    MechaMonst3r Member

    Joined:
    Jan 17, 2020
    Posts:
    9
    I have it set up so itemKey becomes true whenever you press space.
     
  7. MechaMonst3r

    MechaMonst3r Member

    Joined:
    Jan 17, 2020
    Posts:
    9
    I used your debug message and I think I found the issue. Apparently the image_index is set at 1.62 when you try and use the item, thus why none of the code is working when space is pressed. Any reason why thats happening? How can I fix it>
     
  8. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,389
    Is it always exactly 1.62?

    Double check you have set image_speed to 0 in the create event for all relevant objects.
     
  9. TailBit

    TailBit Member

    Joined:
    Oct 16, 2019
    Posts:
    142
    And do some debugs on it before you pass the value on so something else .. that is a strange value tho.
     
  10. MechaMonst3r

    MechaMonst3r Member

    Joined:
    Jan 17, 2020
    Posts:
    9
    Yep! Every time I would re-test the game it would show up as 1.62.. which is really odd but I'm gonna try and collide with a different item type and see what happens. I'll make sure that image_speed is set to 0 in relative create events.
     
  11. MechaMonst3r

    MechaMonst3r Member

    Joined:
    Jan 17, 2020
    Posts:
    9
    I'll put a debug everywhere I'm passing the value so I can see exactly where I'm getting that number.
     
  12. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    765
    How is "index_chooser" being set?

    Is an item's image supposed to stay the same all the time?

    If yes to the second question then as long as you have image_speed set to 0 in the create event and the wanted image index is being assigned properly, you shouldn't have any problems.

    Something like:
    Code:
    //create event of item obj
    image_speed = 0;
    
    var max_images = sprite_get_number(spr_items) - 1;
    
    image_index = irandom(max_images);
    show_debug(string(image_index));
     
  13. MechaMonst3r

    MechaMonst3r Member

    Joined:
    Jan 17, 2020
    Posts:
    9
    So yes its image isnt supposed to change, when an item randomly spawns it will randomly choose which frame to use for the item (each frame is a different item.) My creation event looks similar to the one you just posted though. On creation it sets its image_speed to 0, and then randomly chooses what frame the Sprite is gonna sit at. So when the item spawns in the game, and the player collides with it, it checks the items current image_index and then passes that value around to execute everything else. Seems like it's not passing properly somewhere though, when I'm at my PC I'm gonna set up some debug messages to really narrow it down. If I cant figure it out still I may have to change how I'm going about this all together.
     
  14. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    765
    Yeah it sounds like you have some fun debugging to do!
     
  15. MechaMonst3r

    MechaMonst3r Member

    Joined:
    Jan 17, 2020
    Posts:
    9
    Fixed the issue! Turns out when choosing which image_index for my item to use, I was using random_range(0, 2); which apparently includes decimal points. Changing it to irandom_range(0, 2); resolved the issue completely and my item system is currently working as intended. Thanks so much for helping me figure this out guys! Really really appreciated!
     
    IndianaBones likes this.

Share This Page