SOLVED Minimap Rework

Hello guys. It's been a little while since I've posted.
I got this minimap for free off of this page here: Github Location and it's been a nice addition to the project. However, I'm stepping out in hopes that I may find a way to convert this system that uses the bbox of each object to draw a rectangle to instead draw a desired sprite in it's place. I've made up a variety of sprites that I want to represent each different class of ship and I've been making changes to the code to try and get this to work. The problem is that it's not finding the sprite and I get an error. My intuition with the code in the drawGUI event isn't properly grabbing the sprite from the list on the create event. Any ideas of what I could do to get this to work for me?
Create:
GML:
x = 1130;
y = 495;
scale = 0.05;
width = round(room_width * scale);
height = round(room_height * scale);
objects_to_draw = [
    Obj_Swarmer, c_lime, spr_alien,
    obj_pun, c_lime, spr_alien,
    obj_outrider, c_lime, spr_alien,
    obj_scout, c_lime, spr_alien,
    obj_Aura, c_white, spr_cruiser,
    obj_une, c_white, spr_cruiser,
    obj_aegis, c_white, spr_battleship,
    obj_wasp, c_white, spr_miner,
    obj_judgement, c_white, spr_dreadnought,
    obj_sentinel, c_white, spr_battleship,
    obj_base, c_blue, spr_basesquare,
    obj_frion, c_purple, spr_planet,
    obj_une_base, c_ltgray, spr_basesquare,
    Object2, c_orange, spr_planet,
    obj_comminer, c_white, spr_miner,
    obj_levi, c_white, spr_dreadnought,
    obj_drake, c_ltgray, spr_cruiser,
    obj_omen, c_ltgray, spr_battleship,
]
background_color = c_black;

global.map_on = false;
Draw GUI:

Code:
if instance_exists(global.target){
    if (global.map_on = true){
        if (global.target != obj_une_base){
            if (global.target != obj_base){
                draw_sprite_stretched(spr_waybutt, 0, 1045, 408, 550, 550);
                draw_set_color(c_white);
                draw_set_font(fnt_digital);
                draw_text(1147, 471, string(round(global.target.x))+"/"+string(round(global.target.y)));
                draw_set_color(background_color);
                draw_rectangle(x, y, x + width, y + height, false);

                //Drawing Instances
                for(var i = 0; i < array_length_1d(objects_to_draw); i += 2) {
                    var map_object_index = objects_to_draw[i],
                        map_object_color = objects_to_draw[i + 1],
                        map_object_sprite = objects_to_draw[i + 2];
    
                    draw_set_color(map_object_color);
                    
    
                    for(var j = 0; j < instance_number(map_object_index); j++) {
                        var instance = instance_find(map_object_index, j),
                            current_left = instance.bbox_left * scale,
                            current_top = instance.bbox_top * scale,
                            current_right = instance.bbox_right * scale,
                            current_bottom = instance.bbox_bottom * scale,
                            current_imageangle = instance.image_angle;
                

                    draw_rectangle(x + current_left, y + current_top, x + current_right, y + current_bottom, false);
                    draw_sprite_ext(map_object_sprite, 0, map_object_index.x, map_object_index.y, scale, scale, current_imageangle, c_white, 1);
                    }
                }
            }
        }
    }
}
 

TheouAegis

