GameMaker [SOLVED] Problem with moving an object a certain way.

G

Gronbal

Guest
Hi, so I'm in the progress of trying to master the Gamer Maker 2 programming language. Recently I stumbled across a little problem. What I'm trying to accomplish, is moving an item object to the center of another object, whereafter a series of events will trigger when the two objects are on top of eachother.
My item object is a multipurpose object, that can take shape and form of every item in the game, e.g armor, weapons and relics. To do this I use a variable called item_num which will determine what item it will be based on the integer assignet to it.
I'm therefore looking for a specific value in the variable item_num, and if that value is found, I will proceed to move the object. The problem is that the item isn't moved, If I drop another object, then remove it, then the right item object will move. First I thought this could have been caused by the object not being found if it was dropped, but by using show_debug_message() I could see that it indeed did run the code when the value was met.
My code is down below, and I hope you genius' can help me find a solution.

Code:
//Sets the temporary variable item_id to be the id of object_item, if it is in the collision range
var item_id = collision_circle(x, y, 200, obj_item, false, true);

//Checks to see if the variable item_num is the right value.
if(variable_instance_get(item_id,"item_num") = item.relic) {
    with(item_id){
 
   //Set some temporary variables
    var npc_x, npc_y, hspd, vspd, len, dir;
    npc_x = other.x;
    npc_y = other.y;

    //Get the length and direction
    dir = point_direction(x, y, npc_x, npc_y);
    len = spd;
 
    // Get the hspd and vspd
    hspd = lengthdir_x(len, dir);
    vspd = lengthdir_y(len, dir);

    // Move the item object
    phy_position_x += hspd;
    phy_position_y += vspd;
 
    }
}
 
Last edited by a moderator:

Paskaler

Member
I don't see anything wrong in the code you've posted, but there is something suspicious:

The 'obj_npc.x' refers to a random obj_npc's x instance variable if there are multiple instances of this object in the room, so it might be picking the wrong one here. If this code is executed from within the context of an obj_npc, then use other.x and other.y instead.

Code:
//Sets the temporary variable item_id to be the id of object_item, if it is in the collision range
var item_id = collision_circle(x, y, 200, obj_item, false, true);

//Checks to see if the variable item_num is the right value.
   with(item_id){
       if (item_num != item.relic)
            break;

   //Set some temporary variables
   var npc_x, npc_y, hspd, vspd, len, dir;
   npc_x = obj_npc.x; // <- here
   npc_y = obj_npc.y; // <- here
  
   //Get the length and direction
   dir = point_direction(x, y, npc_x, npc_y);
   len = spd;
  
   // Get the hspd and vspd
   hspd = lengthdir_x(len, dir);
   vspd = lengthdir_y(len, dir);

   // Move the item object
   phy_position_x += hspd;
   phy_position_y += vspd;
  
   }
 
G

Gronbal

Guest
Paskaler I see what you mean, I tried to change it, but sadly there was no improvement. Also there is only one instance of the npc object in the room.. Hmm very weird.
 
G

Gronbal

Guest
The problem was an overlapping, with a movement applied when dropping the item on the ground.
 
Top