0 votes

I want to make a MiniMap, and I have a idea, but please write if this idea is a bad attempt.

So this is my idea:

I have a Frame for my MiniMap and inside of the Frame, I want to show the MiniMap. I made the whole Map as a png File and now I want to show just a small part of this map inside of my Frame and then, when the Player moves, I want to move my map as well, but I want that only the part of the map inside the Frame is visible. Is there a way to do that or is this a bad attempt for a MiniMap?

Thanks for answers and ask if there are ambiguities.

PS: Sorry for bad English and the word ambiguities is from the google translator...

in Engine by (330 points)

1 Answer

0 votes
Best answer

I am using TextureRect for minimap and i am using shader to move map with my player.
Create TextureRect and check expand and set stretch mode to tile. Then add shader

Shader code

shader_type canvas_item;
uniform vec2 pos;

void fragment()
{
    COLOR = texture(TEXTURE, UV + pos);
}

code

var worldsize = Vector2() #your world size


func moveMapWithPlayer():
    #half resolution , used to keep player centered in minimap
    var half_res = get_viewport().size / 2  
    var pos = player.position - half_res

    #clamp
    pos.x = clamp(pos.x,0, worldsize.x)
    pos.y = clamp(pos.y,0, worldsize.y)
    material.set_shader_param("pos",pos / worldsize)

func _process():
    moveMapWithPlayer()
by (731 points)
selected by

This is how it looks like in my game : https://www.youtube.com/watch?v=7Hd66AzfdXo&t=17

I add the second code to the TextureRect and the first code to the shader of the TextureRect, right? Because I got a error, which says that the variable pos isnt declared.

What did I wrong?

sorry it's
var pos = player.position - half_res

and u have to set worldsize

I set the worldsize to 128,128 and I wrote 0,0 as the player.position, in order to test it.

And does it matters where the minimap is located? Because it still happens nothing.

I can still see the rest of the MiniMap outside the Frame

worldsize is in pixels 128x128 is very small

ok I increesd it to 12800, 12800 (my world isnt finished so its just a number) and this is, how it looks in the editor right now:image

Obviously, you cant open the image... In fact, it still doesnt work and does the start position of the Player and the Minimap maters?

i cant view ur image

have u assigned texture in textureRect?

Can I add a .tscn to a comment? Because then I would be able to send you my scene

you cannot send .tscn directly , you need to host it somewhere and send link in comment.
Upload here http://s000.tinyupload.com/ and paste the link in comment

Does this works? (I make it in a new project, where nothing else then the MiniMap and a FakePlayer is.)

http://s000.tinyupload.com/?file_id=28969992982839350570

It is a 7zip file, I hope you can deal with it. If not write me. The important thing is not to move the MiniMap, the important thing is that I cant see the MiniMap outside of the Frame. Thats why I made a fix Node2d as a Player, in order to test it.

I have modified your minimap
Move player with arrow keys
Try moving towards right you will see minimap moving with player

http://s000.tinyupload.com/?file_id=05176349353160326372

To create minimap.png like i have done , you need to-
1.)Make a level
2.)capture screen shot

use this image as ur minimap

Ok, thank you!

But the problem was less that my map is empty and more that you can see the rest of the map outside the frame

I will make another question on this later

Its my function to generate minimap of a level.

func captureMap():
    #minimap to actual map ratio
    var ratio = 8   

    #64 is tile size
    var size = $TileMap.get_used_rect().size * Vector2(64,64)
    OS.window_size = size / Vector2(ratio,ratio)
    var max_xy = min(size.x,size.y)
    var ratio
    if max_xy == size.x:
        ratio = OS.window_size.x / max_xy
    else:
        ratio = OS.window_size.y / max_xy

    self.scale = Vector2(ratio,ratio)

    yield(get_tree(), "idle_frame")
    yield(get_tree(), "idle_frame")
    yield(get_tree(), "idle_frame")
    yield(get_tree(), "idle_frame")
    # Retrieve the captured Image using get_data()
    var img = get_viewport().get_texture().get_data()
    # Flip on the y axis
    # You can also set "V Flip" to true if not on the Root Viewport
    img.flip_y()
    # Convert Image to ImageTexture
    img.save_png("res://minimap.png")

and btw

worldsize = $TileMap.get_used_rect().size * Vector2(64,64)

64 is default tile size

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.