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 Godot instantiable so it can run multiple projects within a single process #8151

Open
migueldeicaza opened this issue Oct 16, 2023 · 4 comments

Comments

@migueldeicaza
Copy link

migueldeicaza commented Oct 16, 2023

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:

@Calinou Calinou changed the title Making Godot Instantiable Make Godot instantiable so it can run multiple projects within a single process Oct 16, 2023
@m4gr3d
Copy link

m4gr3d commented Oct 16, 2023

@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:

  • On Android, graphics rendering is done in a Surface object
  • The current Android platform implementation defines a single Surface that's used by the engine for rendering
  • Following the refactor, the Android platform implementation would define multiple Surface instances, one for each Window reported by the engine (so long as the Window is not embedded)
  • This should allow developers to power multiple GodotView through a single Godot project, which may facilitate development and maintenance (in my subjective opinion)

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
Copy link

hannojg commented Oct 19, 2023

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
Copy link

m4gr3d commented Oct 19, 2023

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
Copy link

hannojg commented Oct 19, 2023

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

  • You can't use multiple instances/views
  • You can't start a instance with different games / project

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

4 participants