z order in mulitmesh 2d

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

Hi,
experimenting with godots 2d part I ran in a very simple problme I could not find a solution to: How do I set the z order for multimeshes? Particulraily, in the code example below, i’d like to set the zorder dependent on the y-coordinate. Did I miss something?

extends MultiMeshInstance2D

func _ready():
  self.multimesh.mesh = get_parent().mesh;
  for i in range(self.multimesh.instance_count):
	multimesh.set_instance_transform_2d(i, Transform2D(0.0, Vector2(float(randi()%2000), float(randi()%2000))));
  pass 
:bust_in_silhouette: Reply From: dschorak

well, multimesh2d just draws in the order of its instances.
if the order needs to be dependent on a coordinate:

  • if object coordinates stay stable, you can simply set the order accordingly.
  • If your objects move - you need to sort your instances according to the draw order you need, and then update the order dependingly… If you need to do this often, I can only assume there is heavy cost connected to such an approach - if noone disagrees with me I can only recommend against it…