When in a YSort node, if I have
func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
print("click at: ", event.position)
var enemyNode = load("res://Enemy.tscn").instance()
enemyNode.position = event.position
add_child(enemyNode)

I don't see the enemyNode. When I have the enemyNode position itself from enemyNode:_ready(), it does show up. Why doesn't it appear? is a position special when adding to YSort?

  • Jack9

    The issue is that the event was not "local".
    eventPositionCorrect = make_input_local(event)
    eventPositionCorrect = get_viewport().canvas_transform.affine_inverse().xform(event.position)
    eventPositionCorrect = get_global_mouse_position()

    Now enemyNode.position = eventPositionCorrect
    works as expected.
    This is the like the Nth Godot gotcha that makes basic things difficult.

If the ysort isn't at origin, you'll have to convert global coordinates to the ysort's local coordinates.

enemyNode.position = ysort.to_local(event.position)

func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
print("click at: ", event.position)
var enemyNode = load("res://Enemy.tscn").instance()
enemyNode.position = event.position
add_child(enemyNode)
// using this line every copy spawns and appears in the same place
// add_child(load("res://Enemy.tscn").instance())
pass

So it's not a because ysort is off the origin (it's not).

This code works fine, and it's doing basically the same thing as your first clip, which makes me think that the problem is somewhere else in your project. You might try using global_position as well as to_local().

extends Node2D

var ys

func _ready() -> void:
	ys = YSort.new()
	ys.position = Vector2.ONE * 200
	add_child(ys)


func _input(event: InputEvent) -> void:
	if event.is_pressed() and event is InputEventMouseButton:
		print(event.global_position)
		var sp = Sprite.new()
		sp.texture = preload('res://icon.png')
		sp.position = ys.to_local(event.global_position)
		ys.add_child(sp)

By the way, if you put "~~~" around your code, it will format better.

    duane

    func _input(event: InputEvent) -> void:
    	if event.is_pressed() and event is InputEventMouseButton:
    		print(event.global_position)
    		print(event.position)
    		var enemyNode = load("res://Enemy.tscn").instance()
    		enemyNode.position = to_local(event.global_position)
    		add_child(enemyNode)
    
    		#if I add this, the sprite appears
    		#add_child(load("res://Enemy.tscn").instance())
    	pass


    print(event.global_position) print(event.position) end up being the same (may be because YSort is at 0,0)
    Adding a texture may be different from adding a Scene.

      Jack9

      The issue is that the event was not "local".
      eventPositionCorrect = make_input_local(event)
      eventPositionCorrect = get_viewport().canvas_transform.affine_inverse().xform(event.position)
      eventPositionCorrect = get_global_mouse_position()

      Now enemyNode.position = eventPositionCorrect
      works as expected.
      This is the like the Nth Godot gotcha that makes basic things difficult.