Member
Code:
for(var i = 0; i < array_length_1d(objects_to_draw); i++) { 
var map_object_index = objects_to_draw[i++],
 map_object_color = objects_to_draw[i++],
 map_object_sprite = objects_to_draw[i];
 

Nidoking

Member
Code:
for(var i = 0; i < array_length_1d(objects_to_draw); i++) {
var map_object_index = objects_to_draw[i++],
map_object_color = objects_to_draw[i++],
map_object_sprite = objects_to_draw[i];
Mucking with the index variable in the body of a for loop is bad form. I recommend against it.
 
Mucking with the index variable in the body of a for loop is bad form. I recommend against it.
Ok. I just learned for loops with arrays so some of this makes sense and some of it doesn't. So I changed it. Now I'm basically trying to draw the sprite listed instead of a rectangle now. Haven't gotten rid of the rectangle yet because I just figure the sprite would be drawn over it once I get the code right and then I'll get rid of the rectangle. This is what I have now and so far it isn't working. I just see the rectangles.
GML:
if instance_exists(global.target){
    if (global.map_on = true){
        if (global.target != obj_une_base){
            if (global.target != obj_base){
                draw_sprite_stretched(spr_waybutt, 0, 1045, 408, 550, 550);
                draw_set_color(c_white);
                draw_set_font(fnt_digital);
                draw_text(1147, 471, string(round(global.target.x))+"/"+string(round(global.target.y)));
                draw_set_color(background_color);
                draw_rectangle(x, y, x + width, y + height, false);

                //Drawing Instances
                for(var i = 0; i < array_length_1d(objects_to_draw); i++) {
                    var map_object_index = objects_to_draw[i++],
                        map_object_color = objects_to_draw[i++],
                        map_object_sprite = objects_to_draw[i];
    
                    draw_set_color(map_object_color);
                    
    
                    for(var j = 0; j < instance_number(map_object_index); j++) {
                        var instance = instance_find(map_object_index, j),
                            current_left = instance.bbox_left * scale,
                            current_top = instance.bbox_top * scale,
                            current_right = instance.bbox_right * scale,
                            current_bottom = instance.bbox_bottom * scale,
                            current_imageangle = instance.image_angle,
                            currentX = instance.x,
                            currentY = instance.y;
                

                    draw_rectangle(x + current_left, y + current_top, x + current_right, y + current_bottom, false);
                    draw_sprite_general(map_object_sprite, 0, x - sprite_width/2, y - sprite_width/2,
                    sprite_width, sprite_height, currentX, currentY,
                    scale, scale, current_imageangle, map_object_color, map_object_color, map_object_color, map_object_color, 1);
                    }
                }
            }
        }
    }
}
 

Nidoking

Member
You're mucking with the index variable in the body of the for loop. If you want to do that, that's your business, but if you're going to quote me telling you not to do that and say "Ok," maybe don't do the thing.

Which rectangles are being drawn? You've got two draw_rectangle calls here, which seems redundant. If you get rid of the first one, does it still draw rectangles? If so, then there's something wrong with your draw_sprite_general call. If not, then it may not be getting into the for loop at all. The use of the deprecated array_length_1d function is a bad indicator. The use of a second nested for loop instead of a with is another. Seems like some debug prints that would tell you what's actually happening might be a good idea.
 
You're mucking with the index variable in the body of the for loop. If you want to do that, that's your business, but if you're going to quote me telling you not to do that and say "Ok," maybe don't do the thing.

Which rectangles are being drawn? You've got two draw_rectangle calls here, which seems redundant. If you get rid of the first one, does it still draw rectangles? If so, then there's something wrong with your draw_sprite_general call. If not, then it may not be getting into the for loop at all. The use of the deprecated array_length_1d function is a bad indicator. The use of a second nested for loop instead of a with is another. Seems like some debug prints that would tell you what's actually happening might be a good idea.
So the first rectangle draw is for the background of the minimap.(The black square background). The second is drawing a rectangle for each object in the game inside the minimap. The index variable inside the body of the for loop is not my code. I got this minimap from a Github page as I stated at the beginning of the post. My goal is not to mess with anything that I don't have to. I'm just trying to change this to draw a sprite instead of a rectangle.
 
So I have the sprites being drawn now. But instead of them being in the minimap they're drawn all over the room and occasionally one just kind of flies by. lol
GML:
                for(var i = 0; i < array_length_1d(objects_to_draw); i++) {
                    var map_object_index = objects_to_draw[i++],
                        map_object_color = objects_to_draw[i++],
                        map_object_sprite = objects_to_draw[i];
    
                    draw_set_color(map_object_color);
                    
    
                    for(var j = 0; j < instance_number(map_object_index); j++) {
                        var instance = instance_find(map_object_index, j),
                            current_left = instance.bbox_left * scale,
                            current_top = instance.bbox_top * scale,
                            current_right = instance.bbox_right * scale,
                            current_bottom = instance.bbox_bottom * scale,
                            current_imageangle = instance.image_angle,
                            currentX = instance.x,
                            currentY = instance.y;
                

                    draw_rectangle(x + current_left, y + current_top, x + current_right, y + current_bottom, false);
                    draw_sprite_ext(map_object_sprite, 0, currentX, currentY, .4, .4, current_imageangle, c_white, 1);
                    }
                }
            }
 

TheouAegis

Member
First off, you are using instance.x and instance.y as is. Shouldn't you be multiplying those by scale? Also, why are current_left et al being added to the control object's coordinates? Even if that's what you want, why aren't you adding currentX and currentY to the coordinates also?
 
First off, you are using instance.x and instance.y as is. Shouldn't you be multiplying those by scale? Also, why are current_left et al being added to the control object's coordinates? Even if that's what you want, why aren't you adding currentX and currentY to the coordinates also?
Hey. So the good news is I'm getting closer to the prize. I did as you suggested and multiplied instance.x and instance.y by scale. Now all the sprites are appearing in the upper left-hand corner of the screen. I need to get them all down into the lower right-hand corner and find a way to adjust their size depending on whether the object is big/small in the game room. I'm not sure how I could add variables like current_left into a draw_sprite_ext function but if there's something I can do with it I'm all ears.
 

TheouAegis

Member
Why do you need current_left with draw_sprite? For drawing a rectangle, yes. For drawing a sprite, I see no reason for it. As for positioning it in the bottom corner, you need to add currentX and currentY to the coordinates of your minimap.
 
Why do you need current_left with draw_sprite? For drawing a rectangle, yes. For drawing a sprite, I see no reason for it. As for positioning it in the bottom corner, you need to add currentX and currentY to the coordinates of your minimap.
I ended up figuring this all out and it looks great now. I'm going to post the final code here in hopes that I can give back to the community in the ways that the community has helped me. As I stated at the beginning of this post I acquired this code from AnnoyedGrunt as a free asset posted on the internet. You can look at the original code here: Github Page.

I know certain portions of this code bother certain people for various reasons but the bottom line is IT WORKS. And I wasn't going to go changing anything unless I really knew what I was doing. I simply wanted to change the code so that instead of making rectangular representations of objects onscreen you could create a sprite for each one and use those instead. Here's the code:

Create Event:
GML:
//You may have to change these values depending on the resolution of your game
x = 1130;
y = 495;

scale = 0.05;
width = round(room_width * scale);
height = round(room_height * scale);

//Object, color of sprite, sprite
objects_to_draw = [
    Obj_Swarmer, c_lime, spr_alien,
    obj_pun, c_lime, spr_punicon,
    obj_outrider, c_lime, spr_alien,
    obj_scout, c_lime, spr_alien,
    obj_Aura, c_white, spr_cruiser,
    obj_une, c_white, spr_cruiser,
    obj_aegis, c_white, spr_battleship,
    obj_wasp, c_white, spr_miner,
    obj_judgement, c_white, spr_dreadnought,
    obj_sentinel, c_white, spr_battleship,
    obj_base, c_blue, spr_basesquare,
    obj_frion, c_purple, spr_planet,
    obj_une_base, c_navy, spr_basesquare,
    Object2, c_orange, spr_planet,
    obj_comminer, c_white, spr_miner,
    obj_levi, c_white, spr_dreadnought,
    obj_drake, c_navy, spr_cruiser,
    obj_omen, c_navy, spr_battleship,
]
background_color = c_black;

global.map_on = false;
Draw GUI Event:


GML:
//Drawing the Background

if (global.map_on = true){
        draw_set_color(background_color);
        draw_rectangle(x, y, x + width, y + height, false);

        //Drawing Instances
        for(var i = 0; i < array_length_1d(objects_to_draw); i++) {
            var map_object_index = objects_to_draw[i++],
                map_object_color = objects_to_draw[i++],
                map_object_sprite = objects_to_draw[i];
   
                    draw_set_color(map_object_color);
                   
                    //Getting the object information
                    for(var j = 0; j < instance_number(map_object_index); j++) {
                        var instance = instance_find(map_object_index, j),
                            current_imageangle = instance.image_angle,
                            currentX = instance.x * scale,
                            currentY = instance.y * scale;
               
                    //Drawing the sprite to scale and at the same image angle as the object
                    //Note that values in the next line will need to be changed for the resolution of your game
                    draw_sprite_ext(map_object_sprite, 0, currentX + 1127, currentY + 495, map_object_index.image_xscale/5, map_object_index.image_yscale/5, current_imageangle, map_object_color, 1);
        }
    }
}
Also note that if you want to make some sprites appear bigger in the minimap all you have to do is change the sprite size.
Thank you Gamemakers for being such great help and support to us beginners! I've learned so much in the last year and will have fun learning more.

~KungFuChowder
 
Top