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

Loading heavy resources on demand #30933

Open
dioptryk opened this issue Jul 29, 2019 · 3 comments
Open

Loading heavy resources on demand #30933

dioptryk opened this issue Jul 29, 2019 · 3 comments

Comments

@dioptryk
Copy link
Contributor

dioptryk commented Jul 29, 2019

Godot version:
3.1.1 stable

OS/device including version:
All

Issue description:
I seek the ability to not have all "heavy" resources (like models, textures etc.) loaded immediately upon loading the scene, but on demand, if necessary. Please hear my reasoning before ridiculing me :)

I'm having much fun with Godot since 2.0, experimenting and trying different things, and I finally decided to write a 3d dungeon crawler from first-person perspective, with randomly generated levels. I already have a working PoC and it works very nice. My problem lies with the randomness in relation to the objects to fill level with. Let's say I want to generate a "cave" level. Since caves are all about spiders and such, I'd like to only fill the level with "spider" monsters. Now, I have several types of spiders, each one in a different .tscn, with a specific script, sounds, models, etc. Now, how to actually find all these scenes and choose from them to place a monster?

Attempt 1: Master scene containing all the monsters and a script to get_monster_type. Nice idea, but guess what, all the resources will be loaded anyway with the scene even if I only want a single spider type. Can you see where I'm going with this?

Attempt 2: A custom node tree containing string paths to .tscn files and some metadata, for example biomes the monster appears in etc. Works, but you need to synchronize your library of monsters and the "database", so it's easy to forget something (or specify wrong path). Besides that, you will have redundancy (the database and the monster will definitely share some metadata, so you need to watch out for differences). And you lose self-containment of a monster .tscn, which I just love (a monster is a complete component, that will simply work if placed under a parent that supports it, like a level).

Attempt 3: Editor plugin to scan all monster scenes before running/deploying the game and producing metadata library with all the necessary info about monsters into a .json file. Pretty nice, but still something may break, and besides that, the editor must load everything to scan the .tscn. Besides that, it's still a workaround.

We have this beautiful (I really mean it) editor, which makes it easy to have a game object completely self-contained in a .tscn and I really don't like to being actually forced to make workarounds around it. I was also pretty surprised, that loading PackedScene actually loads everything inside it, even before instantiating it; a lightweight PackedScene would be enough for me (my "database" node tree would reference the monsters, so it could become Autoload without loading all monster resources).

Please comment on my plight, perhaps some of you had similar experience or a better idea?

@Calinou
Copy link
Member

Calinou commented Jul 29, 2019

I was also pretty surprised, that loading PackedScene actually loads everything inside it, even before instantiating it; a lightweight PackedScene would be enough for me (my "database" node tree would reference the monsters, so it could become Autoload without loading all monster resources).

You can enable Load as Placeholder on specific instanced subscenes by right-clicking them in the Scene Tree dock:

image

What this will do is turn the instance into an InstancePlaceholder instead of loading the full scene immediately. You can then choose when to actually load the full scene.

Also, support for texture streaming is planned in a future release (likely 4.0 or later).

@dioptryk
Copy link
Contributor Author

@Calinou This doesn't work in my case, because I need some data from the object and you can't read any property from InstancePlaceholder (I tried it yesterday). For example: you have a Spider.tscn with Monster.gd script attached, and this script has Biomes property, which says where the monster can spawn. You can't read this script property if the scene is a placeholder. You can indeed read it from the dictionary in PackedScene, but then all the data will be loaded, including textures.

@Calinou
Copy link
Member

Calinou commented Mar 8, 2020

I believe this should be addressed by #36640 as long as you load the resource using a single thread.

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

No branches or pull requests

3 participants