How do I use external files as tables in Lua?
I want to make a program that chooses a random monster from a list, give the user a list of usable weapons, and allows them to choose which weapon to use. I want to use external files to store data for the weapons instead of adding the tables in the Lua file itself. I have tried using Lua files to store the data as a table.
I have a file called sword.lua in the same folder as the program I want to access it's information. It contains
sword = {'sword', '10', '1', '100'}
I am trying to access the information using
wep = io.open("sword.lua", "r")
print(wep:read("*a"))
print(wep[1])
The first print returns all of the text in the file which is
"sword = {'sword', '10', '1', '100'}"
and the second is supposed to return the first item in the table. Every time I do this, I get a nil value from the second print. The file is being read, as indicated by the first print listing the text, but how do I make it read the file as a table that I can use in my program.
lua lua-table
add a comment |
I want to make a program that chooses a random monster from a list, give the user a list of usable weapons, and allows them to choose which weapon to use. I want to use external files to store data for the weapons instead of adding the tables in the Lua file itself. I have tried using Lua files to store the data as a table.
I have a file called sword.lua in the same folder as the program I want to access it's information. It contains
sword = {'sword', '10', '1', '100'}
I am trying to access the information using
wep = io.open("sword.lua", "r")
print(wep:read("*a"))
print(wep[1])
The first print returns all of the text in the file which is
"sword = {'sword', '10', '1', '100'}"
and the second is supposed to return the first item in the table. Every time I do this, I get a nil value from the second print. The file is being read, as indicated by the first print listing the text, but how do I make it read the file as a table that I can use in my program.
lua lua-table
"the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's whatwepis) to return an item in a table described by a string stored in that file?
– Nicol Bolas
Dec 31 '18 at 21:13
add a comment |
I want to make a program that chooses a random monster from a list, give the user a list of usable weapons, and allows them to choose which weapon to use. I want to use external files to store data for the weapons instead of adding the tables in the Lua file itself. I have tried using Lua files to store the data as a table.
I have a file called sword.lua in the same folder as the program I want to access it's information. It contains
sword = {'sword', '10', '1', '100'}
I am trying to access the information using
wep = io.open("sword.lua", "r")
print(wep:read("*a"))
print(wep[1])
The first print returns all of the text in the file which is
"sword = {'sword', '10', '1', '100'}"
and the second is supposed to return the first item in the table. Every time I do this, I get a nil value from the second print. The file is being read, as indicated by the first print listing the text, but how do I make it read the file as a table that I can use in my program.
lua lua-table
I want to make a program that chooses a random monster from a list, give the user a list of usable weapons, and allows them to choose which weapon to use. I want to use external files to store data for the weapons instead of adding the tables in the Lua file itself. I have tried using Lua files to store the data as a table.
I have a file called sword.lua in the same folder as the program I want to access it's information. It contains
sword = {'sword', '10', '1', '100'}
I am trying to access the information using
wep = io.open("sword.lua", "r")
print(wep:read("*a"))
print(wep[1])
The first print returns all of the text in the file which is
"sword = {'sword', '10', '1', '100'}"
and the second is supposed to return the first item in the table. Every time I do this, I get a nil value from the second print. The file is being read, as indicated by the first print listing the text, but how do I make it read the file as a table that I can use in my program.
lua lua-table
lua lua-table
edited Jan 1 at 0:44
braaterAfrikaaner
1,085517
1,085517
asked Dec 31 '18 at 20:47
Omega HenryOmega Henry
83
83
"the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's whatwepis) to return an item in a table described by a string stored in that file?
– Nicol Bolas
Dec 31 '18 at 21:13
add a comment |
"the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's whatwepis) to return an item in a table described by a string stored in that file?
– Nicol Bolas
Dec 31 '18 at 21:13
"the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's what
wep is) to return an item in a table described by a string stored in that file?– Nicol Bolas
Dec 31 '18 at 21:13
"the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's what
wep is) to return an item in a table described by a string stored in that file?– Nicol Bolas
Dec 31 '18 at 21:13
add a comment |
1 Answer
1
active
oldest
votes
To load a table from a file use the require function. For example, save
return { 'sword', '10', '1', '100' }
as sword.lua. Why do I just use return instead of assigning to a variable? It's because that is much more flexible. If I assign the table to the variable sword inside the file, I'm sort of locked into that naming convention and furthermore I pollute the global namespace, making name collisions more likely.
With the above solution I can also assign to a local variable, like so
local sword = require("sword")
print(table.concat(sword,", "))
Another advantage is that calls to require are cached, i.e. even if you require("sword") many times, you only pay the cost for loading once. But keep in mind that because of caching you always get a handle to the same table, i.e. if you alter the table returned from require("sword"), these modifications will be shared by all instances.
Example on Wandbox
Thank you so much! That does exactly what I need it to.
– Omega Henry
Jan 1 at 0:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991297%2fhow-do-i-use-external-files-as-tables-in-lua%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To load a table from a file use the require function. For example, save
return { 'sword', '10', '1', '100' }
as sword.lua. Why do I just use return instead of assigning to a variable? It's because that is much more flexible. If I assign the table to the variable sword inside the file, I'm sort of locked into that naming convention and furthermore I pollute the global namespace, making name collisions more likely.
With the above solution I can also assign to a local variable, like so
local sword = require("sword")
print(table.concat(sword,", "))
Another advantage is that calls to require are cached, i.e. even if you require("sword") many times, you only pay the cost for loading once. But keep in mind that because of caching you always get a handle to the same table, i.e. if you alter the table returned from require("sword"), these modifications will be shared by all instances.
Example on Wandbox
Thank you so much! That does exactly what I need it to.
– Omega Henry
Jan 1 at 0:57
add a comment |
To load a table from a file use the require function. For example, save
return { 'sword', '10', '1', '100' }
as sword.lua. Why do I just use return instead of assigning to a variable? It's because that is much more flexible. If I assign the table to the variable sword inside the file, I'm sort of locked into that naming convention and furthermore I pollute the global namespace, making name collisions more likely.
With the above solution I can also assign to a local variable, like so
local sword = require("sword")
print(table.concat(sword,", "))
Another advantage is that calls to require are cached, i.e. even if you require("sword") many times, you only pay the cost for loading once. But keep in mind that because of caching you always get a handle to the same table, i.e. if you alter the table returned from require("sword"), these modifications will be shared by all instances.
Example on Wandbox
Thank you so much! That does exactly what I need it to.
– Omega Henry
Jan 1 at 0:57
add a comment |
To load a table from a file use the require function. For example, save
return { 'sword', '10', '1', '100' }
as sword.lua. Why do I just use return instead of assigning to a variable? It's because that is much more flexible. If I assign the table to the variable sword inside the file, I'm sort of locked into that naming convention and furthermore I pollute the global namespace, making name collisions more likely.
With the above solution I can also assign to a local variable, like so
local sword = require("sword")
print(table.concat(sword,", "))
Another advantage is that calls to require are cached, i.e. even if you require("sword") many times, you only pay the cost for loading once. But keep in mind that because of caching you always get a handle to the same table, i.e. if you alter the table returned from require("sword"), these modifications will be shared by all instances.
Example on Wandbox
To load a table from a file use the require function. For example, save
return { 'sword', '10', '1', '100' }
as sword.lua. Why do I just use return instead of assigning to a variable? It's because that is much more flexible. If I assign the table to the variable sword inside the file, I'm sort of locked into that naming convention and furthermore I pollute the global namespace, making name collisions more likely.
With the above solution I can also assign to a local variable, like so
local sword = require("sword")
print(table.concat(sword,", "))
Another advantage is that calls to require are cached, i.e. even if you require("sword") many times, you only pay the cost for loading once. But keep in mind that because of caching you always get a handle to the same table, i.e. if you alter the table returned from require("sword"), these modifications will be shared by all instances.
Example on Wandbox
answered Dec 31 '18 at 21:36
Henri MenkeHenri Menke
7,61511327
7,61511327
Thank you so much! That does exactly what I need it to.
– Omega Henry
Jan 1 at 0:57
add a comment |
Thank you so much! That does exactly what I need it to.
– Omega Henry
Jan 1 at 0:57
Thank you so much! That does exactly what I need it to.
– Omega Henry
Jan 1 at 0:57
Thank you so much! That does exactly what I need it to.
– Omega Henry
Jan 1 at 0:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991297%2fhow-do-i-use-external-files-as-tables-in-lua%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's what
wepis) to return an item in a table described by a string stored in that file?– Nicol Bolas
Dec 31 '18 at 21:13