AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [ProblemSolved] Explanation for ? : on off menu cvar changer (https://forums.alliedmods.net/showthread.php?t=26470)

SubStream 04-02-2006 17:29

[ProblemSolved] Explanation for ? : on off menu cvar changer
 
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.

PM 04-02-2006 17:52

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".

Basic-Master 04-02-2006 18:57

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

Xanimos 04-02-2006 23:40

\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

v3x 04-03-2006 00:01

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 :P
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")


Basic-Master 04-03-2006 02:11

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


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

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