0 votes

Hello everybody,

I'm working on a interface with draggable apples, where I'm meant to be able to click on something, at the moment a button node, and get an apple kinematic body that I can drag and drop somewhere, and if "dropped" moves down simulating gravity with moveandcollide.

My problem is that the button spawned apple has a translated drag-anchor point compared to the in-scene positioned apple that I places mayself as testing double. Both attach to mouse pointer by clicking inside the collision shape, but the ones spawned by the button attach far to the left top, kind of like 0,0 coordinates of something.

here is the script for the apple node (istanced)

extends KinematicBody2D

var mouse_in = false
var dragging = false

func _ready():
mouse_in = true

func _process(delta):
if (mouse_in && Input.is_action_pressed("left_click")):
    dragging = true

if (dragging && Input.is_action_pressed("left_click")):
    var position = get_viewport().get_mouse_position()
    dragging = false

func _physics_process(delta):
    move_and_collide(Vector2(0, 1))

func _on_Apple_mouse_entered():
 mouse_in = true

func _on_Apple_mouse_exited():
mouse_in = false

and here is the button code

extends Button

var spawntimer = OS.get_system_time_secs()
var isdown = false

func _on_AppleButton_button_down():
if (OS.get_system_time_secs() - 2 > spawntimer):
    spawntimer = OS.get_system_time_secs()
    var scene = load("res://Apple.tscn")
    var scene_instance = scene.instance()

there is a timed throttle for the spawning event with OS time.

I don't understand why the Apple I placed myself behaves like I need it to, and the other does not.

Thanks for any help.

in Engine by (19 points)

1 Answer

0 votes
Best answer

Found the solution myself:


instead of just addchild(sceneinstance) in the button script, the Apple center is fixed for the spawned one. Apparently the node was being added as child of the button and getting weird positions from getviewport().getmouse_position()

by (19 points)

Careful with adding to the root, it is the root viewport, not the current scene, which may give you problems later.

You can do get_tree().current_scene.add_child() to add it to the current scene without guessing the current scene path.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.