Raised This Month: $51 Target: $400
 12% 

Solved switch/if q


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
lexzor
Veteran Member
Join Date: Nov 2020
Old 01-19-2021 , 03:29   switch/if q
Reply With Quote #1

hello again.

i have a problem with a part of code

PHP Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "super"

static key keys...

static 
aCvarValue

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_menu("randommenu"key"randommenu_handler")
}

public 
randommenu(id){
    
    
aCvarValue get_cvar_num("some_cvar")
    
    new 
szMenu[650], ilen
    ilen 
0
    
    ilen 
+= formatex(szMenu[ilen], charsmax(szMenu), "Menu^n^n")    
    
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r1. \wFirst menu item - %s^n"aCvarValue == "[ON]" "[OFF]")
    
}

public 
randommenu_handler(idkey){
    
    switch(
key):{
        
        case 
0:{    
            switch(
aCvarValue){        
                case 
1:{
                    
aCvarValue 0
                    
//code                
                
}
                
                case 
0:{
                    
aCvarValue 1
                    
//code
                
}
            }
        }
    }

this is just a simulation of what I really want to do. the problem is that switch of the variable "aCvarValue" because regardless of the situation, the output will be the last "case" of the switch. The same situation with "if". I don't understand why it doesn't work, I've done this before and it worked, but now I can't figure it out

Last edited by lexzor; 01-20-2021 at 08:48.
lexzor is offline
Old 01-19-2021, 06:51
hitD
This message has been deleted by hitD. Reason: My bad, it gets called once
LondoN
Senior Member
Join Date: Dec 2015
Location: Roman, Romania.
Old 01-19-2021 , 07:16   Re: switch/if q
Reply With Quote #2

aCvarValue > 0 ? some_function(id) : some_function2(id);
__________________
LondoN is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-19-2021 , 07:30   Re: switch/if q
Reply With Quote #3

The "randommenu" function is being called only once at map start, so of course that "get_cvar_num" is read only once as well.

Why are you using old-style menus in 2021?

aCvarValue = !aCvarValue ?

Also, modifying aCvarValue won't change the actual cvar. It's just a variable that stores its value. You need to use "set_cvar_num".
__________________

Last edited by OciXCrom; 01-19-2021 at 07:31.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-19-2021 , 22:22   Re: switch/if q
Reply With Quote #4

Agree with OciXCrom, if I understand correctly just do the below. This will always toggle on to off and off to on.
PHP Code:
public randommenu_handler(idkey)
{
    switch( 
key )
    {
        case 
0:
        {    
            
aCvarValue = !aCvarValue
            set_pcvar_num
cvarPtr aCvarValue);
        }
    }

__________________
Bugsy is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 01-20-2021 , 05:55   Re: switch/if q
Reply With Quote #5

I used london solution. Thanks you all!
lexzor is offline
Old 01-20-2021, 13:12
Natsheh
This message has been deleted by Natsheh. Reason: Nvm thread owner is stupid
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-20-2021 , 16:50   Re: switch/if q
Reply With Quote #6

LondoN's solution is not ideal here, it's what people typically do just to have less lines of code. While I'm a fan of ternary expressions, they should not replace an if-statement.

Example:

Does not make sense to do this:
Code:
( val == 5 ) ? Func1() : Func2()
It is better for readability to do this:

Code:
if ( val == 5 )
    Func1()
else
    Func2()
They are best used in this type of scenario. Here you can avoid having formatex() twice in your code, whereas in LonDon's example, there is no benefit other than reducing code at the cost of readability.

Code:
formatex( str , charsmax( str ) , "Hello %s" , val==1 ? "bugsy" : "someone else" )
__________________
Bugsy is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 01-21-2021 , 05:25   Re: switch/if q
Reply With Quote #7

Thanks bugsy!

what menu should i use and what benefits would be? idk what are the new menus

Last edited by lexzor; 01-21-2021 at 05:26.
lexzor is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-21-2021 , 08:02   Re: switch/if q
Reply With Quote #8

https://forums.alliedmods.net/showthread.php?t=46364
__________________
Bugsy is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 01-21-2021 , 11:45   Re: switch/if q
Reply With Quote #9

is this required or i can simply use that method from first post ?

i mean when i was trying to use menu_additem the numbers had no colors. like the code was

"1. First item"

and i want to do something like this

"\r1. \wFirst item"

can i do this ?

EDIT: I seen that i can use this:

PHP Code:
 menu_setpropiMenuMPROP_NUMBER_COLOR"\y" ); 
Thanks!

But how can i add an item that don't have any number?

Last edited by lexzor; 01-21-2021 at 11:53.
lexzor is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 01-21-2021 , 12:59   Re: switch/if q
Reply With Quote #10

Quote:
But how can i add an item that don't have any number?
Either by using menu_addtext or breaking the line with the ^n just like the code below

Code:
menu_additem(menu, "Item 1^nItem 2")
__________________








CrazY. is offline
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 12:29.


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