Skip to content

Instantly share code, notes, and snippets.

@Gnumaru
Created November 8, 2024 14:05
Show Gist options
  • Save Gnumaru/5160a440ebe16412d47390b39b3b0b14 to your computer and use it in GitHub Desktop.
Save Gnumaru/5160a440ebe16412d47390b39b3b0b14 to your computer and use it in GitHub Desktop.
extends Node
func pack(p:Node)->PackedScene: var v = PackedScene.new(); v.pack(p); return v
class A extends Node:
@export var m: String = "a"
static func instantiate(p:PackedScene):
var v: A = p.instantiate()
v._post_init()
return v
func _init()->void: print(m) # this will always print "a"
func _post_init()->void: print(m) # this will print whatever was the value was when the scene was packed
func _ready() -> void:
var v: Variant = A.new() # v now is an "A" instance and will print "a"
v.m = "my string"
var p = pack(v) # p now is a PackedScene
v = p.instantiate() # v now is a new "A" instance and will print "a"
print(v.m) # will print "my string"
print("\n")
v = A.instantiate(p); breakpoint # will print "a" and then print "my string"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment