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

Check if variable exists and edit problems

Started by OczkoSX, 01 May 2014 - 09:41 AM
OczkoSX #1
Posted 01 May 2014 - 11:41 AM
Hi! I'm doing little spam on foum, sry for that :D/>

How can I check if variable isnt nil ?

Here is my code: http://pastebin.com/07szXe0i

I'm saying about Tank() function.
I tried if variable == nil but it doesnt work

The second problem is terminating program
When i am in tank mode ($$tank show) and i terminate program, i cant edit it. It says: edit:60: attempt to call nil
I have to reboot PC then.

EDIT: I need checking var because i saw, when tank is empty, his liquid amount is nil, so table[1].amount is nil and program crashes
Edited on 01 May 2014 - 09:42 AM
CometWolf #2
Posted 01 May 2014 - 12:03 PM

if variable then
  --not nil
else
  --nil
end
AS for your other problem, you're overwriting the global table functions with this line

    table = tank_p.getTankInfo("unknown")
the word table is already in use by the global environment, to store all table functions. Either make it a local definition or change the name of your table variable.
OczkoSX #3
Posted 01 May 2014 - 12:20 PM
Thank you! Is it possible to do:

if variable then
–nil
end

?

i tried ~variable but it doesnt work
OczkoSX #4
Posted 01 May 2014 - 12:29 PM
Ok, i've got it, just use not var :D/>
HometownPotato #5
Posted 02 May 2014 - 03:54 AM
Well, if variable then checks if it is not nil and not false. If your variable may be false do: if variable ~= nil then
Bomb Bloke #6
Posted 02 May 2014 - 05:57 AM
OczkoSX was saying he figured out "if not variable then" does the trick.
HometownPotato #7
Posted 02 May 2014 - 06:10 AM
That still gives you the same problem, if it's false or nil then it not var will be true
OczkoSX #8
Posted 02 May 2014 - 08:44 AM
I said I know, just have to use

if not var then