Raised This Month: $ Target: $400
 0% 

Const strings info


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Crackhead69
Member
Join Date: Feb 2021
Old 04-01-2021 , 09:01   Const strings info
Reply With Quote #1

Hello, i've been searching and had no luck finding what im looking for.
For best understanding i will show a script with my similar to desired outcome.

PHP Code:
new const HealthItems[][] ={
    
"120",
    
"150",
    
"50"
}

new 
ItemNumber
new ItemHolder

public plugin_init() {
    
register_clcmd("say /test","set_id_health")
    
register_clcmd("say /increase","increase_const_number")
}

public 
set_id_health(id){
    for(new 
i;i<sizeof (HealthItems);i++){
        
ItemHolder HealthItems[ItemNumber][i]
    }
    
    
set_user_health(id,ItemHolder)
}

public 
increase_const_number(id){
    
ItemNumber++

By the looks of the code in my brain should work accurately, although it does not.
The numbers that i am getting are not the same as the written ones in the strings.

I was not able to find much information on constants and im not sure if that's even possible.
I also wasn't able to find much info about str_to_num function, which might be the solution judging by its description.
Crackhead69 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-01-2021 , 09:57   Re: Const strings info
Reply With Quote #2

Use an array of numbers instead of strings. Your code was accessing the ascii value of a character in the string.
PHP Code:
new const HealthItems[] ={
    
120,
    
150,
    
50
}

new 
ItemNumber
new ItemHolder

public plugin_init() {
    
register_clcmd("say /test","set_id_health")
    
register_clcmd("say /increase","increase_const_number")
}

public 
set_id_health(id){
    for(new 
i;i<sizeof (HealthItems);i++){
        
ItemHolder HealthItems[ItemNumber]
    }
    
    
set_user_health(id,ItemHolder)
}

public 
increase_const_number(id){
    
ItemNumber++

__________________
Bugsy is offline
Crackhead69
Member
Join Date: Feb 2021
Old 04-01-2021 , 10:08   Re: Const strings info
Reply With Quote #3

Yes, although if i want to keep it as a string, is it possible to get the string as it is as number, or is my only option to build it like so
PHP Code:
enum _:ItemsOrder{
    
Name[32],
    
Amount
}

new const 
Healthitems[][ItemsOrder] =
{
    {
"100"100},
    {
"50"50},
    {
"20"20}


Last edited by Crackhead69; 04-01-2021 at 10:09.
Crackhead69 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-01-2021 , 11:00   Re: Const strings info
Reply With Quote #4

No need to have both an integer and string. In your original code, do:

PHP Code:
public set_id_health(id)
{
    
set_user_health(idstr_to_numHealthItems[ItemNumber] ) )

You should add more logic in increase_const_number() so you don't get an index out of bounds error.
__________________

Last edited by Bugsy; 04-01-2021 at 11:13.
Bugsy is offline
Crackhead69
Member
Join Date: Feb 2021
Old 04-01-2021 , 11:44   Re: Const strings info
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
No need to have both an integer and string. In your original code, do:

PHP Code:
public set_id_health(id)
{
    
set_user_health(idstr_to_numHealthItems[ItemNumber] ) )

You should add more logic in increase_const_number() so you don't get an index out of bounds error.
Ooooh.. damn..

Thanks you!

As you mentioned tho, what do you mean by logic?
I figure i just don't allow the array get a number which is out of its bounds, yes?
Crackhead69 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-01-2021 , 14:18   Re: Const strings info
Reply With Quote #6

Right

PHP Code:
if ( ItemNumber++ == sizeofHealthItems ) )
     
ItemNumber 0

// Or

ItemNumber = ++ItemNumber sizeofHealthItems 
__________________

Last edited by Bugsy; 04-01-2021 at 14:27.
Bugsy is offline
Crackhead69
Member
Join Date: Feb 2021
Old 04-01-2021 , 14:32   Re: Const strings info
Reply With Quote #7

Got it!
Thank you for the help
Crackhead69 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 02:40.


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