JohnWatson Posted June 21, 2017 Share Posted June 21, 2017 (edited) For example, I want a component (attached to TheWorld) to modify a table in another lua file whenever the game saves. So before saving, the file would look like this: Nabletame = { ["KU_1234567"] = { stream = "Username", dumber = 0, bowl = true, }, } Then after saving, it would magically modify the file to this: Nabletame = { ["KU_1234567"] = { stream = "Username", dumber = 7, bowl = false, }, ["KU_ABCDEFG"] = { stream = "Nameuser", dumber = 0, bowl = true, }, ["KU_EFGH123"] = { stream = "Unsaemre", dumber = 265603234, bowl = false, }, } Is that something that's even possible to do? I already know how to save and load data, I just want it to be shown visually in a file. Edited June 21, 2017 by JohnWatson Link to comment Share on other sites More sharing options...
alainmcd Posted June 21, 2017 Share Posted June 21, 2017 local ofilename = "myfirsttext.txt" local ofile = io.open(ofilename, "w") ofile:write("texty text") ofile:close() From within modmain.lua, that's GLOBAL.io.open, the rest remains the same. By default, it writes to the data folder of the DST installation. See section 5.1 of the Lua Reference Manual and chapter 21 of Programming in Lua. For example, create this console command and place it in customcommands.lua in the DST settings folder: function c_ListOfSkins() local ofilename = "Skins.txt" local ofile = io.open(ofilename, "w") for k, v in pairs(STRINGS.SKIN_NAMES) do if (k:sub(-8) ~= "_builder") then -- we don't want critter builders ofile:write(v.." ("..k..")\n") end end ofile:close() end Running this c_ListOfSkins() at any point (including the title screen before logging in) will create a file named Skins.txt in the data folder of the DST installation as noted with the contents you'd expect it to. Link to comment Share on other sites More sharing options...
JohnWatson Posted June 21, 2017 Author Share Posted June 21, 2017 @alainmcd Oh cheers, I appreciate the answer, that's actually very helpful. Is it possible to change the directory to the mod folder? Link to comment Share on other sites More sharing options...
alainmcd Posted June 21, 2017 Share Posted June 21, 2017 You definitely can at the very least use relative paths ("../mods/mymod/Scripts.txt" will write it to your mod folder provided mymod already exists). Whether you can create or delete folders or use absolute paths or system paths, I have no idea. Probably. It goes without saying, and I understand it's what you're trying, but for anyone else who sees this and thinks it's a good idea: only read and write files directly when you want to use them externally, not to save game data, and even then, only if you know what you're doing. Link to comment Share on other sites More sharing options...
JohnWatson Posted June 21, 2017 Author Share Posted June 21, 2017 @alainmcd Alright, I've managed to get it to write a complete working lua script. Is there a way to run scripts in a component script like modimport? The script is in '../mods/modname/scripts/components/poncoment.lua' and the script I'm trying to run is in '../mods/modname/test.lua' for example. Link to comment Share on other sites More sharing options...
alainmcd Posted June 21, 2017 Share Posted June 21, 2017 (edited) Was about to bump this with an improved example with basic error checking: function c_ListOfSkins() local ofilename = "..\\mods\\sillymod\\Skins.txt" local ofile = io.open(ofilename, "w") if ofile then for k, v in pairs(STRINGS.SKIN_NAMES) do if (k:sub(-8) ~= "_builder") then -- we don't want critter builders ofile:write(v.." ("..k..")\n") end end ofile:close() return true else print("could not open file "..ofilename) return false end end -- 24 minutes ago, JohnWatson said: @alainmcd Alright, I've managed to get it to write a complete working lua script. Is there a way to run scripts in a component script like modimport? The script is in '../mods/modname/scripts/components/poncoment.lua' and the script I'm trying to run is in '../mods/modname/test.lua' for example. You can always require it. Place your function in a file named test.lua in the scripts folder of your mod (so [DST installation]/mods/mymod/scripts/test.lua), something like this: MyTest = {} function MyTest.MyFunction(parameter) --your code end return MyTest Then call it from your modmain: local mytest = GLOBAL.require"test" mytest.MyFunction(something) And that could work. Btw, running code from your modmain could set the mod folder as the current path, I haven't tested it. Edited June 21, 2017 by alainmcd Link to comment Share on other sites More sharing options...
JohnWatson Posted June 21, 2017 Author Share Posted June 21, 2017 @alainmcd I used dofile("../mods/"..self.modname.."/scripts/Player Scoreboard List.lua") and it worked terrifically. Thanks for the help! Link to comment 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