Jump to content

Recommended Posts

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

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 to

local io = require "io"

A person with more Lua experience should be able to tell you the name of the library.

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 like

local require = GLOBAL.require

Edit: Damnit, you ninja'd me  xD

Try using

file:write("Adding Follower: "..tostring(v).."\n")    

I don't know if entities automatically get converted when trying to concatenate them.

EDIT: Probably you even need

local tostring = GLOBAL.tosring
Edited by Malacath

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.

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 simply

io = GLOBAL.io
instead of require()'ing it. And it's best to do something like

file = io.open(MODROOT.."test.txt", "w")
instead, so the file will be placed inside your mod folder.

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...