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

[Resolved] API function attempt to call nil

Started by sirenman2000, 20 December 2013 - 08:24 PM
sirenman2000 #1
Posted 20 December 2013 - 09:24 PM
Hello,

So I coded an API for use with transmitting rednet signals. I have already verified that the API can be loaded and is transmitting signals successfully. However upon trying to use some of the other functions it says Attempt To Call Nil on line 6. I have tried it with calling the other functions of the same type and the same result, attempt to call nil on their respective lines. Any help is greatly appreciated!

Code Calling the function

function setWarplan()
		rednet.open("top")
		unused, warplan = rednet.receive()
		os.loadAPI("warplan")
				if warplan == "1" then
						warplan.set1()
				elseif warplan == "2" then
						warplan.set2()
				elseif warplan == "3" then
						warplan.set3()
				elseif warplan == "4" then
						warplan.set4()
				elseif warplan == "5" then
						warplan.set5()
				else
						print("Invalid Warplan")
				end
end

Function being called (Pretty sure it's not here, and the correct file is being loaded)

function set1()
		rednet.open("top")
				rednet.send(30, "griff")
				rednet.send(31, "altMain")
				rednet.send(33, "lightMain")
				rednet.send(35, "spleefArena")
				rednet.send(36, "moria")
				rednet.send(37, "willMain")
				rednet.send(38, "1.6House")
				rednet.send(43, "generalShelter")
		rednet.close()
		print("Missile plans sent!")
end
Edited on 21 December 2013 - 05:42 PM
Bomb Bloke #2
Posted 20 December 2013 - 10:58 PM
When you load your "warplan" API, the "warplan" variable gets assigned a table with the functions of that API in it.

When you call "unused, warplan = rednet.receive()", the "warplan" variable gets overwritten with whatever message was received, meaning you lose access to the API's table. "warplan.set1" is hence nil, and you can no longer call it.

Bear in mind also that CC won't re-load an API that's already loaded, and they remain loaded when your script ends unless you specifically specify otherwise. Regardless, don't try to use names for your "regular" variables that're equal to the names of your functions/APIs - they ALL get treated as variables and WILL conflict!
Edited on 20 December 2013 - 10:01 PM
sirenman2000 #3
Posted 21 December 2013 - 06:59 PM
Thank you very much! Tried it out and fixed perfectly.