Raised This Month: $ Target: $400
 0% 

How to make sure a variable is not unset


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Cd5ssmffan
BANNED
Join Date: Feb 2017
Location: GB
Old 06-18-2018 , 16:25   How to make sure a variable is not unset
Reply With Quote #1

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

Last edited by Cd5ssmffan; 06-18-2018 at 16:26.
Cd5ssmffan is offline
Send a message via ICQ to Cd5ssmffan Send a message via AIM to Cd5ssmffan Send a message via Yahoo to Cd5ssmffan Send a message via Skype™ to Cd5ssmffan
maqi
Senior Member
Join Date: Apr 2017
Location: Serbia
Old 06-18-2018 , 16:29   Re: How to make sure a variable is not unset
Reply With Quote #2

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 )
__________________
stuff

Last edited by maqi; 06-18-2018 at 16:29.
maqi is offline
Cd5ssmffan
BANNED
Join Date: Feb 2017
Location: GB
Old 06-18-2018 , 16:44   Re: How to make sure a variable is not unset
Reply With Quote #3

Quote:
Originally Posted by maqi View Post
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?
Cd5ssmffan is offline
Send a message via ICQ to Cd5ssmffan Send a message via AIM to Cd5ssmffan Send a message via Yahoo to Cd5ssmffan Send a message via Skype™ to Cd5ssmffan
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 06-18-2018 , 16:47   Re: How to make sure a variable is not unset
Reply With Quote #4

Show more code. A variable can be used for anything. It depends if you need a number, float, string, etc.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
maqi
Senior Member
Join Date: Apr 2017
Location: Serbia
Old 06-18-2018 , 16:59   Re: How to make sure a variable is not unset
Reply With Quote #5

To check if a cvar is truly unset you should probably get_pcvar_string and check what i told you before.
__________________
stuff
maqi is offline
Cd5ssmffan
BANNED
Join Date: Feb 2017
Location: GB
Old 06-18-2018 , 17:30   Re: How to make sure a variable is not unset
Reply With Quote #6

Quote:
Originally Posted by maqi View Post
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.
Cd5ssmffan is offline
Send a message via ICQ to Cd5ssmffan Send a message via AIM to Cd5ssmffan Send a message via Yahoo to Cd5ssmffan Send a message via Skype™ to Cd5ssmffan
maqi
Senior Member
Join Date: Apr 2017
Location: Serbia
Old 06-18-2018 , 17:37   Re: How to make sure a variable is not unset
Reply With Quote #7

That doesn't make sense, show the code please.
__________________
stuff
maqi is offline
Cd5ssmffan
BANNED
Join Date: Feb 2017
Location: GB
Old 06-18-2018 , 17:42   Re: How to make sure a variable is not unset
Reply With Quote #8

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

Last edited by Cd5ssmffan; 06-19-2018 at 15:50.
Cd5ssmffan is offline
Send a message via ICQ to Cd5ssmffan Send a message via AIM to Cd5ssmffan Send a message via Yahoo to Cd5ssmffan Send a message via Skype™ to Cd5ssmffan
maqi
Senior Member
Join Date: Apr 2017
Location: Serbia
Old 06-18-2018 , 17:52   Re: How to make sure a variable is not unset
Reply With Quote #9

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.
__________________
stuff

Last edited by maqi; 06-18-2018 at 17:59.
maqi is offline
Cd5ssmffan
BANNED
Join Date: Feb 2017
Location: GB
Old 06-18-2018 , 18:05   Re: How to make sure a variable is not unset
Reply With Quote #10

Quote:
Originally Posted by maqi View Post
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 View Post
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?
Cd5ssmffan is offline
Send a message via ICQ to Cd5ssmffan Send a message via AIM to Cd5ssmffan Send a message via Yahoo to Cd5ssmffan Send a message via Skype™ to Cd5ssmffan
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:25.


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