How can I precompile KTS scripts?
# scripting
s
How can I precompile KTS scripts?
My scripts take 3 seconds to compile and 50ms to execute, so I definitely need a way to cache that..
Copy code
Compiled gameplay_effects.udea.kts in 2972ms
Evaluated gameplay_effects.udea.kts in 92ms
[GameAssetLoader] Loaded asset: /ability/gameplay_effects.udea.kts
Compiled npc_melee.udea.kts in 362ms
Evaluated npc_melee.udea.kts in 54ms
[GameAssetLoader] Loaded asset: /ability/npc_melee.udea.kts
Compiled soldier_abilities.udea.kts in 222ms
Evaluated soldier_abilities.udea.kts in 29ms
[GameAssetLoader] Loaded asset: /ability/soldier_abilities.udea.kts
Compiled arrow.udea.kts in 496ms
Evaluated arrow.udea.kts in 31ms
[GameAssetLoader] Loaded asset: /blueprint/arrow.udea.kts
Compiled orc.udea.kts in 404ms
Evaluated orc.udea.kts in 57ms
@Vladislav Koshkin @ilya.chernikov Is this possible?
i
You can check caching mechanisms, e.g., code + test
👀 1
s
Perfect ! my game load time went from 10 seconds to under 1 second
@ilya.chernikov What do you do when a compiled script is no longer compatible with the main programs binary?
The source is compatible, but I changed the source codes now I get
java.lang.NoSuchMethodError
i
The example caching is very primitive; it uses
compiledScriptUniqueName
to calculate a unique name based on the script content. If you want something more advanced, you can implement
CompiledJvmScriptsCache
yourself. The basic implementation is here
s
I don't know if modifying that would do it because I'd need to essentially evaluate the compiled script to know if it needed recompiling
i
An easy way would be to invalidate the cache by deleting the
cacheBaseDir
, or have some more clever way to handle it via the custom "script to cache file" conversion.
If it doesn't work either, you probably need some custom host code.