How to save contents to a external file then load it later

Post Reply
LC Creations
Member
Posts: 77
Joined: Mon Feb 18, 2019 02:53

How to save contents to a external file then load it later

by LC Creations » Post

So, I tried this code I got from stack overflow

Code: Select all

file = io.open (filename [, mode])
However, when using this code, minetest runs an error. Minetest client says that it cannot do that becuase it is not safe or something.

User avatar
rubenwardy
Moderator
Posts: 6078
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: United Kingdom
Contact:

Re: How to save contents to a external file then load it later

by rubenwardy » Post

The error is because you are only allowed to write to the world folder (minetest.get_worldpath()). There's also a syntax error in the code snippet - [] indicates that something is optional - io.open(filename, "w") will open a handle.

You should avoid reading and writing to files manually in minetest. It tends to be inconvenient and subject to data loss (if you don't write safely)

Instead, you should prefer using meta data such as Mod Storage

Code: Select all

-- Call this in init.lua
local storage = minetest.get_mod_storage()

-- Some other function
storage:set_string("foo", "bar")
storage:get_string("foo") --> "bar"

Post Reply

Who is online

Users browsing this forum: thomz92 and 0 guests