So I have the basics for a 2D drag and drop physics game setup, however I am unsure on how to achieve what I want.
The test physics object I setup at the moment, can be “dragged” which instantiates a preview scene that has no physics and is just glued to the mouse, upon releasing the click, the preview scene then instantiates the physics object scene and it appears at the preview location.
This works with the test objects, but obviously I wanna have much more draggable stuff, and I don’t want to make a custom preview scene for every one of them, especially since some of them may be dynamic and change in real time.
One solution I thought of is having like a “Preview” class and a “Physics Object” class, and the physics object class has a sprite node that a preview class takes in order to make itself, but im not really sure what to go from there.
My bad, here’s what I made so far
Here is a physics object I made that a player can drag around, as you can see it instantiates a “preview” scene
Below it is the “preview” scene it’s just a simple copy of the sprite that follows the mouse and spawns the physics object again
extends RigidBody2D
var mouseentered = false
const BLOCKPREVIEW = preload("res://TestScenes/test_block_preview.tscn")
@onready var area: Area2D = $Area2D
func _ready():
area.input_event.connect(_on_input_event)
func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int):
if Input.is_action_pressed("Click") and !Events.blockpickedup:
Events.blockpickedup = true
var PlanterPreview = BLOCKPREVIEW.instantiate()
PlanterPreview.global_rotation = global_rotation
get_tree().current_scene.add_child(PlanterPreview)
queue_free()
extends Node2D
var BLOCK = load("res://TestScenes/test_block.tscn")
# Called when the node enters the scene tree for the first time.
func _ready():
Events.blockpickedup = true
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
global_position = get_global_mouse_position()
if Input.is_action_just_pressed("wheelup"):
global_rotation += deg_to_rad(10)
if Input.is_action_just_pressed("wheeldown"):
global_rotation -= deg_to_rad(10)
if Input.is_action_just_released("Click"):
var new_planter = BLOCK.instantiate()
new_planter.global_position = global_position
new_planter.global_rotation = global_rotation
get_tree().current_scene.add_child(new_planter)
Events.blockpickedup = false
queue_free()
As you can see here, both of the times they are just making and creating new scenes every time you move the physics object, however I want the objects to have some data that are unique to each one, such as randomized sprites for example. As such I want the preview scene to be able to store the data of that object so that when the player drags it around it actually stays the same
So, in your preview scene could you not have variables of the value you need and when you instantiate it set those balues to equal the value you need in the object, then do the reverse when you instantiate the original object again?
Example both scenes have or if it is a property then just the preview scene needs it:
var texture :Texture
other wanted variables
On pickup previewScene.texture = realScene.texture
When dropped realScene.texture = previewScene.texture