godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Make Godot instantiable so it can run multiple projects within a single process #8151

Open migueldeicaza opened 1 year ago

migueldeicaza commented 1 year ago

Describe the project you are working on

I work on the Swift bindings for Godot (SwiftGodot) and many of my users would like to use Godot and SwiftGodot in Apple platforms not just for running a full game, but to spice up existing apps with Godot content - and not just one bit of content, but independently developed parts of content.

I imagine that this is not limited to iOS, it just happens to be an active platform where this is quite common, but I imagine Windows, Linux and other people might want similar functionality

Describe the problem or limitation you are having in your project

Godot currently has a number of globals and singletons and is designed for the whole process to be at the service of one instance of a project (or, the editor, or the project manager).

What I would like is to wrap the entirety of Godot into a class that can be initialized multiple times with different project.godot to execute, and to host the resulting view in different places. So Godot could be treated like an image viewer, just like you can add SVG images to your UI, you could add a Godot View:

For example, this should be possible on iOS, which embeds three different instances of Godot, each with their own top-level project.godot, and when the user taps on one of them, it transitions to a full-screen version of the game:

VStack {
    Text ("View of the World")
    GodotView (projectPath: "Hud/project.godot")
        .padding (10)
    Text ("Game:")
    NavigationLink { 
        GodotView (projectPath: "game/project.godot", args: ["preview=true"])
    } destination: {
        // Run the full game
        GodotView (projectPath: "game/project.godot")
    }
    GodotView (projectPath: "animations/project.godot")

}

Describe the feature / enhancement and how it helps to overcome the problem or limitation

All the singletons in Godot and global variables would have to be part of a "GodotInstance" class.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

The good news is that most globals in Godot are already singletons and are being accessed with a Class::singleton pattern, which helps as a good flag of what needs to be instance-ified.

All the singletons would be moved into a GodotInstance class, and this class would need to be reachable from every point that is currently using the singletons.

There will be some plumbing required to pass the context/closure in places that currently lack it, so every place that currently relies on a Class::singleton would instead have a GodotInstance class

If this enhancement will not be used often, can it be worked around with a few lines of script?

No, this scenario needs some changes to the Godot Engine.

Is there a reason why this should be core and not an add-on in the asset library?

Godot's current structure is such that the instance is managed by the operating system, as a process.

This would allow multiple godot instances in one process, each with their own settings.

Other uses

Scenarios that could benefit from this have been discussed in the past:

m4gr3d commented 1 year ago

@migueldeicaza I've implemented similar support for using Godot as a view on Android, and running into similar limitations due to the fact that only one Godot instance can run per process.

One workaround I have considered (and working to prototype in Godot 4.3) is to leverage Godot 4.x multi-window support to achieve the scenario you described within the same process. The difference with your proposal would be that each GodotView would instead be backed by a Godot Window instead of being backed by a full instance of Godot.

I'm not familiar with iOS, so here's a high level overview of how this would work on Android:

Don't get me wrong, there's still value in being able to instantiate multiple Godot instances in a single process, and that should also help for code encapsulation, maintenance and testing; but this may not be a hard blocker for the scenario you're describing.

hannojg commented 1 year ago

I'd love to have that change (sorry for the spam, but I'd be so hyped about that change. That way we could use Godot in mobile applications to enhance the user experience so much!)

m4gr3d commented 1 year ago

I'd love to have that change (sorry for the spam, but I'd be so hyped about that change. That way we could use Godot in mobile applications to enhance the user experience so much!)

@hannojg Even with the current limitations in place, Godot is already useable in mobile applications. See https://docs.godotengine.org/en/latest/tutorials/platform/android/android_library.html for reference.

This is the approach used by the Tesla Android and iOS mobile apps.

hannojg commented 1 year ago

Yes, I know, however it's too limited. I'd need to use it in a similar manner as @migueldeicaza . So some limitations are: