AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How can I turn on and off a function on the best way? (https://forums.alliedmods.net/showthread.php?t=244549)

proffs 07-20-2014 05:24

How can I turn on and off a function on the best way?
 
How can I turn on and off a function on the best way?

For example those noclip and godmode toggles on block makers.
How can I on the best way make a toggle?

Where I can in a menu turn on and off a function.

Thanks

mottzi 07-20-2014 11:44

Re: How can I turn on and off a function on the best way?
 
you can do it with something like this:
PHP Code:

new bool:g_Noclip[33]

public 
client_connect(id)
{
    
g_Noclip[id] = false
}

public 
menu_show(id
{
    new 
menu menu_create("testmenu""handler")
    
    
menu_additem(menug_Noclip[id] ? "Noclip (Deactivate)" "Noclip (Activate)")
    
    
menu_display(id)
}

public 
handler(idmenuitem)
{
    if(
item == 0)
    {
        
g_Noclip[id] = !g_Noclip[id]
        
        
set_user_noclip(idg_Noclip[id] ? 0)
    }
    
    
menu_destroy(menu)
    return 
PLUGIN_HANDLED



Bos93 07-20-2014 12:21

Re: How can I turn on and off a function on the best way?
 
mottzi, you can change g_Noclip to get_user_noclip ;-)

Flick3rR 07-20-2014 12:24

Re: How can I turn on and off a function on the best way?
 
Quote:

Originally Posted by Bos93 (Post 2170893)
mottzi, you can change g_Noclip to get_user_noclip ;-)

This is true, but only for the current example, that mottzi gave. It was about the way you can detect and turn on/off the function. And actually, the best way is with bool, because it has only two values, which you can easily reverse in this way. (bool[id] = !bool[id])

NikKOo31 07-20-2014 14:59

Re: How can I turn on and off a function on the best way?
 
Quote:

Originally Posted by Flick3rR (Post 2170895)
This is true, but only for the current example, that mottzi gave. It was about the way you can detect and turn on/off the function. And actually, the best way is with bool, because it has only two values, which you can easily reverse in this way. (bool[id] = !bool[id])

PHP Code:

set_user_noclip(id, !get_user_noclip(id)) 

:crab:

Flick3rR 07-20-2014 15:28

Re: How can I turn on and off a function on the best way?
 
Guys... We are talking about the way to do it. It may not be a single native, but whole algorhytm of functions. I think he was asking how to return the current value and detect it's reversing.

mottzi 07-20-2014 21:46

Re: How can I turn on and off a function on the best way?
 
As Flicker correctly mentioned, I was only giving an exapmle of how you'd do something like this - of course you don't need to store the state in a variable when premade functions are availiable.

Flicker, you can also use the "!"-Operator with integer-values.


All times are GMT -4. The time now is 13:06.

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