run a local Lua-code

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

run a local Lua-code

Post by Luke100000 »

I want to add in my game a computer where you can program in Lua. But I have some problems:
1.) how I can run a code, that he doesn't edit important variables of the games and that you can create own variables whitout deleting the old one?
2.) You should only use special functions, not direct functions of love2d.

here is an example how I want it...

Code: Select all


code = "..." --the code string
functions = {...} --special functions like print, computer on/off, ...

ok, errorstring = loadstring(code)
if ok then
   ok() --so, now it should have "functions" and his own variables, but how?
else
   print(errorstring)
end

Please no unhelpful commends like "Your english is bad!",..., Thanks.
Last edited by Luke100000 on Tue Apr 01, 2014 11:51 am, edited 1 time in total.
User avatar
slime
Solid Snayke
Posts: 3170
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: run a local Lua-code

Post by slime »

Maybe look into the setfenv and loadstring functions.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: run a local Lua-code

Post by micha »

For an example of a game that let's the player program, check out trainsported. It is make by Germanunkol (on this forum). He solved the programming by letting the player use an external text editor.

Fonts with constant letter width are called monospaced fonts
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Re: run a local Lua-code

Post by Luke100000 »

I edit my question, maybe you know better what I want now.
For an example of a game that let's the player program, check out trainsported. It is make by Germanunkol (on this forum). He solved the programming by letting the player use an external text editor.
I don´t find the part of the programming which I need...
But this is exactly what I wanted. :awesome:
Maybe look into the setfenv and loadstring functions.
of course I use loadstring, but I need something too. :death:

If someone knows how it works, please press POSTREPLY.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: run a local Lua-code

Post by Robin »

What slime meant is you'd have to use loadstring together with setfenv. Have you looked into [manual]setfenv[/manual]? It's... not very complicated.
Help us help you: attach a .love.
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Re: run a local Lua-code

Post by Luke100000 »

Thank's to everbody! It works! Sorry slime, I thought setfenv is something like loadstring... :crazy:

I tried it so:

Code: Select all

a = true
env = { }
env.a = true
code = "a = false" --it will set a to false
ok = loadstring(code) --create a function
setfenv(ok, env) --set the enviroment of ok() to env
ok() --run ok
print(a, env.a) --- true, false
setfenv(ok, _G) --set the enviroment of ok() to the standart, called _G
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: run a local Lua-code

Post by Robin »

I'd leave out that last line, Luke, if you don't use ok later doesn't do anything, and if you do, it does something you don't want.
Help us help you: attach a .love.
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Re: run a local Lua-code

Post by Luke100000 »

Robin wrote:I'd leave out that last line, Luke, if you don't use ok later doesn't do anything, and if you do, it does something you don't want.
I just want to use _G :awesome:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest