How to access a nodes properties dynamically (code reference)
- Edited
REVBENT Here's a simple example to play with:
Resource class:
class_name Data extends Resource
var foo = 1
Instantiated scene class (scene.gd):
extends Node
@export var data = Data.new()
Script that does scene instantiation:
extends Node
func _ready():
var s1 = load("res://scene.tscn").instantiate()
var s2 = load("res://scene.tscn").instantiate()
print(s1.data)
print(s2.data)
You should get a printout of two different Data resource objects:
<Resource#-9223372008316730102>
<Resource#-9223372008132180722>
In the case you don't do Data.new()
in the scene script, but instead you create and assign a Data
object in the editor (to an exported property) that resource will be shared between all scene instances.
However if you enable resource_local_to_scene
flag for that resource, the engine will automatically duplicate it when instantiating the scene.
ok i will try to figure this out, i am struggling to visualize this. Maybe some coffee and a break will help it sink in.
Thanks for the heads up too with naming conventions. I actually do use a better naming scheme for everything, but just for example sake i typed that stuff.
In my global script how should i reference the created node? I was sending the "self", the getnode(self), getnodepath(self)... as a NodePath in my global script.
I would then try to say in process:
if Input.action....(Leftmouse):
var scene = <path of node i was sending>.data
scene.Name = globalname
.....and the above was not working.... so i think i was trying to access the property properly potentially (see what i did there )....
what i think i am doing wrong is referencing that specific node (created at run time) in the script.
seriously thanks for taking your time. i know we all got shtuff to do!
how do i load the:
<Resource#-9223372008132180722>
in the global script.....
i can get my node to reference that and send it. i think i already did.
so in the global i want to load that resource from reference at runtime, without declaring the variables in _ready()... because the number will change depending how many objects i instantiate or delete.... as in i wont know "S1" to "Sx) at run time/change with load data
or do i say something like:
var object = <Resource#-9223372008132180722>
object.Name = globalName
yes.... autoload. I figured i was safest to use that to hold the information and have a separate window to change data... with the singleton acting as the "middleman"
BTW... i have read you explaining instantiate stuff (maybe the above example???)... as well as many other posts. I really do try to source other places/archives best i can.
With that said...
You put some serious time into this site. Its highly impressive and very appreciated.
I know i say this a lot... but i feel it should be.... THANK YOU!
The object scene itself is setting a global variable when Input to its <id/path/self>.
In the autoload i have an export var for <path/id/self>.
I am trying to create a global func to set a value (data) in another scene (controller scene) that changes the values of the "data"
The global script have separate set of variables to be set to what the new_data.
If the player chooses to keep "new_data" it will save the changes to objects "data"...
If the player chooses to discard i wont have touched the original data.
I was thinking to have more types of controllers or objects eventually and or access them between scenes.
I think that is how i need to structure handling data that's created/edited at run time???
I want to push a button in game: #MAIN scene
create object(1)
push button;
create object(2) #just for example it may be other objects... they are added with button in game
click object(1)
load object DATA into "Menu" #Controller scene
change object(1) DATA via buttons/sliders in "Menu"
close the "Menu" saving the DATA(new) ....or... close the "Menu" keeping DATA(old)
switch scenes back to "MAIN" where the object(1) property has been updated to DATA(new) #MAIN scene again
I want to load data from object(1) into "controller scene".
Change the data in the "controller scene". SAVE or NOT
Switch to Main scene and keep the data.
I think i am not referencing the created node or its properties correctly in my code.
i can do all of this if i have the scenes in the scene tree and access them by their path. All that works for me, but when trying to do so "dynamically" in game (scene added to tree in game), i can't get the nodes reference to work in my global scene.
i will try to refine what i have thus far and hopefully... have something working (ish) to help show what i want to do. i KNOW everything i do will be able to be seriously improved.
you will totally understand what i'm going for when you see it. Its more than i should have done to learn UI, but its been a fun uphill lesson.
Game wise, you are the "player" and the data IS what they interact with
click button.
make object.
click object.
change several different types of data attributed to that object, color/size/labels/etc
have main screen show the related changes and save the changes/load the changes
sort the data based off the objects attributes when pressing button.
yes!
and i would like to change that data/save changes to data.
i've tried so many different ways now i'm so turned around.
- Edited
REVBENT A reference to the object should be sent to the global script by a signal. Declare a custom signal in your instantiated scene. Upon instantiation connect this signal to a signal handler in the global script using connect()
. When scene's button is clicked it should emit that custom signal. The reference to self can be sent as a signal argument.
xyz Thank you. I was trying to do this the correct way i suppose. that is the reference i was passing. Accessing the "references" properties isn't working for me.
do i declare a:
@export var scene = singnalPATH #singalPATH is self reference
scene.property = new_property
if so i am doing something wrong bc it always is telling me i don't have that property on a null instance, no matter how i reference self in the signal.
Seriously thanks for the time you always take to help me.... i feel like i owe you some cookies or beer etc.