AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   how can you scan a variable and then apply it? (https://forums.alliedmods.net/showthread.php?t=317365)

Doggg 07-09-2019 11:37

how can you scan a variable and then apply it?
 
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?

farawayf 07-09-2019 11:52

Re: how can you scan a variable and then apply it?
 
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;



Doggg 07-09-2019 13:24

Re: how can you scan a variable and then apply it?
 
Quote:

Originally Posted by farawayf (Post 2658475)
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!

farawayf 07-09-2019 13:46

Re: how can you scan a variable and then apply it?
 
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

Doggg 07-09-2019 14:21

Re: how can you scan a variable and then apply it?
 
Quote:

Originally Posted by farawayf (Post 2658487)
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).

Cruze 07-09-2019 14:33

Re: how can you scan a variable and then apply it?
 
Arguement number to retrieve from the command.

farawayf 07-09-2019 14:40

Re: how can you scan a variable and then apply it?
 
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


Doggg 07-09-2019 14:58

Re: how can you scan a variable and then apply it?
 
Quote:

Originally Posted by farawayf (Post 2658501)
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?

farawayf 07-09-2019 15:16

Re: how can you scan a variable and then apply it?
 
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;  



Doggg 07-09-2019 15:30

Re: how can you scan a variable and then apply it?
 
Quote:

Originally Posted by farawayf (Post 2658503)
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?


All times are GMT -4. The time now is 10:13.

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