This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
sploders101's profile picture

Running lua code from a string

Started by sploders101, 10 April 2013 - 07:49 AM
sploders101 #1
Posted 10 April 2013 - 09:49 AM
Is there any simple way to run lua code from a variable without creating a new file and running it?
Like, for example: an ssh server/client?

Thanks in advance and tell me if I need to include any more information as I've never posted on a forum before.

–Superanonymous ;)/>
ThePH #2
Posted 10 April 2013 - 12:26 PM
Please someone move this thread to the right place…
To run a function from a string, (that is called 'evaluating') you use loadstring(string) in lua 5.1- (computercaft) or load(string) in 5.2+
What it will do is compile the string to an anonymous function which can be run.

eg:


--Defining a string to use as function later
funcStr = "print('test')"
--Running it directly
loadstring(funcStr)()
--Defining a function from the string and running it
func = loadstring(funcStr)
func()

Correct me if i'm wrong, but I believe i'm not ^_^/>
SuicidalSTDz #3
Posted 10 April 2013 - 12:31 PM
Please someone move this thread to the right place…
You have two hands and a mouse, use them. There is a report button at the bottom of each post. Report the main post.
sploders101 #4
Posted 11 April 2013 - 09:39 AM
It only returns the function but doesn't actually run it. Didn't see the last part of the code ill try that. I used to play roblox and used that all the time and I hate to see it go. Any other ideas?
sploders101 #5
Posted 11 April 2013 - 09:47 AM
That last part worked thanks.


Umm… How do I close a thread?
Lyqyd #6
Posted 11 April 2013 - 11:00 AM
You don't. We do, if we feel it's necessary. I will move this to Ask a Pro, but I'd also like to point out nsh, my remote shell program. Just in case you're looking for a complete ssh-type solution at some point.
sploders101 #7
Posted 13 April 2013 - 08:40 AM
Thank you but I'd like to have the thrill of writing my own. ;)/>