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

Generate .gdns files? #73

Open
arunvickram opened this issue Nov 15, 2020 · 9 comments
Open

Generate .gdns files? #73

arunvickram opened this issue Nov 15, 2020 · 9 comments

Comments

@arunvickram
Copy link
Contributor

Hi, I just started playing around with the Godot Nim stub project, and I was wondering if there was a way to autogenerate the GDNative script files per compilation?

Right now I'm just copying the info from FPSCounter.gdns and tweaking the variables to fit the class file that I compile, but if I have to manually do this every time, it doesn't seem too scalable.

Looking forward to hearing from you, and so far nice work!

@geekrelief
Copy link
Contributor

Sure why not? A gdns file is just text. Though you might have to shut down the editor to reload it. I'm pretty sure the editor only reloads the dll. What variables are you tweaking?

@arunvickram
Copy link
Contributor Author

So, if I have a MainPanel.gdns with the following content:

[gd_resource type="NativeScript" load_steps=2 format=2]

[ext_resource path="res://nimlib.gdnlib" type="GDNativeLibrary" id=1]

[resource]

resource_name = "MainPanel"
library = ExtResource( 1 )
class_name = "MainPanel"

I end up changing class_name and resource_name, but I don't want to have to do that every time I create a new class.

@geekrelief
Copy link
Contributor

So write a script to generate the gdns. That's what I did.

@arunvickram
Copy link
Contributor Author

arunvickram commented Nov 16, 2020

Right, I'm going to do that, but I figure it would be nice if the godot-nim project itself included that build script anyways as a quality-of-life improvement

@geekrelief
Copy link
Contributor

Yeah I agree. :) Though I think the this would be more of a change to godot-nim-stub. You could modify the nakefile to add your own task to generate a nim, gdns, and tscn file.

@arunvickram
Copy link
Contributor Author

So, quick update, this is what I ended up adding to my nakefile.nim:

proc genScripts() =
  for srcFile in walkFiles("src/source/**/*.nim"):
    let (_, fileName) = splitPath(srcFile)
    let file = open(srcFile)
    defer: file.close()

    let classNames = collect(newSeq):
      for line in file.lines:
        if line =~ re"gdobj\s*(\S+)\s*": matches[0]

    createDir("testScripts")

    for className in classNames:
      let scriptFile = open(fmt"scripts/{className}.gdns", mode = fmWrite)
      defer: scriptFile.close()

      scriptFile.writeLine("[gd_resource type=\"NativeScript\" load_steps=2 format=2]")
      scriptFile.writeLine("")
      scriptFile.writeLine("[ext_resource path=\"res://nimlib.gdnlib\" type=\"GDNativeLibrary\" id=1]")
      scriptFile.writeLine("")
      scriptFile.writeLine("[resource]")
      scriptFile.writeLine("")
      scriptFile.writeLine(&"resource_name = \"{className}\"")
      scriptFile.writeLine("library = ExtResource( 1 )")
      scriptFile.writeLine(&"class_name = \"{className}\"")

This requires that the src directory in the nimble project has an extra source directory with all the code so as to not process the generated Godot API for Nim

@geekrelief
Copy link
Contributor

You can also use this: https://nim-lang.github.io/Nim/strutils.html#%25%2Cstring%2CopenArray%5Bstring%5D instead of writing line by line.

@arunvickram
Copy link
Contributor Author

arunvickram commented Nov 23, 2020

The reason I wrote it line by line was to avoid adding tabs/spaces at the beginning of the file, but I'll take a look at it.

@geekrelief
Copy link
Contributor

Spaces shouldn't be a problem. I do this in my code.

const gdns_template = """
[gd_resource type="NativeScript" load_steps=2 format=2]

[sub_resource type="GDNativeLibrary" id=1]
entry/Windows.64 = "res://$3/$1.dll"
dependency/Windows.64 = [  ]

[resource]
resource_name = "$2"
class_name = "$2"
library = SubResource( 1 )
"""

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

2 participants