Godot Version
Godot 4.3 dev 6
Question
Hello everyone Many of you probably know such a node as CodeEdit. And I wondered how I could use this node to compile the code, at least in GDScript. I want to influence objects with this CodeEdit
Godot 4.3 dev 6
Hello everyone Many of you probably know such a node as CodeEdit. And I wondered how I could use this node to compile the code, at least in GDScript. I want to influence objects with this CodeEdit
Hi there and welcome to the forum!
There are two options:
Expression
allows you to easily evaluate simple things at runtime. It is a bit limited in what it can do, but simple to use. Plus you can easily define what the expression can access, so you can limit what people can do (potentially avoiding crashing the game)GDScript
class can also be generated dynamically by creating a new instance and then assigning to the source_code
property. This way you have full access to the scripting layer. However this comes with the risk that users may access things they shouldn’t.If you need examples for either of those approaches, feel free to say so! For now I’ve not included examples as I don’t know which route you want to go.
Thanks for the answer, but I would like to limit users from entering where they don’t need to. Would it be difficult for you to tell us a little more about the first method?
Expressions are basically simple statements.
A few things they can not do:
=
symbol within expressions)And a some things they can do:
node.set_position(Vector2(10, 0))
Here is a very basic example that sets the position of one node to the same as another node:
var input_names := ["foo", "bar"]
var input_values := [$Foo, $Bar] # just two random nodes
var your_expression := "foo.set_position(bar.position)"
var expr := Expression.new()
var error := expr.parse(your_expression, input_names)
var result := e.execute(input_values))
You probably want to check for errors before executing, but this example should get the idea accross.
There is also an example in the docs:
Thanks you much