ingie 6 Posted September 16, 2014 Share Posted September 16, 2014 Just a quick note on something that may confuse a beginner [like me, until i found out what to do] When prototyping a lua library, i tend to have a lua prompt open for testing and evaluating things, and an offline editor for the code - in mycase ZerobraneStudio - and have the config option "bufferChanges=false" instead of true, so that the file system on actual disk, is always in sync with the virtual game HDD [ note, see the warnings and notice in the config if you're changing this yourself, you've been warned - but unless your game crashes, it should be safe - it will often be safe even if the game does crash, but... YMMMYRYHO* ] However, you probably find that you're making "on-the-fly" changes to your library, and want to see them take effect in your lua prompt. so, imagine in my editor i type local lib = {} lib.test = function() print("Hello, Lua") end return lib i save this as "testlib.lib" in my home/lib folder and at my lua prompt i type lua> t = require("testlib") lua> t.test() Hello, Lua lua> i then make a change to my testlib.lib local lib = {} lib.test = function(name) print("Hello, "..tostring(name)) end return lib and save that over the testlib.lua file. still at my lua prompt, i want to test this change, so i naturally type: lua> t = require("testlib") -- to reload the library changes lua> t.test("ingie") Hello, Lua lua> hold on, i say, that's not my changed library... a first thought might be, ah, i know: lua> t2 = require("testlib") lua> t2.test("ingie") Hello, Lua lua> to fool lua into creating a new library... but no, lua is too clever for that... so, how do i rectify this? well, lua uses a table packages.loaded to tell whether it should bother loading a library or not.we can check this. lua> for p in pairs(packages.loaded) do print p end ... other loaded packages ... testlib ... other loaded packages ... lua> aha! so, to reload our library, we can just nillify [if that's a word] the entry for our library note that this name is the name of the filename, less extension, i.e. the bit we "required" it with, not the name of the variable we assigned it to lua> packages.loaded.testlib = nil and then as before lua> t = require("testlib") -- to reload the library changes lua> t.test("ingie") Hello, ingie lua> this caught me out for a while, before i read this note also that the same is true even if you're exiting the lua prompt, using edit within OC to change the file, then going back into the lua prompt to test... the shell environment will still hold your library from before. * your mileage may make you rip your hair out Quote Link to post Share on other sites
Eunomiac 6 Posted December 22, 2014 Share Posted December 22, 2014 I'm not sure if this is due to a change in more-recent versions, but you'll want to use "package.loaded", rather than packages.loaded, for this to work. This also works from within programs: Simply set the package.loaded table to nil for any libraries you frequently edit just before you require them, and any changes made to those libraries will be reflected on a soft reboot (rather than having to manually turn the computer/server off-then-on). Here's a C&P from the top of my program: --========= DECLARING TOP-SCOPE VARIABLES ============================================================================ local component, dbCard, cb local str, event, term local A, D, H local CLASS, HEXCLASS local CONSOLE, GRID local DB, STATUS do --========= INITIALIZING TOP-LEVEL COMPONENTS, CLASSES & VARIABLES ================================================ do --*** IMPORTING LIBRARIES & INITIALIZING TABLE VARIABLES *** component = require("component") dbCard = component.debug cb = component.command_block str = require("unicode") event = require("event") term = require("term") package.loaded.ArtLib = nil package.loaded.DataLib = nil package.loaded.HexConfig = nil A = require("ArtLib") D = require("DataLib") H = require("HexConfig") --// snip // ... and so on Quote Link to post Share on other sites
foxzoolm 0 Posted May 6, 2015 Share Posted May 6, 2015 useful ! thanks ! I use lib to store saving in json. --data.lua local data={<serialized>} return data Im bit loafer to code a good way to do this otherwise ) Quote Link to post Share on other sites
raingloom 0 Posted November 4, 2015 Share Posted November 4, 2015 If OpenOS require is anything like standard Lua's, then you can force a reload for all future `require` calls by `table.remove`-ing the first entry in `package.loaders`. That is the one that queries `package.loaded`. Quote Link to post Share on other sites
dgelessus 26 Posted November 4, 2015 Share Posted November 4, 2015 If OpenOS require is anything like standard Lua's, then you can force a reload for all future `require` calls by `table.remove`-ing the first entry in `package.loaders`. That is the one that queries `package.loaded`. That's a bad idea, there probably is some code that relies on require not reloading every module every time. It's safer to remove the modules that you want to reload from package.loaded instead. Quote Link to post Share on other sites