Add a node (sprite) on screen after mouse click, on mouse coordinates ?

fkossyvasfkossyvas Posts: 7Member

Hi, i am starting just now studying Godot but i cannot understand how can i do this :
i want to add an object on screen (from what i understand, i have to instantiate it), just where i click the left mouse button (place it at coordinated of mouse click).
What should be the best way to do it ?
Thank you in advance.

Answers

  • AvencherusAvencherus Posts: 49Member
    edited February 24 Answer ✓

    The best way depends on your situation, but here is a basic example.

    extends Node2D
    
    func _ready():
        set_process_input(true)
    
    
    func _input(event):
        if(event.type == InputEvent.MOUSE_BUTTON and event.button_index == BUTTON_LEFT and event.pressed):
            var sprite = Sprite.new()
            sprite.set_texture(preload("res://icon.png"))
            sprite.set_pos(event.pos)
            add_child(sprite)
    
  • fkossyvasfkossyvas Posts: 7Member

    Thank you, i will try it right away !

  • AvencherusAvencherus Posts: 49Member
    Answer ✓

    No problem.

Sign In or Register to comment.