How can I access a node(mesh instance) from a grid map?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dabin

Hello. I use a translator because I can’t speak English. Please excuse me. ㅠ. ㅠ

I use GridMap in my project to construct a map. By the way, how can you access the accessed node from the code?
I tried get_item _mesh method of mesh_library, but this is not the node I want.
I want to access the node and change its location during runtime.

At this point, I think it’s easier to use MeshInstance directly from the beginning without using GridMap.
But isn’t there any performance problem when you do that way? GridMap will be faster internally. I also want the game to work smoothly on my smartphone.

:bust_in_silhouette: Reply From: johnygames

Take a look here: GridMap — Godot Engine (stable) documentation in English

You should use Gridmap’s methods instead of its properties. More specifically, you need the get_cell_item ( int x, int y, int z ) method, which finds what item is on a specific location, and the get_meshes ( ) method, which outputs all the meshes that you have placed. If you want to change the item of a cell, you might want to use the set_cell_item ( int x, int y, int z, int item, int orientation=0 ) method, which changes or erases a cell item at the specified xyz coordinates. Check the other methods as well, you might find something that suits your needs.

You use the Gridmap methods simply by making a script and calling the methods. For example, you can do this:

extends GridMap


func _ready():
	var meshL = get_meshes().size() # gets how many meshes you have placed
	var meshC = get_cell_item(10,10,10) # finds item at xyz coordinates
	print(meshL, meshC)

Finally, you could use the Gridmap as is and only place MeshInstances where they are absolutely necessary. For example, if you know a box will have to be moved, make this box a separate MeshInstance.

In any case, I think that Gridmap is way faster because it makes a single draw call to the GPU, but I am not sure.

Does this answer your question? If it does, please mark this answer as best.

Thank you for your kind reply :). I checked the documents, and I tried get_cell_item and other methods, but it was Vector3, not Node. But to do what I want to do, I think we can use the separate MesshInstance as you say. Thank you!

Dabin | 2019-08-12 08:21