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

Global pollution #187

Open
Hadaward opened this issue Jan 11, 2024 · 3 comments
Open

Global pollution #187

Hadaward opened this issue Jan 11, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@Hadaward
Copy link

I personally don't really like global pollution, you could take advantage of the javascript module system to export godot methods, utilities and classes without needing to register the godot namespace into the global. If you are wondering how this would work, it is quite simple, the first thing needed would be a d.ts documentation file for intellisense to work. And then we can consume the godot api using import syntax:

import { Node2D, print } from "godot";

export class Main extends Node2D {
  _ready() {
    print("Hi from Javascript Main class");
  }
}

This is literally the only thing I dislike about this api, the godot api is simply thrown into the global scope.

@Hadaward Hadaward changed the title Global poluttion Global pollution Jan 11, 2024
@snatvb
Copy link

snatvb commented Jun 7, 2024

I think this is limited engine - C++ <-> JS, you can bind data into JS from C++ only in global object. Actually another languages with runtime has similar behaviour

@Hadaward
Copy link
Author

Hadaward commented Jul 5, 2024

I think this is limited engine - C++ <-> JS, you can bind data into JS from C++ only in global object. Actually another languages with runtime has similar behaviour

Hmm, I don't know what to say about this because I've already found libraries in C#, for example, that allowed running javascript engines and exporting objects as modules to be imported. So i dunno if there's a lib like that in C++

@nmerget nmerget added the enhancement New feature or request label Sep 15, 2024
@nmerget
Copy link
Collaborator

nmerget commented Sep 17, 2024

@Hadaward at the moment godot is registered in the global context in this file.

I'm not comfortable enough to do some changes at this point and I'm not sure if we can build an own object somehow.

I personally don't like global pollution, as well. But there is some workaround for TS.

You could create a new file godot.ts where you wrap everything like this:

 // @ts-nocheck
export class Node extends godot.Node {}

export const DEBUG_ENABLED: boolean = godot.DEBUG_ENABLED;

export const register_class = (target: godot.GodotClass, name: string) =>
  godot.register_class(target, name);

...

You need to map everything you need then you are able to use it as module for now.
We should generate the godot.ts file via editor as well in the future as an alternative for godot.d.ts.

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

No branches or pull requests

3 participants