Hi, let's say i have source code for enemy saved in .txt file and need to load it to variable, and then run this code from that variable.
something like this:
function love.load()
monsterhp = 15
--want to load the .txt file contents here
end
function love.update(dt)
-- running txt contents here
end
Is it even possible?
Source code loading from .txt file :P
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Source code loading from .txt file :P
Make a .lua file for your enemy! Name it enemy.lua and you can use a require-command to use it in your main.lua:
You can define functions and variables in enemy.lua and use them by adding "enemy." without quotes in front of them in the main.lua. No need for txt-files here. If you look at some source code from other games, you can see that this is common practice. I suggest checking out http://stabyourself.net for some examples. The games in the sidebar are made with LÖVE and have the sources available.
Code: Select all
require("enemy")
Re: Source code loading from .txt file :P
Sure. Look for loadfile() / loadstring() / dofile() Lua's functions.
Re: Source code loading from .txt file :P
Do this:
This might also be of interest to you if you are running foreign untrusted lua code.
For lua 5.1: http://lua-users.org/wiki/SandBoxes
For lua 5.1 and 5.2: http://lua-users.org/wiki/EnvironmentsTutorial
These might help too: http://stackoverflow.com/questions/1128 ... in-lua-5-2 http://stackoverflow.com/questions/1224 ... ua-sandbox
Code: Select all
-- load file contents as a string
contents = love.filesystem.read("/path/to/file")
-- execute string
loadstring(contents) ()
-- use this if you want to "catch" any errors that are thrown when running this string of code
-- assert(loadstring(contents)) ()
For lua 5.1: http://lua-users.org/wiki/SandBoxes
For lua 5.1 and 5.2: http://lua-users.org/wiki/EnvironmentsTutorial
These might help too: http://stackoverflow.com/questions/1128 ... in-lua-5-2 http://stackoverflow.com/questions/1224 ... ua-sandbox
My Tox ID: 0F1FB9170B94694A90FBCF6C4DDBDB9F58A9E4CDD0B4267E50BF9CDD62A0F947E376C5482610
Re: Source code loading from .txt file :P
I~=Spam wrote:Do this:This might also be of interest to you if you are running foreign untrusted lua code.Code: Select all
-- load file contents as a string contents = love.filesystem.read("/path/to/file") -- execute string loadstring(contents) () -- use this if you want to "catch" any errors that are thrown when running this string of code -- assert(loadstring(contents)) ()
For lua 5.1: http://lua-users.org/wiki/SandBoxes
For lua 5.1 and 5.2: http://lua-users.org/wiki/EnvironmentsTutorial
These might help too: http://stackoverflow.com/questions/1128 ... in-lua-5-2 http://stackoverflow.com/questions/1224 ... ua-sandbox
Thank You very much That's exaclty what i need!
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests