lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


An obvious way that springs to mind is:

	function set(a,b) a=b return b end

	if set(s, project:FindSprite("Title")) ~= nil then 
	  print("we have a title") 
	end

Just out of interest the ~= nil is redundant. The following does the
same:

	s = project:FindSprite("Title") 
	if s then 
	  print("we have a title") 
	end

You might also try:

	> s = true and print("hello")
	hello
	> s = nil and print("hello")

i.e.
	s = project:FindSprite("Title") and print("we have a title")

http://lua-users.org/wiki/ExpressionsTutorial



-----Original Message-----
From: Ando Sonenblick [mailto:ando@spritec.com] 
Sent: Wednesday, November 19, 2003 12:40 PM
To: Lua list
Subject: inline conditional assignment possible?

Hi gang, 

I'm trying to assign a varible in an if statement and have its resulting
value be evaluated for the if. For example: 
Normal way: 

s = project:FindSprite("Title") 
if s ~= nil then 
print("we have a title") 
end 

New desired way: 

if (s = project:FindSprite("Title")) ~= nil then 
print("we have a title") 
end 

But lua always complains and I've tried all sorts of syntactical
variations. Is what I'm trying to do possible? 

thx, 
ando