frame drop when instancing and freeing tilemap node in tree.

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

So I am doing a map generator, when the player reaches certain x position a tilemap is loaded and another is distroyed I use the following code to do this:

func _on_createa2_body_entered(body):
	print("create")
	total_distance = total_distance + 1024
	var new_tile = TileMap1.instance()
	new_tile.position.x = total_distance
	tiles.push_back(new_tile)
    #Here I add the instance to the tree
	get_tree().get_root().get_child(0).get_node("Tiles").add_child(new_tile)
	pass 


func _on_destroya2_body_entered(body):
	print("destroy")
	var arreglo = tiles.pop_front()
    #Here I remove the node
	arreglo.queue_free()
	pass 

For some reason the games slowdown, I read into object pooling but some dev a time ago said that was not necessary in godot so I dont know what to do.

:bust_in_silhouette: Reply From: Reloecc

What do you expect you’ll get as an answer here? If someone said “pooling is not needed” he maybe meant “godot has good memory management” he didn’t meant “instancing new objects won’t take any cpu”.

In 60 FPS environment, there is only 15 ms to load your tilemap without framedrop (as you probably know). If your tilemap is big or has many objects you have to expect some cpu usage.

But… you allways have options for sure, at first read this: Background loading — Godot Engine (stable) documentation in English

But a simpler solution may be reducing your tilemap sizes (make more chunks). Or preload them when player is inactive (reading text, mixing potions, opening inventory and so on) if possible.