Jump to content

Recommended Posts

Abstract: This tutorial will help you configure your vs code and lua LSP to learn the components and c++ global variables and functions in DST. Together with the already available lua scripts, you will gain full access to any global variables or component variables.

Here are screenshots that demonstrate the facility. Before this you can only access the component variables that is used in your local function.

before.png.d65c793ea77687abcd327a516f7f20fa.png

With my setup, you are able to access full component variables with one extra line `---@param inst Instance`.after.png.8c10696c982eb4d69dd08817ed560964.png

 

Yet there is one remaining problem that, without manual annotation, it is impossible to guess the use of component variables and the type of params.

Edited by Rickzzs
  • Like 4
  • Thanks 1

Step 1 Install Lua Language Server Extension

Open VS Code, search "lua" and install the extension by sumneko.

Snipaste_2024-09-20_17-07-23.jpg.ad47155fc7460c8b057afc8bdf820ed5.jpg

Step 2 Add DST Scripts/ into Library

Extract Steam\steamapps\common\Don't Starve Together\data\databundles\scripts.zip  to your favorite work folder and purge its content.

The files I choose to remove, to name a few, are speeches, prefabs, translations, layouts and skins. The intention to remove them is to reduce the loading time and memory usage.

The removed files will be moved outside of the folder for manual lookup.

When you are done, add this folder as a library.

Snipaste_2024-09-20_17-10-00.jpg.06e82361aca38c0f69804212d79863fc.jpg

Step 3 Disable Diagnostics

Disable some type of code preferences that is not in DST's flavor. And also disable library dianostics.

Snipaste_2024-09-20_17-16-15.jpg.b40853c64d0bfa8fd0bf3c179c2bf324.jpg

Step 4 Add `scripts/` Require Path

DST will try to `require` a file not from MODROOT but from MODROOT/scripts/. We need to configure this.

Snipaste_2024-09-20_17-18-22.jpg.ae2a12f8781c1e51b21fa5896a4e2deb.jpg

Step 5 Add Mod Util Variables

Since we code in mod environment, things that are not in GLOBAL/_G environment must be added manually. But all postinit functions can be acquired from scripts/modutil.lua, so we don't need to add them yet.

Here is a small list of these variables. You can find them in scripts/mods.lua

GLOBAL=_G
env={}
MODROOT="./"
modname="modname"

create a new file in the aforesaid library folder, name it "modvar.lua" or something.

Step 6 Select Lua Version

In extension settings, select lua 5.1

Step 7 Rewrite tuning.lua

Open scripts/tuning.lua. The only thing to do is to remove the `function()` and `end` thing.

Look at the screenshot below, the function Tune is deleted, so that all TUNING values will be recognized by the extension. By the way I removed a few more lines above, but it doesn't matter. If you've done all right, you will be able to see the value when you input `TUNING.XXX`.

Snipaste_2024-09-20_17-29-58.jpg.46440ea05a0c3190a16bbd019ac9007c.jpg

See TUNING.AFFINITY_15_CALORIES_SUPERHUGE=1.1

Snipaste_2024-09-20_17-32-53.jpg.e0704db3decf746f7454484e08ee16f2.jpg

 

Edited by Rickzzs
  • Like 3

Step 8 Export C Variables

https://steamcommunity.com/sharedfiles/filedetails/?id=3332879511

Subscribe the mod and enable it. Open console and input

require("exportengine")()

Open DST data folder and cut engine.lua to your library folder.

Step 9 Configure EntityScript

1. Add `---@class EntityScript` before `EntityScript = Class(function(self, entity)`

2. Create a new file, namely instance.lua or something, in your library folder.

---@class components
components = {}

---@meta
---@return Instance
---@param name string?
---@override
function CreateEntity(name) end
---@class Instance: EntityScript
---@field lower_components_shadow table
---@field GUID number
---@field spawntime number
---@field persists boolean
---@field inlimbo boolean
---@field name string
---@field data table
---@field listeners table
---@field updatecomponents table
---@field updatestaticcomponents table
---@field actioncomponents table
---@field inherentactions table
---@field inherentsceneaction table
---@field inherentscenealtaction table
---@field event_listeners table
---@field event_listening table
---@field worldstatewatching table
---@field pendingtasks table
---@field children table
---@field platformfollowers table
---@field actionreplica table
Instance = {
  entity = Entity,
  SoundEmitter = SoundEmitter,
  RoadManager = RoadManager,
  MiniMap = MiniMap,
  MiniMapEntity = MiniMapEntity,
  Follower = Follower,
  AnimState = AnimState,
  VideoWidget = VideoWidget,
  ShardClient = ShardClient,
  EnvelopeManager = EnvelopeManager,
  Label = Label,
  Physics = Physics,
  PhysicsWaker = PhysicsWaker,
  Light = Light,
  TextEditWidget = TextEditWidget,
  Network = Network,
  TextWidget = TextWidget,
  PostProcessor = PostProcessor,
  WaveComponent = WaveComponent,
  GroundCreep = GroundCreep,
  GroundCreepEntity = GroundCreepEntity,
  MapGenSim = MapGenSim,
  Transform = Transform,
  MapExplorer = MapExplorer,
  DebugRender = DebugRender,
  SoundEmitter = SoundEmitter,
  FontManager = FontManager,
  DynamicShadow = DynamicShadow,
  StaticShadow = StaticShadow,
  ClientSleepable = ClientSleepable,
  UITransform = UITransform,
  Image = Image,
  Pathfinder = Pathfinder,
  ShardNetwork = ShardNetwork,
  ShadowManager = ShadowManager,
  Map = Map,
  AccountManager = AccountManager,
  ParticleEmitter = ParticleEmitter,
  ImageWidget = ImageWidget,
  ServerNonSleepable = ServerNonSleepable,
  LightWatcher = LightWatcher,
  TwitchOptions = TwitchOptions,
  GraphicsOptions = GraphicsOptions,
  MapLayerManager = MapLayerManager,
  VFXEffect = VFXEffect,
}
---@type components
Instance.components = components
Instance.replica = components

