0 votes

I have been researching on how threads work for a few days but I can't seem to find a solution.
So I have a simple scene tree:
- TileMap

and I have a script on the TileMap node:

extends TileMap

var thread = Thread.new()

func _ready():
    thread.start(self, "generate_tiles")

func generate_tiles(data):
    for y in range(100):
        for x in range(100):
            set_cell(x,y,0) #btw the tileset[0] is a valid tileset I made

But why is it that when I play the project, it crashes?
When replace set_cell(x,y,0) with print(x,y), it works perfectly fine.
Is there something about threads that I should know? (Sry I'm a little new to godot threads so I don't understand exactly how they work)

Thanks :)

in Engine by (12 points)

1 Answer

+5 votes

Because anything that touches nodes that are inside the scene tree is not thread-safe. You could be setting tiles while the TileMap is ongoing processing stuff (_process, _draw...), and your calls makes it clash.

You may want to generate a 2D array in a thread (with generation logic if you do), and apply it on the main thread.
Or, maybe you could try not adding the tilemap to the scene tree and only add it when it's done generating, but I am not sure about this one.

by (27,897 points)

Oh I didn't know about that! Thanks for the help I'll try that out. I really appreciate it! :)

Because anything that touches nodes that are inside the scene tree is not thread-safe.

Thanks a ton for this clarification. It should be in the docs for Thread.

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.