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

startup:7: attemp to compare __l t on nill and number

Started by luchokoldo, 14 March 2014 - 12:00 PM
luchokoldo #1
Posted 14 March 2014 - 01:00 PM
hi guys, firs sorry for my english xD.. this Its my secos program and this control my first program to emir redstone signal to start my dynamos(TE3).. i cant see the problem.. i know other lengueges like pawn and visual c# and I trying to learn lua.. I think anyone can help in my mistakes


function setNumDynamo(num)

  if num < 0 then

    num = 0 

  elseif num > MaxDynamo then

    num = MaxDynamo

  end

  peripheral.call(ModemPoss, "transmit", DefaultChannel, DefaultChannel, tostring(num))

  return num

end

function getAmmoutEnergy()

	return DynamosActive * EnergyPerTick
	
end

function getModemPoss()

  for x, pName in ipairs(peripheral.getNames()) do

    if peripheral.getType(pName) == "modem" then

      return(pName)

    end

  end

end

function setRestartEngine()

	peripheral.call(ModemPoss, "transmit", DefaultChannel, RestartID, "")
	
end

local DefaultChannel = 1

local RestartID = 2707

local MaxDynamo = 130

local EnergyPerTick = 80

local ModemPoss = getModemPoss()

local DefaultDynamos = 42

sleep(5)

setNumDynamo(DefaultDynamos)

local Command = ""

local szOutput = { "", "$: Update Complete", "$: Dynamos Updates Complete", "$: ReBoot Complete", "$: Unknow Command" }

local aOutput = { aEmpty = 1, aUpdate = 2, aDyUpdate = 3, aReBoot = 4, aUnkCommand = 5 }

local OutputIndex = aOutput["Empty"]

local DynamosActive = DefaultDynamos

while true do

	term.clear()

	term.setCursorPos(1, 1)
	
	print("Dynamos funcionando: " ..tostring(DynamosActive))
	
	term.setCursorPos(1, 3)
	
	print("Energia Producida: " ..tostring(getAmmoutEnergy()) .."RF/t")
	
	term.setCursorPos(1, 6)
	
	print(szOutput[OutputIndex])
	
	term.setCursorPos(1, 5)
	
	term.write("$Rak$/: ")
	
	Command = read()
	
	if string.find(Command, "set") then
	
		DynamosActive = setNumDynamo(tonumber(string.sub(Command, 4)))
		
		OutputIndex = aOutput["aDyUpdate"]
		
	elseif Command == "Update" then
	
		setRestartEngine()
		
		sleep(3)
		
		setNumDynamo(DynamosActive)
		
		OutputIndex = aOutput["aUpdate"]
		
	elseif Command == "reboot" then
	
		DynamosActive = setNumDynamo(DefaultDynamos)
		
		OutputIndex = aOutput["aReBoot"]
	
	else
	
		OutputIndex = aOutput["aUnkCommand"]
		
	end
	
end

thanks for read! :3

Edit: the problem Is in setNumDynamo and i Can't solve this
Edited on 14 March 2014 - 01:52 PM
CometWolf #2
Posted 14 March 2014 - 02:55 PM
The problem is the following line, line 7. according to the error, the second variable is a nil, in this case MaxDynamo.

elseif num > MaxDynamo then
It's a nil variable because this function is defined prior to defining the variable locally. This makes it point to the global variable MaxDynamo(_G.MaxDynamo) instead of the locally defined one(MaxDynamo). Meanwhile, the one you defined is defined locally after the function is defined, and thus is not the same variable. I'd suggest defining the variable prior to defining the functions.
luchokoldo #3
Posted 14 March 2014 - 03:05 PM
Thank dude.. I move some vars and function and now work all ! Ty