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

utility function import #66

Open
ksjogo opened this issue Dec 20, 2021 · 4 comments
Open

utility function import #66

ksjogo opened this issue Dec 20, 2021 · 4 comments

Comments

@ksjogo
Copy link
Contributor

ksjogo commented Dec 20, 2021

Given a file util.ts with

export function map() {
  return 1;
}

and a file Actors.ts with

import { map } from "./util";

export default class Actor {
  something() {
    return map();
  }
}

The generated util.gd file will be empty and only have the comment:

# This file has been autogenerated by ts2gd. DO NOT EDIT!








The Actor.gd file will look like:


# This file has been autogenerated by ts2gd. DO NOT EDIT!



class_name Actor
    



var () => number = load("res://actors/util.gd")

func something():
  return map()
@adamuso
Copy link
Contributor

adamuso commented Dec 20, 2021

To clarify, this issue is related to #61 right? Because before that export default was never a thing in ts2gd :)

@ksjogo
Copy link
Contributor Author

ksjogo commented Dec 20, 2021

Ahh, yeah I tested it on that branch first, put it also happens on main (without the default).

@adamuso
Copy link
Contributor

adamuso commented Dec 21, 2021

I think in this case ts2gd should just show an error because in GDScript you cannot create a helper function without creating a class. The only option is to use static function in GDScript like in code below, but currently for some reason static in ts2gd is not supported (I think we should add support for it, added issue #69).

# A.gd

static func utilFunc():
  print("util")
# B.gd

extends Node

const A = preload("res://A.gd")

static func _ready():
  A.utilFunc()

@johnfn
Copy link
Owner

johnfn commented Dec 21, 2021

This is obviously an error today so we should probably emit an error for now, rather than just incorrect code. :)

But I disagree that this should be an error for all time. It's a very convenient pattern in TS to export and import top-level functions and variables, so if we can make it work somehow, I think we should try to.

One idea I had for this case was to gather up all top-level declarations into a new class and make that class an autoload. e.g.

util.ts:

export function map() {
  return 1;
}

generates UtilAutoload.gd

class_name
UtilAutoload

func map():
    return 1

Then map() would emit UtilAutoload.map()

I know this kinda goes against the principle of 'simplest possible emit' but it is such a handy thing to do in TS that I wouldn't want to disallow people from doing it.

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

3 participants