Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

what ? and : means

return arm[@id] != nil && arm[@id]['rdmg'] != nil ? arm[@id]['rdmg'] : 0

im talking about the last ? arm[@id]['rdmg'] : 0, how they work?
 

fbu

Member

I guess it's the ID of a certain weapon, for example, of a dragon sword with the ID 3 and the damage you want to have.

I don't know RGSS, but It must be something like that.

Bugs! I mean, Hugs! :haha:
 
return arm[@id] != nil && arm[@id]['rdmg'] != nil ? arm[@id]['rdmg'] : 0

If red is true, return blue, otherwise return green.

Basically, if the armour exists, return the armour's rdmg; otherwise return 0.

Just a guess but it seems pretty logical to me.
 
This is actually a conditional operator. Basically, it's in this format:
Code:
condition ? var1 : var2
In that event, it would return var1 if the condition is true, and it would return var2 otherwise. It could be rewritten this way:
Code:
if condition

  return var1

else

  return var2

end
The cool thing about this operator is that it can be used to set variables dynamically. It can also be nested within itself, so that you have something like this:
Code:
(((var1 == var2) ? var3 : var4) == var5) ? var6 : var 7
In that example, if var1 is equal to var2, and var3 is equal to var5, it returns var6. If var1 is not equal to var2, and var4 is equal to var5, it returns var6. If var1 is equal to var2 and var3 is not equal to var5, then it returns var7. And finally, if var1 is not equal to var2, and var4 is not equal to var5, it would also return var7.
 
Just making sure that everyone definately understands what's going on, lemme point out the fact that return is actually an element to improve readability of a script in Ruby, however not necessarily needed. Therefore, you could leave them all out, which Glitch did in the first code block. With return, it'd look like this:
[rgss]condition ? return var1 : return var2
[/rgss]
That should make more sense if you see the second code block of Glitch as a rewrite of the first.

Another thing I'd like to mention about these operators is that you can actually do both of these:
[rgss]@variable == 0 ? @variable = 1 : @variable = 0
[/rgss]
[rgss]@variable = @variable == 0 ? 1 : 0
[/rgss]

And last but not least, while the appliance Glitch mentioned last is perfectly valid, you shouldn't overdo that kind of wrapping-code-into-itself... try keeping a clear style that you and others can easily read!
 
Then why this is returning nil?
return Improved_weapons[@id]['exp_mod'] if Improved_weapons[@id] != nil and Improved_weapons[@id]['exp_mod'] != nil ? Improved_weapons[@id]['exp_mod'] : 0

I want to return 0 if the array has nothing.
 
You have to use brackets or just a new line:
Code:
weapon = Improved_weapons[@id]

if weapon != nil

 return weapon['exp_mod'].nil? ? 0 : weapon['exp_mod']

end

In this case you can also use the || operator.
Code:
weapon = Improved_weapons[@id]

if weapon != nil

 return weapon['exp_mod'] || 0

end
 

Atoa

Member

Code:
return Improved_weapons[@id]['exp_mod'] if Improved_weapons[@id] != nil and Improved_weapons[@id]['exp_mod'] != nil ? Improved_weapons[@id]['exp_mod'] : 0

You confusing things...

the "?" itself IS an condition. So you're chekingan condition against the result of other condition

If the condition is false/nil you're doing
return value1 if 0
Makes sense? No.
because? because the condition ? value1 : value2 returns the result.

doing this will do the trick:
return condition ? value1 : value2

Off-Topic:
return arm[@id] != nil && arm[@id]['rdmg'] != nil ? arm[@id]['rdmg'] : 0
I Know this code : D
 
By the way, if we are talking about style:
For Hashes with "exp_mod", "rdmg" or any string as keys you better should youse symbols :)exp_mod, :rdmg).
They are a bit faster and it's a better coding style since it also occures like this in Ruby 1.9
 
Neo-Bahamut":9qgswlrk said:
By the way, if we are talking about style:
For Hashes with "exp_mod", "rdmg" or any string as keys you better should youse symbols :)exp_mod, :rdmg).
They are a bit faster and it's a better coding style since it also occures like this in Ruby 1.9

ok, i didnt know that
 
Haha, just to clarify the clarifications, the ? : operator is called the ternary operator. It is in the form of boolean ? consequence : alternative

If boolean is true, evaluate consequence, else evaluate alternative. Note that this is NOT returning anything, it runs it just like code. You can do, for example

bool_variable ? function_if_true() : function_if_false();


It's basically a shorthand of
if(bool_variable){
function_if_true();
}else{
function_if_false();
}


However, since that fits in one line, and it will be evaluated in place, not as a seperate block, the ternary operator is more than a shorthand of if-else. For example, consider the following:

result = bool_variable ? function_if_true() : function_if_false();


Notice how you cannot do that with an if-else:

result = 
if(bool_variable){
function_if_true();
}else{
function_if_false();
}


will not work
 
Oh, nice, I forget about some of those things Ruby does. Well, historically, what I described is one of the benefits of ternary. I guess in Ruby it really is just a short hand if-then-else.
 

Zeriab

Sponsor

It's funny that ? : is called the ternary operator when a ternary operator is just an operator which takes arguments. Since if-then-else can be used like an operator you can also it as a ternary operator. I wouldn't recommend it since that would probably lead to confusion.

It's some syntactic sugar which can be used for brevity in some cases.
It can sometimes lead to more confusing code and nesting ternary operators is rarely a good idea.


P.s. nice discussion guys :3

*hugs*
 
Yeah, right, haha.

Zeriab, I'mma let you finish but ?: is the best ternary operator of all time. OF ALL TIME.

Haha, but yeah, in Ruby it appears that ?: is just sugar, but in C++ there's actually functionality differences. I think that's just important to note, so people don't think "Oh, I know ifthenelse I don't need the ternary operator" The ternary operator is quite useful in C++
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top