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

how can you scan a variable and then apply it?


Post New Thread Reply   
 
Thread Tools Display Modes
Whai
Senior Member
Join Date: Jul 2018
Old 07-09-2019 , 15:39   Re: how can you scan a variable and then apply it?
Reply With Quote #11

Quote:
Originally Posted by Doggg View Post
ofc its not 32 chars because that will be stupid. but what is it?
Hum... Actually it IS.


btw, here is a guide for you to understand. (this can also help you)
__________________
Whai is offline
Doggg
Member
Join Date: Jun 2018
Old 07-09-2019 , 16:12   Re: how can you scan a variable and then apply it?
Reply With Quote #12

Quote:
Originally Posted by Whai View Post
Hum... Actually it IS.


btw, here is a guide for you to understand. (this can also help you)
hahahahahaha, i thought there is some logic behind this.
Thanks, I will look at the guide.
wait, so char in the size of 4 woudlnt be enough for mp restart 15 for example? i mean i am not planning to enter a 32 length number for the mp restart command.

Last edited by Doggg; 07-09-2019 at 16:12.
Doggg is offline
Whai
Senior Member
Join Date: Jul 2018
Old 07-09-2019 , 16:29   Re: how can you scan a variable and then apply it?
Reply With Quote #13

Well,

char value[4] is an array of char (char is the abbreviation of characters) with the size of 4 but there is an exception only on arrays of char.

