Rickzzs Posted September 20, 2024 Share Posted September 20, 2024 (edited) 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. With my setup, you are able to access full component variables with one extra line `---@param inst Instance`. 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 September 20, 2024 by Rickzzs 4 1 Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/ Share on other sites More sharing options...
Rickzzs Posted September 20, 2024 Author Share Posted September 20, 2024 (edited) Step 1 Install Lua Language Server Extension Open VS Code, search "lua" and install the extension by sumneko. 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. Step 3 Disable Diagnostics Disable some type of code preferences that is not in DST's flavor. And also disable library dianostics. 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. 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`. See TUNING.AFFINITY_15_CALORIES_SUPERHUGE=1.1 Edited September 20, 2024 by Rickzzs 3 Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1749587 Share on other sites More sharing options...
Rickzzs Posted September 20, 2024 Author Share Posted September 20, 2024 (edited) 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.` 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 August 2, 2025 by Rickzzs 3 Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1749631 Share on other sites More sharing options...
Cctt Posted November 22, 2024 Share Posted November 22, 2024 很不错的帖子,晚点尝试弄一下,看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? Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1758689 Share on other sites More sharing options...
Rickzzs Posted December 2, 2024 Author Share Posted December 2, 2024 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? 机翻吧,我没兴趣维护多语言(),写代码的肯定懂英语 Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1766999 Share on other sites More sharing options...
Anomen77 Posted December 18, 2024 Share Posted December 18, 2024 Thanks for the guide! It was easy to follow and it's going save me a ton of time digging through the files. Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1780516 Share on other sites More sharing options...
DrunkProtos Posted January 27, 2025 Share Posted January 27, 2025 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" Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1790069 Share on other sites More sharing options...
Rickzzs Posted January 28, 2025 Author Share Posted January 28, 2025 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 Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1790496 Share on other sites More sharing options...
DrunkProtos Posted January 28, 2025 Share Posted January 28, 2025 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. 1 Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1790603 Share on other sites More sharing options...
Dallen Posted February 4, 2025 Share Posted February 4, 2025 (edited) 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 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 February 4, 2025 by Dallen Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1792876 Share on other sites More sharing options...
WH-2099 Posted June 30, 2025 Share Posted June 30, 2025 (edited) 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 July 4, 2025 by WH-2099 1 Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1824992 Share on other sites More sharing options...
liolok Posted July 1, 2025 Share Posted July 1, 2025 (edited) 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 July 1, 2025 by liolok Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1825102 Share on other sites More sharing options...
Rickzzs Posted July 1, 2025 Author Share Posted July 1, 2025 (edited) 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 July 1, 2025 by Rickzzs 1 Link to comment https://forums.kleientertainment.com/forums/topic/159850-tutorial-vs-code-lua-extension-with-dst-setup/#findComment-1825126 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now