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

[SOLVED] Getting a pcall function call know everything from pcall caller

Started by CosmoConsole, 09 July 2013 - 04:45 AM
CosmoConsole #1
Posted 09 July 2013 - 06:45 AM
I'm creating a debug console, so I can run lua commands without modifying the script itself.
Pressing a key will bring up an input, where you can type a command and it should run it
as if it were in the script.

I've taken an example from the "rom/programs/lua", but I cannot get the pcall function know
anything about from the caller of the pcall. Sounds incomprehensible? In my script, there's a function
called pause(). When this snippet calls the function I typed into the debug console:



local result = {pcall(function() return func() end)}

The line is ran properly, but it doesn't know what is "pause()" and returns error "attempt to call nil".

I've tried stuff like this right before the pcall line:


local env = getfenv()
setfenv(func, env)

but it doesn't work. How can I get it to work?
Cranium #2
Posted 09 July 2013 - 09:03 AM
Split into new topic.
KaoS #3
Posted 09 July 2013 - 09:08 AM
firstly you don't have to

local result = {pcall(function() return func() end)}
you can just

local result = {pcall(func)}

also setfenv should work fine… try this one

local result = {pcall(setfenv(func,getfenv()))}
this works because setfenv(func,env) returns the function it modifies. PLEASE NOTE: local functions are a lot more difficult, make sure you are not using a local function, if you are then please let me know so I can make a workaround

EDIT: when I say using a local function I mean the pause() function must not be local

EDIT2: also if you could provide us the full code it would make troubleshooting a whole lot easier :)/> https://dl.dropboxusercontent.com/u/84093549/Links/NINJAAAA.png
Edited on 09 July 2013 - 07:17 AM
GopherAtl #4
Posted 09 July 2013 - 09:10 AM
Hard to help without seeing the code. If pause function is local, it won't be accessible in the fenv, though, I don't think?
CosmoConsole #5
Posted 09 July 2013 - 10:22 AM
The full code is here:
http://pastebin.com/x5W3su3e
CosmoConsole #6
Posted 09 July 2013 - 10:25 AM
(REPLY)
This one worked out.