First, index of arrays go from 0 to the size - 1, that means if you create an array with the size of 4 (meaning you'll have 4 places to store item) : it will go from 0 to 3.
However, on arrays of char (in sourcemod or maybe others programming languages) the last index is reserved to place the "\0" --> it means the end of the "word".

By creating an array of char with the length of 4, you will have 3 free places.

In conclusion, yes : if you write "15" you need at least 2 free places.

Note : I wrote all of this to make this for you understand roughly speaking how works array in SM
__________________

Last edited by Whai; 07-09-2019 at 16:32. Reason: readable
Whai is offline
Doggg
Member
Join Date: Jun 2018
Old 07-09-2019 , 16:49   Re: how can you scan a variable and then apply it?
Reply With Quote #14

Quote:
Originally Posted by Whai View Post
Well,

char value[4] is an array of char (char is the abbreviation of characters) with the size of 4 but there is an exception only on arrays of char.

First, index of arrays go from 0 to the size - 1, that means if you create an array with the size of 4 (meaning you'll have 4 places to store item) : it will go from 0 to 3.
However, on arrays of char (in sourcemod or maybe others programming languages) the last index is reserved to place the "\0" --> it means the end of the "word".

By creating an array of char with the length of 4, you will have 3 free places.

In conclusion, yes : if you write "15" you need at least 2 free places.

Note : I wrote all of this to make this for you understand roughly speaking how works array in SM
Thanks for the time you put into this, I knew most of this, It is very similar to C (at least i think it is) you have any idea why the guy here put 32 in the code? 32 looks bit too much for me. i mean i think the max is going to be 5 (this is means i can put up to 1000 right?, 4 free spaces.

Last edited by Doggg; 07-09-2019 at 16:53.
Doggg is offline
Whai
Senior Member
Join Date: Jul 2018
Old 07-09-2019 , 17:01   Re: how can you scan a variable and then apply it?
Reply With Quote #15

Quote:
Originally Posted by Doggg View Post
you have any idea why the guy here put 32 in the code? 32 looks bit too much for me.
I don't know, maybe he was thinking about float value

Quote:
Originally Posted by Doggg View Post
I knew most of this
Ah yes I forgot. Good then, if there is errors I did : you will not be affected
__________________

Last edited by Whai; 07-10-2019 at 04:49.
Whai is offline
I am inevitable
Member
Join Date: May 2019
Location: 0xA6DA34
Old 07-09-2019 , 19:50   Re: how can you scan a variable and then apply it?
Reply With Quote #16

Don’t use ServerCommand.
__________________
I do make plugins upon requests, so hit me up on discord if you're interested: Stefan Milivojevic#5311
I am inevitable is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 07-10-2019 , 05:30   Re: how can you scan a variable and then apply it?
Reply With Quote #17

Quote:
Originally Posted by I am inevitable View Post
Don’t use ServerCommand.
Saying "Don't use ServerCommand" is like saying "Don't use SQL". It could use a better explanation.

What you mean to say is "Don't use ServerCommand with unsanitized, untrusted input because someone can easily throw in a ; quit; // and ruin your day".

Of course, if there's a better, safer way to do it, then that'd be preferable.

If you're expecting a number, use StringToInt and use the %d format specifier. IF you're expecting a convar, make sure it exists with FindConVar, and so on.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
I am inevitable
Member
Join Date: May 2019
Location: 0xA6DA34
Old 07-10-2019 , 09:47   Re: how can you scan a variable and then apply it?
Reply With Quote #18

Quote:
Originally Posted by nosoop View Post
Saying "Don't use ServerCommand" is like saying "Don't use SQL". It could use a better explanation.

What you mean to say is "Don't use ServerCommand with unsanitized, untrusted input because someone can easily throw in a ; quit; // and ruin your day".

Of course, if there's a better, safer way to do it, then that'd be preferable.

If you're expecting a number, use StringToInt and use the %d format specifier. IF you're expecting a convar, make sure it exists with FindConVar, and so on.
Yeah, you're completely right. My bad.

Here's a way of safely setting convar values (strings in this case):

PHP Code:
void something()
{
    
// find the convar
    
ConVar hConVar FindConVar("");
    
    
SetConvarStringSafe(hConVarsString);
}

bool SetConvarStringSafe(ConVar hConVarchar[] sString)
{
    
// if the convar is invalid, then don't do anything
    
if (hConVar == null)
        return 
false;
    
    else
        
hConVar.SetString(sString);
    
    return 
true;

__________________
I do make plugins upon requests, so hit me up on discord if you're interested: Stefan Milivojevic#5311
I am inevitable is offline
Whai
Senior Member
Join Date: Jul 2018
Old 07-10-2019 , 10:19   Re: how can you scan a variable and then apply it?
Reply With Quote #19

Quote:
Originally Posted by I am inevitable View Post
~snip~
In this case, we do not need your code because we already know that ConVar (mp_restartgame) exist

PHP Code:

public Action rr(int iClientint iArgs) {
    if(
iArgs1) { 
        
ReplyToCommand(client"[SM] Usage: sm_rr <seconds>"); 
        return 
Plugin_Handled
    } 
    
char strValue[8]; // 8 just in case of float value

    
GetCmdArg(1strValuesizeof(strValue));
    
float fValue StringToFloat(strValue);

    if(!(!
fValue)) //if fValue is not equal to 0.0
        
ServerCommand("mp_restartgame %0.2f"fValue); // 0.2 meaning 2 digits after the decimal point
    
    
else
    {
        
ReplyToCommand(iClient"[SM] Invalid value entered !");
        return 
Plugin_Handled
    
}
    return 
Plugin_Handled;  

If you are asking why I changed variables' name : because it is more easy to debug for me
Just in case if you are interested
__________________

Last edited by Whai; 07-10-2019 at 10:20.
Whai is offline
Doggg
Member
Join Date: Jun 2018
Old 07-10-2019 , 13:57   Re: how can you scan a variable and then apply it?
Reply With Quote #20

Quote:
Originally Posted by Whai View Post
In this case, we do not need your code because we already know that ConVar (mp_restartgame) exist

PHP Code:

public Action rr(int iClientint iArgs) {
    if(
iArgs1) { 
        
ReplyToCommand(client"[SM] Usage: sm_rr <seconds>"); 
        return 
Plugin_Handled
    } 
    
char strValue[8]; // 8 just in case of float value

    
GetCmdArg(1strValuesizeof(strValue));
    
float fValue StringToFloat(strValue);

    if(!(!
fValue)) //if fValue is not equal to 0.0
        
ServerCommand("mp_restartgame %0.2f"fValue); // 0.2 meaning 2 digits after the decimal point
    
    
else
    {
        
ReplyToCommand(iClient"[SM] Invalid value entered !");
        return 
Plugin_Handled
    
}
    return 
Plugin_Handled;  

If you are asking why I changed variables' name : because it is more easy to debug for me
Just in case if you are interested
now i need you to treat me like a dummy xD
what ConVar is? i have no idea. and why in this code you went on float?
Doggg 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 14:34.


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