AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to make sure a variable is not unset (https://forums.alliedmods.net/showthread.php?t=308393)

Cd5ssmffan 06-18-2018 16:25

How to make sure a variable is not unset
 
Code:

g_blah = register_cvar("amx_blah","")

...

if (g_blah == "") {
//cvar is empty
}
else
{
//cvar has been set
}

How do you check if g_blah is actually unset here in a config? I've tried this method but I am getting error 033: array must be indexed <variable "-unknown-">

maqi 06-18-2018 16:29

Re: How to make sure a variable is not unset
 
Code:
if( !g_blah[0] )
or
Code:
if( equal( g_blah, "" ) )

These should work. ( You might wanna trim before the first one, so better use the second one )

Cd5ssmffan 06-18-2018 16:44

Re: How to make sure a variable is not unset
 
Quote:

Originally Posted by maqi (Post 2597805)
Code:
if( equal( g_blah, "" ) )

Thanks a lot. Just had an "argument mismatch" error and realised I was doing get_pcvar_num on this beforehand. I can't find anything related to variables in get_pcvar.. Do I need to use get_pcvar_string?

OciXCrom 06-18-2018 16:47

Re: How to make sure a variable is not unset
 
Show more code. A variable can be used for anything. It depends if you need a number, float, string, etc.

maqi 06-18-2018 16:59

Re: How to make sure a variable is not unset
 
To check if a cvar is truly unset you should probably get_pcvar_string and check what i told you before.

Cd5ssmffan 06-18-2018 17:30

Re: How to make sure a variable is not unset
 
Quote:

Originally Posted by maqi (Post 2597811)
To check if a cvar is truly unset you should probably get_pcvar_string and check what i told you before.

I can't put "" into a string, it tells me that the array index is out of bounds.

maqi 06-18-2018 17:37

Re: How to make sure a variable is not unset
 
That doesn't make sense, show the code please.

Cd5ssmffan 06-18-2018 17:42

Re: How to make sure a variable is not unset
 
Code:

snip
I tried to make it as short as possible before, oh and g_blah is g_custom_text

maqi 06-18-2018 17:52

Re: How to make sure a variable is not unset
 
Code:
g_custom_text[32] = register_cvar("sa_custom_text","")

This is wrong. register_cvar always returns an integer and it's a cvar pointer to a cvar, which you can use to get_pcvar.

Along with that

Code:
get_pcvar_string(g_custom_text[32], g_custom_text, 32)

This is wrong. First of all the first argument is a cvar pointer you got from registering a cvar, in your case this throws an error.

Also length should be sizeof - 1 ( In your case 31, or just use charsmax(buffer) )

This native returns a number of cells written to a buffer, so keep that in mind.

Cd5ssmffan 06-18-2018 18:05

Re: How to make sure a variable is not unset
 
Quote:

Originally Posted by maqi (Post 2597827)
Code:
g_custom_text[32] = register_cvar("sa_custom_text","")
This is wrong. register_cvar always returns an integer and it's a cvar pointer to a cvar, which you can use to get_pcvar.

Can I fix it by using create_cvar instead?

Quote:

Originally Posted by maqi (Post 2597827)
Code:
get_pcvar_string(g_custom_text[32], g_custom_text, 32)
This is wrong. First of all the first argument is a cvar pointer you got from registering a cvar, in your case this throws an error.

Also length should be sizeof - 1 ( In your case 31, or just use charsmax(buffer) )

This native returns a number of cells written to a buffer, so keep that in mind.

How do I fix this?


All times are GMT -4. The time now is 12:25.

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