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

Weird Problem With Functions

Started by coolocelot, 05 October 2013 - 03:16 PM
coolocelot #1
Posted 05 October 2013 - 05:16 PM
Title: Weird Problem With Functions!
K so I am making a OS(kinda'of, much more like a launcher) and I encountered one big annoying problem…

So I have this function

function MID(buttons,click,clickX, clickY)
for k,button in ipairs(buttons) do
  term.setCursorPos(45,10)
  for x=button[1],button[2] do
   term.setCursorPos(45,11)
   if x==clickX then
	term.setCursorPos(45,12)
	for y=button[3],button[4] do
	 term.setCursorPos(45,13)
	 if y==clickY then
	  if click==1 then
	   local func = loadstring("/data/settings/PROGRAMS/RadOS.lua")
	   setfenv(func, getfenv(1))
	   func()
	  end
	 end
	end
   end
  end
end
end


But the func() gives me "attempt to call nil"
If I replace it with

shell.run("/data/settings/PROGRAMS/RadOS.lua")
it will give me "Attempt to index ? (a nil value)
Note: If I move the shell.run or func() sequence outside this function they will work…

So, to use the function I would do:

DATAFOLDER="/data"
local _,click,clickX,clickY = os.pullEvent("mouse_click")
local buttons = readIniFile(DATAFOLDER.."/settings/MA/icons.ini")
MID(buttons,click,clickX,clickY)

icons.ini:

2.0|16.0|3.0|6.0|/data/settings/PROGRAMS/icon_softwareCenter|
19.0|33.0|3.0|6.0|/data/settings/PROGRAMS/icon_projector|
36.0|50.0|3.0|6.0|/data/settings/PROGRAMS/icon_console|
2.0|16.0|8.0|11.0|/data/settings/PROGRAMS/RadOS.lua|

Could somebody help me? I would have posted the whole script, but it has an entire army of configurations behind it…
Here you go anyway: Pastebin Link
Lyqyd #2
Posted 06 October 2013 - 01:22 AM
Split into new topic.

Aside from any other issues the code may have, a quick glance shows that it looks like you're using loadstring as if it is loadfile.
jay5476 #3
Posted 06 October 2013 - 01:43 AM
Split into new topic.

Aside from any other issues the code may have, a quick glance shows that it looks like you're using loadstring as if it is loadfile.
yes it does and i beleive loadstring returns boolean, function
theoriginalbit #4
Posted 06 October 2013 - 01:48 AM
yes it does and i beleive loadstring returns boolean, function
Nope.


local func, err = loadstring("print("Hello World!")")
if not func then
  print("Failed: ", err)
end
coolocelot #5
Posted 06 October 2013 - 02:05 AM
replaced loadstring with loadfile and it worked! Ty guys, I didn't think about that :)/>

BTW: Can I make something like this?

if local func = loadstring(string or file) then
	func()
elseif local func = loadfile(string or file) then
	func()
end

This function will run both api functions and files, so how can I pull this off?

NVM, i got it:

		if fs.exists(button[5]) then
		 local func = loadfile(button[5])
		 setfenv(func, getfenv(1))
		 func()
		else
		 func = loadstring(button[5])
		 setfenv(func, getfenv(1))
		 func()
		end

And still, replacing loadstring with shell.run shouldn't have worked? Cause' it didn't..

I think this coveres it all, you may close the thread if you need to, I would keep it open a little bit more, in case I encounter other problems I can't solve myself!
theoriginalbit #6
Posted 06 October 2013 - 02:50 AM
I think this coveres it all, you may close the thread if you need to, I would keep it open a little bit more, in case I encounter other problems I can't solve myself!
and for that reason it is very rare that threads are locked in AaP.