Jump to content




Check if variable exists and edit problems


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

#1 OczkoSX

  • Members
  • 29 posts

Posted 01 May 2014 - 10: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 by OczkoSX, 01 May 2014 - 10:42 AM.


#2 CometWolf

  • Members
  • 1,283 posts

Posted 01 May 2014 - 11:03 AM

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.

#3 OczkoSX

  • Members
  • 29 posts

Posted 01 May 2014 - 11:20 AM

Thank you! Is it possible to do:

if variable then
--nil
end

?

i tried ~variable but it doesnt work

#4 OczkoSX

  • Members
  • 29 posts

Posted 01 May 2014 - 11:29 AM

Ok, i've got it, just use not var :D

#5 HometownPotato

  • Members
  • 62 posts

Posted 02 May 2014 - 02: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

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 02 May 2014 - 04:57 AM

OczkoSX was saying he figured out "if not variable then" does the trick.

#7 HometownPotato

  • Members
  • 62 posts

Posted 02 May 2014 - 05:10 AM

That still gives you the same problem, if it's false or nil then it not var will be true

#8 OczkoSX

  • Members
  • 29 posts

Posted 02 May 2014 - 07:44 AM

I said I know, just have to use
if not var then






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users