Thread Tools Display Modes
04-18-10, 01:43 AM   #1
Ulfsaar
A Defias Bandit
Join Date: Apr 2010
Posts: 3
Simple code I cant get to work + saved variable

Hey I'm trying to teach myself lua and im creating my first addon but I can't get this to work.
Code:
function Begin()
	if variable ~= nil then
		print(variable)
	else
		print("Type /set <text>")
	end
end
This always goes straight to print("Type /set <text>") instead of print(variable). When I type /script print(variable) it comes up fine, though so I think the problem is with this function.
  Reply With Quote
04-18-10, 01:55 AM   #2
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Check what value your variable has when the function is called.
Add something like this as first line in your function.
Code:
print(type(variable),variable, variable ~= nil)
Then you'll know why the interpreter decided to switch to the 'else'.
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
04-18-10, 01:59 AM   #3
Ulfsaar
A Defias Bandit
Join Date: Apr 2010
Posts: 3
Shouldn't the variable come up as what I set it to before though?
  Reply With Quote
04-18-10, 03:06 AM   #4
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Sure, it SHOULD
But it looks that it doesnt ... looking at the code I see no error.
That means your variable ( is it global or local ? ) is probably not what
you expect it to be.
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
04-18-10, 03:54 AM   #5
Vilkku
An Aku'mai Servant
 
Vilkku's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 35
I'm a bit of a novice myself, but I'd do it the following way:

Code:
function Begin()
	if variable then
		print(variable)
	else
		print("Type /set <text>")
	end
end
Again, the first code you posted should work. Like the poster before me, check your variables.
  Reply With Quote
04-18-10, 06:19 AM   #6
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,961
So, are you saying that if you type print(variable) it prints a value ? Or does it show up as nil ?

As has been said, your code should work fine as it is.

What are the lines of code before the function is executed ?
As has been asked, is variable a global value ?

With your code it will only print(variable) if your variable contains a value. Hence my first question. If the variable hasn't been initialised prior to the functions execution then it will be nil so hence the else execution.

variable = nil .... go to else block...
variable = anything else go to if block ...

Vikku's example however will do something slightly different.

variable = zero, false, nil then go to else block
variable = true, non zero, any other value then go to if block

So yes, first case scenario, before the if test put that plain print statement in to see what the if test is seeing and it should answer your question.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
04-18-10, 11:35 AM   #7
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
When are you checking for the value of variable? If you are checking before game has loaded your saved variables for your addon, then this is why that it is nil.

http://www.wowwiki.com/Events_that_f...oading_Process
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
04-18-10, 01:11 PM   #8
Ulfsaar
A Defias Bandit
Join Date: Apr 2010
Posts: 3
Originally Posted by Seerah View Post
When are you checking for the value of variable? If you are checking before game has loaded your saved variables for your addon, then this is why that it is nil.

http://www.wowwiki.com/Events_that_f...oading_Process
I checked the variable many times it was fine. If this is true then this is probably the problem, it is loading instantly on log in.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Simple code I cant get to work + saved variable


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off