How can I run "meta" code in the editor?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Diet Estus

I want to do some of my level generation automatically, so that I don’t have to manually position my nodes in my level scenes when I’m designing levels in the editor.

So, for example, suppose I have my Level.tscn open in the editor and I want to run a script which instances my Wall.tscn many times and places the instances at regular intervals.

Here’s an example of the script:

var wall_scene = load("res://Wall.tscn")
for n in range(100):
    var wall = wall_scene.instance()
    get_node("Level").add_child(wall)
    wall.global_position.y = 0
    wall.global_position.x = n * 64

Suppose I then wanted to save the level as a scene, with the walls now as children.

I know I can run this script in the _ready() function of my Level, but I don’t want to run it on the instancing of my level. I want to run it so that the Walls appear in the editor.

How can I do this kind of meta scripting?

If you make your code a tool, then change the owner of your wall nodes to the root node of Level, it will show up in the editor tree. Of course every time you run it, you might get duplicates of walls.

SIsilicon | 2018-10-09 11:36