Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

what's the usage of logical operator in this piece of code from corona cannon.
Started by Quitalizner Nov 25 2018 06:19 AM

2 replies to this topic
corona cannon logical operators

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Quitalizner

[GLOBAL: userInfoPane.html]
Quitalizner
  • Enthusiast

  • 30 posts
  • Corona SDK

I was going through the code in corona cannon and stumbled upon this. I tried to figure out the purpose of its usage. 

 

function cannon:getAmmoCount()
		return #balls + (self.ball and 1 or 0)
end

I ended up with the following error :- attempt to perform arithmetic on a table value. when i removed the or operator.

function cannon:getAmmoCount()
		return #balls + (self.ball and 1)
end

Can someone explain how self.ball a table value doesn't show error using the first approach, but does when i change it to the second approach. I have a vague idea what it could be, but i'm not sure.



[TOPIC: post.html]
#2

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,259 posts
  • Enterprise

  Best Answer

it's Lua's version of the ternary operator


  • roaminggamer likes this

[TOPIC: post.html]
#3

Quitalizner

[GLOBAL: userInfoPane.html]
Quitalizner
  • Enthusiast

  • 30 posts
  • Corona SDK

Haha, thanks a lot




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: corona cannon, logical operators