magnanimousness Posted December 1, 2013 Share Posted December 1, 2013 Hello Everyone, Is it possible to read and write to files with the lua scripts? I tried editing the follow the leader script to write the entities that follow to a file using this code:file = io.open("test.txt", "w")file:write("Adding Follower: " .. v .. "\n") file:close()But anytime this gets executed I get the error: "attempt to index global 'io' (a nil value)" Anybody have any ideas? Thanks, Link to comment https://forums.kleientertainment.com/forums/topic/30117-file-io/ Share on other sites More sharing options...
Malacath Posted December 1, 2013 Share Posted December 1, 2013 I guess it's not ever needed so the input/output library is probably not loaded. I have no clue how that library is called but I think you'll just have to load it similiar tolocal io = require "io"A person with more Lua experience should be able to tell you the name of the library. Link to comment https://forums.kleientertainment.com/forums/topic/30117-file-io/#findComment-381245 Share on other sites More sharing options...
magnanimousness Posted December 1, 2013 Author Share Posted December 1, 2013 I tried your suggestion and got this instead: attempt to call global 'require' (a nil value) Which seems, based on my limited knowledge, like a weird error to get. Isn't 'require' intrinsic to lua? Link to comment https://forums.kleientertainment.com/forums/topic/30117-file-io/#findComment-381247 Share on other sites More sharing options...
magnanimousness Posted December 1, 2013 Author Share Posted December 1, 2013 Okay no wait, that was my bad, I didnt have local require = GLOBAL.require In the file, that seems to have worked, getting an error concatenating the string, but the io nil value issue is gone. Thanks Malacath Link to comment https://forums.kleientertainment.com/forums/topic/30117-file-io/#findComment-381259 Share on other sites More sharing options...
Malacath Posted December 1, 2013 Share Posted December 1, 2013 (edited) I don't know how exactly it works, but it's something like this. All the default libraries (string, math...) are loaded in the GLOBAL environment wich of course also includes 'require'. But the MOD environment which you are in when coding inside modmain and modwordlgen is almost empty. It has all the functions to hook into prefabs and classes and so on but barely anything else. So you'll need to grab require from GLOBAL likelocal require = GLOBAL.requireEdit: Damnit, you ninja'd me xDTry usingfile:write("Adding Follower: "..tostring(v).."\n") I don't know if entities automatically get converted when trying to concatenate them.EDIT: Probably you even needlocal tostring = GLOBAL.tosring Edited December 1, 2013 by Malacath Link to comment https://forums.kleientertainment.com/forums/topic/30117-file-io/#findComment-381262 Share on other sites More sharing options...
magnanimousness Posted December 1, 2013 Author Share Posted December 1, 2013 haha, yeah I face palmed pretty hard when I saw I was missing the GLOBAL.require line. Okay the tostring worked perfectly, didnt need to add the GLOBAL.tostring() so it looks like thats added automatically. Thanks for the assist Malacath, much appreciated. Link to comment https://forums.kleientertainment.com/forums/topic/30117-file-io/#findComment-381270 Share on other sites More sharing options...
simplex Posted December 1, 2013 Share Posted December 1, 2013 haha, yeah I face palmed pretty hard when I saw I was missing the GLOBAL.require line. Okay the tostring worked perfectly, didnt need to add the GLOBAL.tostring() so it looks like thats added automatically. Thanks for the assist Malacath, much appreciated.You can also do simplyio = GLOBAL.ioinstead of require()'ing it. And it's best to do something likefile = io.open(MODROOT.."test.txt", "w")instead, so the file will be placed inside your mod folder. Link to comment https://forums.kleientertainment.com/forums/topic/30117-file-io/#findComment-381306 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