exaV / screeps-kotlin-starter

A starting point for a Screeps AI written in Kotlin
MIT License
46 stars 63 forks source link

How to add other JavaScript libraries, such as Traveler? #17

Closed Zomis closed 3 weeks ago

Zomis commented 4 weeks ago

Thanks for your great work with making it easy to get started with Screeps in my favorite language!

I have already spent (read: wasted) some time on Screeps with JavaScript, during which I added the Traveler Screeps library.

I'm struggling though with importing this into a Kotlin Screeps project.

I am getting errors such as:

Module not found: Error: Can't resolve 'Traveler' in 'C:\Users\Simon\Desktop\screeps\screeps-kotlin-starter\build\js\packages\screeps-kotlin-starter\kotlin'

When trying to add @file:JsModule("Traveler") and @file:JsNonModule to the top of a separate file.

exaV commented 4 weeks ago

Thanks for the kind words.

I actually did integrate Traveler into a bot before, you can find the code here: https://github.com/exaV/screeps-kotlin The setup may be a bit outdated but it should be mostly the same today.

Zomis commented 4 weeks ago

@exaV Thanks for the example, I did take a look at it and copied what I deemed were the relevant files and sections but it doesn't seem to work for me. I still get the "Module not found" error above.

exaV commented 4 weeks ago

are you uploading the module with the deploy task? it should upload a separate file. At least that's how I did it back then. I think nowadays you should be able to add is as an npm dependency in gradle and then bundle it with webpack into the same file.

Zomis commented 3 weeks ago

Thanks, I managed to figure it out!

I added Traveler.js and the following package.json to a folder, e.g. <project>/ext/traveler

package.json:

{
  "name": "traveler",
  "version": "1.0.0",
  "description": "",
  "main": "Traveler.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Then in build.gradle.kts I added this:

    sourceSets {
        jsMain {
            dependencies {
                ...
                val pathToLocalNpmModule = rootProject.projectDir.resolve("ext/traveler").canonicalPath
                implementation(npm("traveler", "file:$pathToLocalNpmModule"))
            }
            ...
        }
        ...
    }

Then I also copied the relevant parts about Traveler from your other project.