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

loadfile = bios.lua: 502: expected string, string

Started by EveryOS, 18 April 2016 - 12:57 PM
EveryOS #1
Posted 18 April 2016 - 02:57 PM
I am using CC 1.75

I am trying to use loadfile on a variable. It normally works, but for some reason it's currently not.


loadfile(fi)
bios.lua: 502: expected string, string

I tested 'fi' and got:

'NyanOS/menustuff/someFile'

The full file uses API, so I can't post it.
Edited on 18 April 2016 - 05:05 PM
EveryOS #2
Posted 18 April 2016 - 03:23 PM
I looked at the bios,

local file = fs.open( _sFile, "r" )
But I provided that argument, _sFile
EveryOS #3
Posted 18 April 2016 - 05:12 PM
If it's really needed, here's the code, but it does use API:


--Import is from custom API
import('menuUtils')
import('termUtils')
while true do
  local menurunning = menuUtils.getMenurunning()
  if menurunning==true then
	l,h=term.getSize();
	if not fs.exists('NyanOs/mem/sel') then
	  _G.options={
		'someFile'
	  };
	  _G.files={
		':/NyanOs/menustuff/someFile'
	  };
	  local file = fs.open('NyanOs/mem/sel','w');
	  file.write(textutils.serialise(options));
	  file.close();
	  local file = fs.open('NyanOs/mem/loc','w');
	  file.write(textutils.serialise(files));
	  file.close();
	else
	  file=fs.open('NyanOs/mem/sel','r');
	  options=textutils.unserialise(file.readAll());
	  file.close()
	  file=fs.open('NyanOs/mem/loc','r');
	  files=textutils.unserialise(file.readAll());
	  file.close();
	end;
	shortened={};
	local max=l/4;
	local highest=0;
	local nSel=0;
	for n,selection in ipairs(options) do
	nSel=n;
	  if string.len(selection) > max then
		highest=max;
		table.insert(shortened,string.sub(selection,0,max-3)..'...');
	  elseif string.len(selection) > highest then
		highest=string.len(selection);
		table.insert(shortened,selection);
	  else
		table.insert(shortened,selection);
	  end;
	end
	term.setCursorPos(1,h-nSel);
	term.setBackgroundColor(colors.white);
	term.setTextColor(colors.black);
	for n,text in ipairs(shortened) do
	  write(text)
	  for i=0,highest-string.len(text) do
		i=i+1;
		write(' ');
	  end;
	  print();
	end;
	local event,button,x,y = os.pullEvent('mouse_click');
	if button==nil then
	  event=''
	end
	if y~= h and button == 1 and x<= highest and y>h-#files-1 then
	  term.setTextColor(colors.white);
	  term.setBackgroundColor(colors.black);
	  term.clear();
	  term.setCursorPos(1,1)
	  local fi = files[#files-(h-y)+1]
	  print(fi)
	  termUtils.addProcess(loadfile(fi));
	  menuUtils.setMenurunning(false);
	  event=nil;
	  term.setBackgroundColor(colors.black);
	  term.clear();
	  break;
	else
	  menuUtils.setMenurunning(false)
	  term.setBackgroundColor(colors.black);
	  term.clear();
	  event=nil;
	  break;
	end
  end
end;
Edited on 18 April 2016 - 05:05 PM
Bomb Bloke #4
Posted 19 April 2016 - 12:21 AM
I'd replace this line:

print(fi)

… with something like:

if type(fi) ~= "string" then error("type(files["..tostring(#files-(h-y)+1).."]) = "..type(fi)..", #files = "..tostring(#files)..", h = "..tostring(h)..", y = "..tostring(y)) end