Jump to content




German | attempt to index ? (a nil value)



  • You cannot reply to this topic
4 replies to this topic

#1 Dr.Prof.Evil

  • New Members
  • 2 posts

Posted 20 August 2014 - 02:03 PM

Guten Tag | Hello

Also: Ich schreibe gerade ein kleines Programm für manche Turtle aus Computercraft dieser Welt und habe dafür bis jetzt 3 Datei benutzt.
In der "StartComputer" befinden sich ein paar funktionen die dann ausgeführt werden, wenn der Computer startet. Diese werden in der startup mit os.loadAPI("StartComputer") und dann StartComputer.(function) aufgerufen.
Dies funktioniert, doch sobald in der Funktion in der StartComputer sich eine Shell.[...] befehl kommt, dann kommt der Fehler: StartComputer: 27: attempt to index ? (a nil value)
In dieser Zeile (27) steht der 1. Shell.[...] Befehl. Ich weiß nicht woran dies liegt und bitte euch daher um hilfe.
Danke im vorraus.
-- startup
-- Variablen:
local function Ausgabe(text)

   local x_r,y_r   = term.getSize() local x2_r,y2_r = term.getCursorPos()		  --	  speichert die Position des Cursors
   term.setCursorPos(math.ceil((x_r / 2) - (text:len() / 2)), y2_r)				--	  Zentriert den Text
   write(text .. "\n")
	  
end
function main()
firstStartCheck()
os.loadAPI("StartComputer")[/size][/font][/color]
StartComputer.Instanziieren()  -- 'befüllt' die Variablen
StartComputer.Informationen()  -- Gibt Informationen aus
StartComputer.Download()  -- downloaded alles wichtige

while true do
  Menu()
end

shell.run("reboot")
end
function firstStartCheck()

if fs.exists("StartComputer") == false then

  shell.run("delete StartComputer")
  shell.run("pastebin get G2YmEZNW StartComputer")

end
end
main()


-- StartComputer[/size][/font][/color]
NameOfTurtle = " "
-- Beschreibt die Variablen
function Instanziieren()
NameOfTurtle = os.getComputerLabel()
Load()
end
-- Ladet die Variablen
function Load()
if fs.exists("Evil_Variablen") == true then  -- Hier werden die Variablen ausgelesen wenn die Datei vorhanden ist
  reader = fs.open("Evil_Variablen","r")
  NameOfTurtle = reader.readLine()
  reader.close()
else		   --  Hier werden die Variablen NEU reingeschrieben wenn die Datei nicht vorhanden ist
  writer = fs.open("Evil_Variablen","w")
  writer.writeLine(NameOfTurtle)
  writer.close()
end
end
-- funny sentences
function Informationen()
-- Kann man später einstellen ob man das angezeigt haben möchte[/size][/font][/color]
shell.run("clear")

print("Computer wird gestartet...")

sleep(1)

print("CPU und GPU machen sich ein Kaffee...")

sleep(0.5)

print("CPU und GPU nehmen die Arbeit auf...")

sleep(1)

print("Computer: " .. os.getComputerLabel())


end
-- Hier werden alle Datei heruntergeladen und vorher gelöscht
function Download()
shell.run("delete startup")
shell.run("delete StartComputer")
shell.run("delete menus")
shell.run("delete strip")
shell.run("delete tanken")

shell.run("pastebin get p7HhEhV4 startup")
shell.run("pastebin get G2YmEZNW StartComputer")
shell.run("pastebin get pVdh36xC menus")
shell.run("pastebin get JMtaBaNy strip")
shell.run("pastebin get gkr34rS8 tanken")
end


-- 3 menues
function MainMenu()
l = 1 -- Var für Auswahl

while true do
  shell.run("clear")

  Ausgabe("Willkommen\n\n")

  if l == 1 then  Ausgabe("[Programme]")
  else   Ausgabe(" Programme ")
  end

  if l == 2 then  Ausgabe("[Aktualisieren]")
  else   Ausgabe(" Aktualisieren ")
  end

  if l == 3 then  Ausgabe("[Einstellungen]")
  else   Ausgabe(" Einstellungen ")
  end

  a, b = os.pullEvent()
  while a ~= "key" do
   a, b = os.pullEvent()
  end

   if b == 208 and l == 1 then l = 2
  elseif b == 208 and l == 2 then l = 3

  elseif b == 200 and l == 3 then l = 2
  elseif b == 200 and l == 2 then l = 1

  elseif b ==  28	  then break
	end

