Legacy GM with(furthest_object)

Dagoba

Member
Hello!

I am trying to do something like this:
- Get an ID of an object that is the furthest from goal.x and goal.y
- The object must have a certain variable being a certain string, like "Blue" or "Yellow"

How to do this?
This is inside a script, you use it like this: scr_upgrade_item("Blue");
And the script looks like this:
Code:
globalvar color;
color = argument[0];
globalvar furthest_color;
var cost;

if (instance_exists(par_color)) {
    with(par_color) {
        if (type == color) {
            furthest_color = instance_furthest(obj_goal.x, obj_goal.y, id);
        }
    }
}

cost = furthest_color.upgrade_cost;

with(furthest_color) {
    if (type == "Blue") {
        global.AI_cash -= cost;
        upgrade_level++;
    }
}
I think that the problem is with the globalvar color, but I do not know how to do it.
Should I make a global variable instead of globalvar?

Thanks in advance!
 

CloseRange

Member
your problem is the line:
Code:
furthest_color = instance_furthest(obj_goal.x, obj_goal.y, id);
because you arn't checking if it's further than every other par_color only that it's further than every other object with it's unique "id" (only itself, therfor it will always return itself"
here is what that line probobly should be replaced with (could be wrong)
Code:
var dis = point_distance(obj_goal.x, obj_goal.y, x, y);
if  dis< point_distance(obj_goal.x, obj_goal.y, furthest_col.x, furthest_col.y) {
     furthest_col = id;
}
what this is doing is taking both the distance of the calling object and the distance of the current "furthest object" and checking what one is further. If the calling is further then it will set furthest_col to itself
 

Simon Gust

Member
The problem here is that you feed the instance_furthest function with only 1 possible instance each time so it will always return "id". Of course every instance has id and then furthest color is just going to be the last instance checked that has the right color.
Code:
var color = argument0;
var furthest = 0;
var furthestID = noone;

var goalX = obj_goal.x;
var goalY = obj_goal.y;

with (par_color) {
    if (type == color) {
        var dis = point_distance(x, y, goalX, goalY);
        if (dis > furthest) {
            furthest = dis;
            furthestID = id;
        }
    }
}

if (furthestID != noone)
{
    // rest of the code
   
}
If furthestID is not noone, it means that it holds the id of the furthest away instance
that also has the right type.
 
Top