3. The most important thing, whenever you write a function(inst)end, you need to prepend an annotation

---@param inst Instance

Step 10 Export Components

Use the following python script.

exportLuaComponentDefs.py

This will give you a file named component_def.lua, also put it into library folder.

If you are all successful, you will get access to component variables now. Check if you are done with a simple line of `inst.components.inspectable.`

Snipaste_2024-09-20_20-48-16.jpg.25e3e854fb0d52a8ae365904aedf2c40.jpg

Step 11 Export Modutils

The last thing of this tutorial is to export scripts/modutils.lua, where there are some postinit functions. Similar to components, I provide another python script to do this.

exportLuaModutilDefs.py

 

Conclusion

This tutorial is a setup for full access to DST's c++ and component variables using VS Code and lua LSP. However, it still lacks accurate annotation, which cannot be acquired with machine algorithm.

Edited by Rickzzs
  • Like 3

很不错的帖子,晚点尝试弄一下,看python代码楼主是中国人吧,请问有中文文档的出处嘛

Great post! I’ll try it out later. Judging by the Python code, the original poster seems to be Chinese, right? Is there a source for Chinese documentation?

On 11/22/2024 at 6:32 PM, Gyde_CT said:

很不错的帖子,晚点尝试弄一下,看python代码楼主是中国人吧,请问有中文文档的出处嘛

Great post! I’ll try it out later. Judging by the Python code, the original poster seems to be Chinese, right? Is there a source for Chinese documentation?

机翻吧,我没兴趣维护多语言(),写代码的肯定懂英语

On 9/20/2024 at 3:26 PM, Rickzzs said:

Step 8 Export C Variables

https://steamcommunity.com/sharedfiles/filedetails/?id=3332879511

Subscribe the mod and enable it. Open console and input

require("exportengine")()

Open DST data folder and cut engine.lua to your library folder.

 

When I input the command into the console, it says in the log "error: failed to open engine.lua"

22 hours ago, DrunkProtos said:

When I input the command into the console, it says in the log "error: failed to open engine.lua"

Then io.open is broken or this file is occupied by another program. Try open the exportengine script and replace io.open(x)/x.write() thing with TheSim:SetPersistentString(filename,str)

This will give you a file in save folder Klei/DoNotStarveTogether/xxxxxx/client/filename

3 hours ago, Rickzzs said:

Then io.open is broken or this file is occupied by another program. Try open the exportengine script and replace io.open(x)/x.write() thing with TheSim:SetPersistentString(filename,str)

This will give you a file in save folder Klei/DoNotStarveTogether/xxxxxx/client/filename

Thank you; it worked flawlessly, and just for future reference, I will write more detailed steps for fixing this issue.

Go to the mod directory; it's either in 'steamapps\common\Don't Starve Together\mods' or 'steamapps\workshop\content\322330'. Next, go into the scripts folder, open 'exportengine.lua' with a code/text editor, scroll to the bottom where you will find this piece of code:

return function()
  local f = io.open("engine.lua", "w")
  if not f then
    print("error: failed to open engine.lua")
    return
  end
  f:write(PrettyPrint(DirectCFunctions))
  f:write(PrettyPrint(CMetatables))
  f:close()
end

And replace the function with this:

return function()
	TheSim:SetPersistentString("engine.lua", PrettyPrint(DirectCFunctions) .. PrettyPrint(CMetatables))
end

Save, start/restart the game, go into a world or create a new one, input the command stated previously:

require("exportengine")()

Remember to input it in local and not remote, and finally, go to 'Klei\DoNotStarveTogether\474722705\client_save', which is usually located in the Documents folder. You will find your 'engine.lua' there.

  • Big Ups 1

Following this I couldn't get step 7 to work.

If I understand correctly, you are deleting the function declaration, its corresponding end, and the call to the function we deleted at the end of the file `Tune()`.

Edit: Looks like Klei forum shrank the screenshots to unreadable. See links

https://imgur.com/a/y0ewzgv

https://imgur.com/a/2sezDZb

 

image.png

image.png

image.png.95194a6c07712667d14f7c28ef4d414e.png

image.png.067bccbe80fce9cc4ec7d526dbe11768.png

 

Edit - Solved. Keeping everything for prosperity. LUA extension didn't install properly the first time. Reinstalling it wiped all settings. I had to run through the steps from the beginning after the extension installed was successful.

Edited by Dallen

In order to help my friend @liolok, I have simply optimized the generation of annotations here. If you are interested, you can give it a try.

https://github.com/WH-2099/dst-server/blob/main/gen_annotations.py

Edited by WH-2099
  • Like 1

Thanks for @Rickzzs and @WH-2099!

Still have a few questions:

1. In step 2, what files exactly should be removed? Or should we reserve some of the files only?

2. Step 8, the mod seems broken since Klei changed write permission, is there any better way to get engine.lua?

Edited by liolok
1 hour ago, liolok said:

Thanks for @Rickzzs and @WH-2099!

Still have a few questions:

1. In step 2, what files exactly should be removed? Or should we reserve some of the files only?

2. Step 8, the mod seems broken since Klei changed write permission, is there any better way to get engine.lua?

1. It's your freedom. My pc have limited mem. Just to make vs code respond quickly. Apparently there are some junks and some of little interest (for example skins)

2. Well You can edit the export lua file, save it to unsafedata/engine.lua It is not a barrier as long as you follow Klei's demand.

Edited by Rickzzs
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...