end
  if l == 1 then ProgramsMenu()
elseif l == 2 then
elseif l == 3 then SettingsMenu()
  end
end
local function SettingsMenu()
l = 1 -- Var für Auswahl

while true do
  shell.run("clear")

  Ausgabe("Einstellungen\n\n")

  if l == 1 then  Ausgabe("[Name des Turtels: " .. os.getComputerLabel() .. "]")
  else   Ausgabe(" Name des Turtels: " .. os.getComputerLabel() .. " ")
  end

  if l == 2 then  Ausgabe("[Zurueck]")
  else   Ausgabe(" Zurueck ")
  end


  a, b = os.pullEvent()
  while a ~= "key" do
   a, b = os.pullEvent()
  end

   if b == 208 and l == 1 then l = 2

  elseif b == 200 and l == 2 then l = 1

  elseif b ==  28	  then break
	end

end

  if l == 1 then TurtleNameWechseln()
  end
end
local function ProgramsMenu()
l = 1 -- Var für Auswahl

while true do
  shell.run("clear")

  Ausgabe("Einstellungen\n\n")

  if l == 1 then  Ausgabe("[Stripmineprogramm]")
  else   Ausgabe(" Stripmineprogramm ")
  end

  if l == 2 then  Ausgabe("[Tanken]")
  else	Ausgabe(" Tanken ")
  end

  if l == 3 then  Ausgabe("[Zurueck]")
  else   Ausgabe(" Zurueck ")
  end


  a, b = os.pullEvent()
  while a ~= "key" do
   a, b = os.pullEvent()
  end

   if b == 208 and l == 1 then l = 2
  elseif b == 208 and l == 2 then l = 3

  elseif b == 200 and l == 3 then l = 2
  elseif b == 200 and l == 2 then l = 1

  elseif b ==  28	  then break
	end

end

  if l == 1 then shell.run("strip")
elseif l == 2 then shell.run("tanken")
  end

end
local function TurtleNameWechseln()
NewName = ""

print("")
write("Neuer Name: ")

NewName = read()

if NewName ~= nil or NewName ~= " " then
  shell.run("label set " .. NewName)
  shell.run("clear")
end
end

local function Ausgabe(text)

   local x_r,y_r   = term.getSize() local x2_r,y2_r = term.getCursorPos()		  --	  speichert die Position des Cursors
   term.setCursorPos(math.ceil((x_r / 2) - (text:len() / 2)), y2_r)				--	  Zentriert den Text
   write(text .. "\n")
	  
end


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 20 August 2014 - 03:09 PM

Moved to Ask a Pro. For reference, this is an English-speaking forum, so please be sure to provide at least a Google Translate translation to English.

The error is caused because APIs do not have access to the shell "API". The easiest thing to do is to run code that calls shell.run from the main program, or to rewrite the API to eliminate the need for the shell.run calls.

#3 Dr.Prof.Evil

  • New Members
  • 2 posts

Posted 20 August 2014 - 03:21 PM

View PostLyqyd, on 20 August 2014 - 03:09 PM, said:

[...] or to rewrite the API to eliminate the need for the shell.run calls.

And how can I do this?

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 20 August 2014 - 03:56 PM

Instead of shell.run("clear"), you could use this:

term.clear()
term.setCursorPos(1, 1)

Instead of deleting things through the delete program, you could use fs.delete(). And instead of downloading things through the pastebin program, you could use the http and fs APIs to fetch the data and write it to the appropriate files.

#5 koslas

  • Members
  • 62 posts
  • LocationChristchurch, New Zealand

Posted 20 August 2014 - 04:39 PM

Just edited your code to easily update your programs without using shell.run()

Spoiler

Spoiler

Spoiler

Also try indenting all of your code, as it's easier to read, and also instead of shell.run("clear") I always make a function like this

function clear()
  term.clear()
  term.setCursorPos(1,1)
end

Therefore instead of doing shell.run("clear") you can just call clear()

Edited by koslas, 20 August 2014 - 04:41 PM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users