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

Storing string in cVar


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
GoldNux
Senior Member
Join Date: Mar 2018
Old 11-17-2022 , 19:22   Storing string in cVar
Reply With Quote #1

I would like to store strings in cVars, can someone show me how to do this properly?
new g_pCvarTest[32]?
g_pCvarTest = register_cvar("gn_test", "teststring")

Thanks a ton.
GoldNux is offline
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 11-17-2022 , 19:28   Re: Storing string in cVar
Reply With Quote #2

Quote:
Originally Posted by GoldNux View Post
I would like to store strings in cVars, can someone show me how to do this properly?
new g_pCvarTest[32]?
g_pCvarTest = register_cvar("gn_test", "teststring")

Thanks a ton.
what do you want to do exactly?, do you want to save "teststring" or "gn test"

PHP Code:
new g_pCvarTest[] = "teststring" 

register_cvar"gn_test"g_pCvarTest ); 
or u can do this:
PHP Code:
new g_pCvarTest[2][] =
{
   
"gn_test",
   
"teststring"
}

register_cvarg_pCvarTest[0], g_pCvarTest[1] ); 
like i said i don't know what you are trying to do exactly

Last edited by MrPickles; 11-17-2022 at 19:29.
MrPickles is offline
GoldNux
Senior Member
Join Date: Mar 2018
Old 11-17-2022 , 19:32   Re: Storing string in cVar
Reply With Quote #3

Quote:
Originally Posted by MrPickles View Post
what do you want to do exactly?, do you want to save "teststring" or "gn test"
I'm sorry for being unclear.
I would like to register a cvar that can store a string similar to the "name" command.
I need this so that users of my plugin can set which weapons to spawn with with a simple command.

for example I could change gn_round1 to "ak47".
Since the size of the string will change, I'm not sure how large to make the array.

Thanks again.

Last edited by GoldNux; 11-17-2022 at 20:20.
GoldNux is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-17-2022 , 19:57   Re: Storing string in cVar
Reply With Quote #4

Quote:
Originally Posted by MrPickles View Post
what do you want to do exactly?, do you want to save "teststring" or "gn test"

PHP Code:
new g_pCvarTest[] = "teststring" 

register_cvar"gn_test"g_pCvarTest ); 
or u can do this:
PHP Code:
new g_pCvarTest[2][] =
{
   
"gn_test",
   
"teststring"
}

register_cvarg_pCvarTest[0], g_pCvarTest[1] ); 
like i said i don't know what you are trying to do exactly
"pCvar" in the variable name means that it's a cvar pointer being stored in the variable.

Quote:
Originally Posted by GoldNux View Post
I'm sorry for being unclear.
I would like to register a cvar that can store a string similar to the "name" command.
I need this so that users of my plugin can set which weapons to spawn with with a simple command.

for example I could change gn_round1 to "ak47".
Since the size of the string will change, I'm not sure how large to make the array.

Thanks again.
You don't need to worry about the length until you go to read the cvar value. You read the cvar's string value with get_pcvar_string().
__________________

Last edited by fysiks; 11-17-2022 at 20:00.
fysiks is offline
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 11-17-2022 , 19:58   Re: Storing string in cVar
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
"pCvar" in the variable name means that it's a cvar pointer being stored in the variable.
He talks about strings, and the only ones I see are the ones inside the register_cvar, without details it's not very easy to guess what he really wants
"I'm sorry for being unclear."

Last edited by MrPickles; 11-17-2022 at 20:01.
MrPickles is offline
GoldNux
Senior Member
Join Date: Mar 2018
Old 11-17-2022 , 20:42   Re: Storing string in cVar
Reply With Quote #6

Quote:
Originally Posted by fysiks View Post
You don't need to worry about the length until you go to read the cvar value. You read the cvar's string value with get_pcvar_string().
I'm thinking I could parse the value and read multiple settings from one string.
If I figure out how to do that.

Something like this: "gn_gear" "weapon_ak47 weapon_usp flashbang flashbang hegrenade

What am I doing wrong here?
It prints nothing.
PHP Code:
    new testString[32]
    
get_pcvar_string(g_cVarTesttestvarcharsmax(g_cVarTest));
    
console_print(0"%s"testString
Thanks!

Last edited by GoldNux; 11-17-2022 at 20:50.
GoldNux is offline
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 11-17-2022 , 20:56   Re: Storing string in cVar
Reply With Quote #7

Quote:
Originally Posted by GoldNux View Post
I'm thinking I could parse the value and read multiple settings from one string.
If I figure out how to do that.

Something like this: "gn_gear" "weapon_ak47 weapon_usp flashbang flashbang hegrenade

What am I doing wrong here?
It prints nothing.
PHP Code:
    new testString[32]
    
get_pcvar_string(g_cVarTesttestvarcharsmax(g_cVarTest));
    
console_print(0"%s"testString
Thanks!

https://forums.alliedmods.net/showpo...18&postcount=5

forget the last server_print

Last edited by MrPickles; 11-17-2022 at 20:57.
MrPickles is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-17-2022 , 20:59   Re: Storing string in cVar
Reply With Quote #8

Quote:
Originally Posted by GoldNux View Post
I'm thinking I could parse the value and read multiple settings from one string.
If I figure out how to do that.

Something like this: "gn_gear" "weapon_ak47 weapon_usp flashbang flashbang hegrenade

What am I doing wrong here?
It prints nothing.
PHP Code:
    new testString[32]
    
get_pcvar_string(g_cVarTesttestvarcharsmax(g_cVarTest));
    
console_print(0"%s"testString
Thanks!
It's because you never actually populated the string. This is the basic string cvar usage:

PHP Code:
new g_pCvarMyString

public plugin_init()
{
    
g_pCvarMyString register_cvar("cvarName""Cvar Value String")
}

newFunction()
{
    new 
szString[32]
    
get_pcvar_string(g_pCvarMyStringszStringcharsmax(szString))
    
server_print("My Cvar String: %s"szString)

__________________
fysiks is offline
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 11-17-2022 , 21:04   Re: Storing string in cVar
Reply With Quote #9

GoldNux, i advises you that instead of using charsmax, you do the subtraction yourself, let's say the variable created has 32 cells, instead of charsmax(Array), you only put 31, you save an unnecessary operation, some use them to avoid having to do use of the head

charsmax = sizeof(Array) -1

PHP Code:
    new cvar,msg2[32
    
cvar register_cvar("admin_msg""ADMIN MSG")
    
get_pcvar_string(cvar,msg2charsmax(msg2)) 
=>

PHP Code:
    new cvar,msg2[32
    
cvar register_cvar("admin_msg""ADMIN MSG")
    
get_pcvar_string(cvar,msg231
Well, in the post that I sent you, the one who did it did understand the concept, very basic

Last edited by MrPickles; 11-17-2022 at 21:11.
MrPickles is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-17-2022 , 21:14   Re: Storing string in cVar
Reply With Quote #10

Hardcoding things like this is bad practice. Stop advising people to do unnecessary optimizations.

Quote:
Originally Posted by MrPickles View Post
some use them to avoid having to do use of the head
That's a dumb thing to say. It's used for code maintainability. This too is a magic number.
__________________

Last edited by fysiks; 11-17-2022 at 21:20.
fysiks 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:01.


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