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

how can you scan a variable and then apply it?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Doggg
Member
Join Date: Jun 2018
Old 07-09-2019 , 11:37   how can you scan a variable and then apply it?
Reply With Quote #1

Hey dudes, I am trying to write a code that change the sv cheats as i wish.
i mean like, i am going to write /cheats 1 and sv_cheats will change to 1;
so i try to scan from the user this info and then apply it into the code.
what is the scanning function called? how to use it? i mean in C you just scanf("%+letter",&variable)
also, i want this to print a fitting message to the client,
if the variable = 1 to print in GREEN svcheats is *on*
if the variable = 1 to print in RED svcheats is *off*
i have 0 idea how i can scan from the user chat and apply it. any help?

Last edited by Doggg; 07-09-2019 at 11:38.
Doggg is offline
farawayf
Senior Member
Join Date: Jan 2019
Old 07-09-2019 , 11:52   Re: how can you scan a variable and then apply it?
Reply With Quote #2

upd include
PHP Code:
#include <sourcemod>

#pragma newdecls required

public void OnPluginStart() {
    
RegAdminCmd("sm_cheats"cheatsADMFLAG_CONVARS);
}

public 
Action cheats(int clientint args) {    
    if(
args 1) {
        
ReplyToCommand(client"[SM] Usage: sm_cheats <value>");
        return 
Plugin_Handled;
    }    
    
    
char arg[4];
    
    
GetCmdArg(1argsizeof(arg));
    
    if(
StrEqual(arg"1")) {
        
ServerCommand("sv_cheats 1");
        
PrintToChat(client"\x04svcheats is *on*");
    }
    else if(
StrEqual(arg"0")) {
        
ServerCommand("sv_cheats 0");
        
PrintToChat(client"\x03svcheats is *off*");
    }        
    return 
Plugin_Handled;


Last edited by farawayf; 07-09-2019 at 12:01.
farawayf is offline
Doggg
Member
Join Date: Jun 2018
Old 07-09-2019 , 13:24   Re: how can you scan a variable and then apply it?
Reply With Quote #3

Quote:
Originally Posted by farawayf View Post
upd include
PHP Code:
#include <sourcemod>

#pragma newdecls required

public void OnPluginStart() {
    
RegAdminCmd("sm_cheats"cheatsADMFLAG_CONVARS);
}

public 
Action cheats(int clientint args) {    
    if(
args 1) {
        
ReplyToCommand(client"[SM] Usage: sm_cheats <value>");
        return 
Plugin_Handled;
    }    
    
    
char arg[4];
    
    
GetCmdArg(1argsizeof(arg));
    
    if(
StrEqual(arg"1")) {
        
ServerCommand("sv_cheats 1");
        
PrintToChat(client"\x04svcheats is *on*");
    }
    else if(
StrEqual(arg"0")) {
        
ServerCommand("sv_cheats 0");
        
PrintToChat(client"\x03svcheats is *off*");
    }        
    return 
Plugin_Handled;

thanks for the code, But i am trying to understand the logic for improve.
a few questions,
is this the actual scan commnad? GetCmdArg(1, arg, sizeof(arg));?
if so, what does the one means? i understood that args is a char array, and the sizeof(args) means the bytes (am i even right?), but what does the one means?
when will args will be lower then one? when there is none? like cheats <nothing>?
thanks!
Doggg is offline
farawayf
Senior Member
Join Date: Jan 2019
Old 07-09-2019 , 13:46   Re: how can you scan a variable and then apply it?
Reply With Quote #4

PHP Code:

public Action cheats(int clientint args) {    
    
/* if arguments less than 1, then print to client and return. This will be called if player types the command without 
        arguments. Like "!cheats"
    */ 
     
if(args 1) {
        
ReplyToCommand(client"[SM] Usage: sm_cheats <value>");
        return 
Plugin_Handled;
    }   
    
    
char arg[4]; //creating char with any name, "arg" for example.
    
    
GetCmdArg(1argsizeof(arg)); //get the first argument of command and store it in char with full size of argument
    
   //then check equal and do stuff
    
if(StrEqual(arg"1")) {
        
ServerCommand("sv_cheats 1");
        
PrintToChat(client"\x04svcheats is *on*");
    }
    else if(
StrEqual(arg"0")) {
        
ServerCommand("sv_cheats 0");
        
PrintToChat(client"\x03svcheats is *off*");
    }        
    return 
Plugin_Handled;

GetCmdArg: https://sm.alliedmods.net/new-api/console/GetCmdArg

Last edited by farawayf; 07-09-2019 at 13:51.
farawayf is offline
Doggg
Member
Join Date: Jun 2018
Old 07-09-2019 , 14:21   Re: how can you scan a variable and then apply it?
Reply With Quote #5

Quote:
Originally Posted by farawayf View Post
PHP Code:

public Action cheats(int clientint args) {    
    
/* if arguments less than 1, then print to client and return. This will be called if player types the command without 
        arguments. Like "!cheats"
    */ 
     
if(args 1) {
        
ReplyToCommand(client"[SM] Usage: sm_cheats <value>");
        return 
Plugin_Handled;
    }   
    
    
char arg[4]; //creating char with any name, "arg" for example.
    
    
GetCmdArg(1argsizeof(arg)); //get the first argument of command and store it in char with full size of argument
    
   //then check equal and do stuff
    
if(StrEqual(arg"1")) {
        
ServerCommand("sv_cheats 1");
        
PrintToChat(client"\x04svcheats is *on*");
    }
    else if(
StrEqual(arg"0")) {
        
ServerCommand("sv_cheats 0");
        
PrintToChat(client"\x03svcheats is *off*");
    }        
    return 
Plugin_Handled;

GetCmdArg: https://sm.alliedmods.net/new-api/console/GetCmdArg
Thanks!
one last question, can you go a bit deeper about the first thing you need to input?
GetCmdArg(1, arg, sizeof(arg)); this 1 means you can input only 0 and 1? or what? i am not fully get what this 1 stands for (as you may see its new for me).
Doggg is offline
Cruze
Veteran Member
Join Date: May 2017
Old 07-09-2019 , 14:33   Re: how can you scan a variable and then apply it?
Reply With Quote #6

Arguement number to retrieve from the command.
__________________
Taking paid private requests! Contact me
Cruze is offline
farawayf
Senior Member
Join Date: Jan 2019
Old 07-09-2019 , 14:40   Re: how can you scan a variable and then apply it?
Reply With Quote #7

argument number to retrieve.
for example we need to make a command like sm_cvar <cvar> <value>

so need to do like this

PHP Code:
    char cvar[64], cvarValue[32], buffer[96];

    
GetCmdArg(1cvarsizeof(cvar)); //get the first argument (cvar)
    
GetCmdArg(2cvarValuesizeof(cvarValue)); //get the second argument (value)

    
Format(buffersizeof(buffer), "%s %s"cvarcvarValue); //format 2 chars to one
    
    
ServerCommand(buffer); //then execute the command and value from buffer char
    
return Plugin_Handled

Last edited by farawayf; 07-09-2019 at 14:41.
farawayf is offline
Doggg
Member
Join Date: Jun 2018
Old 07-09-2019 , 14:58   Re: how can you scan a variable and then apply it?
Reply With Quote #8

Quote:
Originally Posted by farawayf View Post
argument number to retrieve.
for example we need to make a command like sm_cvar <cvar> <value>

so need to do like this

PHP Code:
    char cvar[64], cvarValue[32], buffer[96];

    
GetCmdArg(1cvarsizeof(cvar)); //get the first argument (cvar)
    
GetCmdArg(2cvarValuesizeof(cvarValue)); //get the second argument (value)

    
Format(buffersizeof(buffer), "%s %s"cvarcvarValue); //format 2 chars to one
    
    
ServerCommand(buffer); //then execute the command and value from buffer char
    
return Plugin_Handled

understood, Thanks.
sorry to bother so much, But another last question.
if i would like to use this number and do something with it?
lets say i want to make a restart command and then enter seconds for it to wait before restart.
so i did this far :

Code:
public Action rr(int client, int args)
{
 if(args < 1)
 {
   ReplyToCommand(client, "[SM] Usage: sm_rr <seconds>");
   return Plugin_Handled;
 }
 
 char args[4];
 GetCmdArg(1, arg, sizeof(args));
 ServerCommand("mp_restartgame " + args);
 return Plugin_Handled;
}
i know its wrong, but how should i use that?

Last edited by Doggg; 07-09-2019 at 15:02.
Doggg is offline
farawayf
Senior Member
Join Date: Jan 2019
Old 07-09-2019 , 15:16   Re: how can you scan a variable and then apply it?
Reply With Quote #9

PHP Code:

public Action rr(int clientint args) {
    if(
args 1) { 
        
ReplyToCommand(client"[SM] Usage: sm_rr <seconds>"); 
        return 
Plugin_Handled
    } 
    
char value[32]; //also do not forget to increase the size of the array. in your code char size is 4 which is too small.

    
GetCmdArg(1valuesizeof(value));

    
ServerCommand("mp_restartgame %s"value);
    return 
Plugin_Handled;  


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

Quote:
Originally Posted by farawayf View Post
PHP Code:

public Action rr(int clientint args) {
    if(
args 1) { 
        
ReplyToCommand(client"[SM] Usage: sm_rr <seconds>"); 
        return 
Plugin_Handled
    } 
    
char value[32]; //also do not forget to increase the size of the array. in your code char size is 4 which is too small.

    
GetCmdArg(1valuesizeof(value));

    
ServerCommand("mp_restartgame %s"value);
    return 
Plugin_Handled;  

thanks,
char array in size of 4 means that it will contain 1 number? (integer byte size is 4)? how should i measure the array size? what 32 means? ofc its not 32 chars because that will be stupid. but what is it?
Doggg is offline
Reply


Thread Tools
Display Modes

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 20:06.


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