Raised This Month: $ Target: $400
 0% 

[ProblemSolved] Explanation for ? : on off menu cvar changer


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SubStream
Veteran Member
Join Date: Aug 2005
Location: USA
Old 04-02-2006 , 17:29   [ProblemSolved] Explanation for ? : on off menu cvar changer
Reply With Quote #1

I have read about 40 posts where xeroblood replied and read menu tutorials, but I have a specific question on how a certain part of a plugin works.

I am trying to understand how exactly the atac menu for atac_cfg plugin changes ON to OFF and vice versa.
Code:
new option = get_cvar_num("atac_options") format(smenu, 63, "%L\R1/2", id, "MENU_CFG_PTITLE") format(menuoption[0], 63, "\w1. %L\R\y%s^n", id, "MENU_CFG_SLAP", (option & (1<<0)) ? "ON" : "OFF")
1. I do not understand \R1/2 or what it does ???
2. I do not understand \R because \r is for red but what is \R ???

I understand that (1<<0) is key 1. I understand that it is checking to see if (1<<0) is a bit in option which is get_cvar_num("atac_options") which is = to 8183 in the atac.cfg file.

I do not see how it is checking the cvar to know whether to change the menu to display ON or OFF because I do not see how Key 1 is represented by the slap cvar.

Does this mean it is checking if Key 1 is a bit in 8183? Could someone explain this so I can understand it?

I also do not understand the ? after (option & (1<<0)) or the : between "ON" and "OFF". Are they operators? I do not understand what exactly they are and what exactly they do.

Does the ? mean to check if (option & (1<<0)) is true and then if it is true display "ON" and if false display "OFF"?

If someone could explain this code and how that works that would help me out.

Edit:

Thank you for the below explanations I appreciate it everyone.
SubStream is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 04-02-2006 , 17:52  
Reply With Quote #2

I guess \r is case insensitive - I might be wrong though.


But what I can explain is the ?: operator. It's the only ternary operator we have! It has the form:
Code:
a ? b : c

And this means...

If a evaluates to true (non-zero), the value of the expression is b. Otherwise, the value of the expression is c.

So,
Code:
a ? 10 : 5
stands for 10 if a is true and 5 if a is false. In your case:
Code:
(option & (1<<0)) ? "ON" : "OFF"
1<<0 is still 1 which is the lowest significant bit; so option & 1 will be only non-zero if the lowest significant bit in option is set (-> if the number is odd). Then, "ON" will be used. Otherwise, "OFF".
__________________
hello, i am pm
PM is offline
Basic-Master
Veteran Member
Join Date: Apr 2005
Location: hello pm
Old 04-02-2006 , 18:57  
Reply With Quote #3

hello pm!
yes, you are right, \r is case-sensitive. the meaning of the ? operator is pretty simple. it's like an if-condition but much smaller and you can use it for values only.. an example:

Code:
new gaben[64] gaben = (is_user_alive(alfred)) ? "oh, it's true, devouur!" : "NOES, damn"
this would set gaben to "oh, it's true, devouur!" if user alfred is alive. otherwise it'll be set to "NOES, damn"

you could use this, too, but nobody really likes such code parts:
Code:
new gaben[64] if (is_user_alive(alfred))   gaben = "oh, it's true, devouur!" else   gaben = "NOES, damn"

I hope I could clear that up for you.

-- Ga.. Basic-Master
Basic-Master is offline
Send a message via ICQ to Basic-Master Send a message via MSN to Basic-Master
Xanimos
Veteran Member
Join Date: Apr 2005
Location: Florida
Old 04-02-2006 , 23:40  
Reply With Quote #4

\r is for the color red \R is to format the rest of the text on that line to the right. (Notice how the 1/2 is on the right of the line?) and yes what they said is true above for the ? and : operators
Xanimos is offline
Send a message via AIM to Xanimos Send a message via MSN to Xanimos
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 04-03-2006 , 00:01  
Reply With Quote #5

Quote:
Originally Posted by Basic-Master
Code:
new gaben[64] if (is_user_alive(alfred))   gaben = "oh, it's true, devouur!" else   gaben = "NOES, damn"
Ahem
Code:
new gaben[64] if (is_user_alive(alfred))   copy(gaben , 63 , "oh, it's true, devouur!") else   copy(gaben , 63 , "NOES, damn")
Quote:
Originally Posted by Pawn Tutorial - AmWiki
You CANNOT do this! While it may compile, it is highly dangerous as it might cause overflow errors:

Code:
new myString[6] myString = "Hello"     //INVALID! myString[0] = "Hello"  //INVALID! //To add data to a string, you can do this: new goodString[7] copy(goodString, 6, "Hello")
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
Basic-Master
Veteran Member
Join Date: Apr 2005
Location: hello pm
Old 04-03-2006 , 02:11  
Reply With Quote #6

hello v3x

my code works as well as yours and it was only an example.. but yes, people should use your code then, it's bettAR
Basic-Master is offline
Send a message via ICQ to Basic-Master Send a message via MSN to Basic-Master
Reply



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

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

Forum Jump


All times are GMT -4. The time now is 16:43.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode