+2 votes

I have a script's source that I will need to extract variables from. I have to do it in this way due to an implementation detail. Here's what I did:

var script = GDScript.new()
script.set_source_code('var test_var = "hi this is working"') 
var my_object = Node.new()
var my_object.set_script(script)

Then I do:

print(my_object.test_var) #I get an error! Doesn't work :(

Why doesn't it work? What am I missing here? I tried using Reference.new() but still doesn't work!

in Engine by (45 points)

I can propose an alternative.
Load script like node.

1 Answer

+1 vote
Best answer

Reload the script after setting it, and remove the second 'var' declaration on my_object:

var script = GDScript.new()
script.set_source_code('var test_var = "hi this is working"')
script.reload()
var my_object = Node.new()
my_object.set_script(script)
print(my_object.test_var)
by (1,310 points)
selected by
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.