Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make runtime loading compatible with threads ? #41

Open
Lertsenem opened this issue Mar 19, 2024 · 1 comment
Open

Make runtime loading compatible with threads ? #41

Lertsenem opened this issue Mar 19, 2024 · 1 comment

Comments

@Lertsenem
Copy link
Contributor

Hi ! Maybe this is actually not an issue, but I have not been able to figure a solution yet.

So I'm trying to build a very (very) big map for the player to play in. To avoid gulping too much memory (and also to avoid in-editor freezes when opening Godot ^^"), I'm dividing the map in more manageable chunks of 300×300 tiles. In tiled I can then use the world feature to stitch those chunks together.

In Godot I built a Node which is able to read info from the world file, and load neighboring chunks when needed, using YATI runtime importer. It works, but with a minor issue : the import operation takes quite some time and freezes the main thread for about 2 seconds when loading a new chunk, which is very annoying from the player perspective.

To solve this issue, I tried using Threads thusly :

var t = Thread.new()
var c = load_map.bind(m)
t.start(c)

with load_map being :

func load_map(filename):
    var map_scn = Importer.import("res://maps/%s" % filename)
    map_scn.name     = filename
    map_scn.position = Vector2(map["x"], map["y"])
    maps_parent.add_child.call_deferred(map_scn.duplicate())

Unfortunately this approach does not work, and the entire game freezes with error such as :

E 0:00:22:0692   TilemapCreator.gd:273 @ handle_layer(): This function in this node (TileMap) can only be accessed from either the main thread or a thread group. Use call_deferred() instead.
E 0:00:22:0707   TilemapCreator.gd:598 @ create_map_from_data(): This function in this node (level) can only be accessed from either the main thread or a thread group. Use call_deferred() instead.
E 0:00:22:0707   TilemapCreator.gd:486 @ create_polygons_on_alternative_tiles(): This function in this node (level) can only be accessed from either the main thread or a thread group. Use call_deferred() instead.

(of course I tried using call_deferred() as suggested, to no avail).

Now my understanding is that these errors mean the runtime import is not Thread safe, and so I should not use it with Threads. But now I'm all out of ideas on how to tackle my problem, which is why I'm coming here. :]

  • Is there a way to make the runtime import Thread-safe?
  • Is there another preferred solution I'm not seeing for this kind of issue?
@Kiamo2
Copy link
Owner

Kiamo2 commented Mar 19, 2024

  • Is there a way to make the runtime import Thread-safe?

Unfortunately that's beyond my restricted Godot knowledge (others to the rescue...?)

  • Is there another preferred solution I'm not seeing for this kind of issue?

Same ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants