AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   How to change string into int? (https://forums.alliedmods.net/showthread.php?t=317403)

Doggg 07-11-2019 07:41

How to change string into int?
 
Hey I am to get better in sourcepawn (as you may see) but i feel like I am doing terrible job. The source api not really helping because i need to know how to actually use these functions so I have no other chooise then to ask you.
I wanted to create a plugin that will spam "spam" in the chat. and added a color choose to the client.
for exaple he will write /spam r 5, this will print 5 times in red spam. (I am really trying to get better so i guess i need to practice in these nonesence plugins, If someone challange me to write a plugin for begginers ofc, i started learning only 3 days ago.. i will be more then happy to try) anyway, How bad is it?
Code:

public void OnPluginStart()
{
 RegConsoleCmd("sm_spam", SPAM);
}

public Action SPAM (int client, int args)

{
if(args > 2)
{
ReplyToCommand(client, "[SM] spam <color> <amount of times>");
}       

char colorchoose[3];
GetCmdArg(1, colorchoose, sizeof(colorchoose));
char Camount[4];
GetCmdArg(2, Camount, sizeof(Camount));
int iamount = StringToInt(Camount, 10);

if(iamount = 0)
{
ReplyToCommand(client, "[SM] Please enter a normal number");
}


if(StrEqual(colorchoose,"r"))
{
for (int i = 0; i < iamount; i++)
{
PrintToChatAll("\x03Spam");
}
}

else if(StrEqual(colorchoose,"g"))
{

for (int i = 0; i < iamount; i++)
{
PrintToChatAll("\x04Spam");
}

}


nosoop 07-11-2019 08:06

Re: How to change string into int?
 
Don't put yourself down over it, gotta start somewhere.

The thing that stands out is that you really should indent your code. Take a look at other (released) plugins as examples and see that they indent the line after every opening brace ({) and unindent before the closing one (}). It'll easily catch the missing bracket at the end that would cause this to not compile, assuming it didn't get lost when making this post.

StringToInt is used correctly, so you're fine there, and so are the other function calls from what I can tell.

if (args > 2) doesn't catch the case if you only pass in zero or one argument (something like /spam r may throw an error at GetCmdArg(2, ...)).

if (iamount = 0) is also incorrect. The compiler or your development environment will give you a warning about a possibly unintended assignment because of the single equals; equality checks use ==. You'll want to use if (iamount <= 0) to ensure the player doesn't put in a negative value.

You may also want to add one last else to tell the player that they didn't pick a valid color, but I'm sure you'd catch that in a more serious plugin.

You should also use return to leave the function early (after you use ReplyToCommand, you don't want to process the rest of the function). For console / chat commands, return Plugin_Handled is generally the way to go about it.

Doggg 07-11-2019 10:10

Re: How to change string into int?
 
Quote:

Originally Posted by nosoop (Post 2658750)
Don't put yourself down over it, gotta start somewhere.

The thing that stands out is that you really should indent your code. Take a look at other (released) plugins as examples and see that they indent the line after every opening brace ({) and unindent before the closing one (}). It'll easily catch the missing bracket at the end that would cause this to not compile, assuming it didn't get lost when making this post.

StringToInt is used correctly, so you're fine there, and so are the other function calls from what I can tell.

if (args > 2) doesn't catch the case if you only pass in zero or one argument (something like /spam r may throw an error at GetCmdArg(2, ...)).

if (iamount = 0) is also incorrect. The compiler or your development environment will give you a warning about a possibly unintended assignment because of the single equals; equality checks use ==. You'll want to use if (iamount <= 0) to ensure the player doesn't put in a negative value.

You may also want to add one last else to tell the player that they didn't pick a valid color, but I'm sure you'd catch that in a more serious plugin.

You should also use return to leave the function early (after you use ReplyToCommand, you don't want to process the rest of the function). For console / chat commands, return Plugin_Handled is generally the way to go about it.

Thanks for the answer!
how can i fix the arg thing? i mean i thought it should catch it because if(args >2) means also 0 and 1 but i dont know, How should I fix it?
yeah i totally forgot about the return plugin handled thing.. i guess its very important.
forgot too about the case the client will input negetive values, thanks for that.
i feel really bored, i dont know if these stuff really help me progress (have you done these kind of plugins just for practice?).

Whai 07-11-2019 11:23

Re: How to change string into int?
 
Quote:

Originally Posted by Doggg (Post 2658761)
Thanks for the answer!
how can i fix the arg thing? i mean i thought it should catch it because if(args >2) means also 0 and 1 but i dont know, How should I fix it?

Just do "if(args != 2)"

Quote:

Originally Posted by Doggg (Post 2658761)
yeah i totally forgot about the return plugin handled thing.. i guess its very important.

If you don't put the return Plugin_Handled, when you enter the command in console : it will say to you "Unknown command" even when the command is executed

Quote:

Originally Posted by Doggg (Post 2658761)
i feel really bored, i dont know if these stuff really help me progress (have you done these kind of plugins just for practice?).

Well, you did mistakes, someone will correct you : you learn from your errors. So, yes you will progress.

Quote:

Originally Posted by Doggg (Post 2658761)
have you done these kind of plugins just for practice?

Yes and no, I did plugins for training but also for the community

I am inevitable 07-11-2019 16:31

Re: How to change string into int?
 
You might wanna have a space on PrintToChatAll(""); in the beginning, because for some reason, color codes as first character isn't readable.

CrazyHackGUT 07-11-2019 23:44

Re: How to change string into int?
 
Space in the beginning required only for CS:GO. For normal games like CS:S or TF2, all color works without space.

Kellan123 07-12-2019 02:50

Re: How to change string into int?
 
Quote:

Originally Posted by CrazyHackGUT (Post 2658827)
Space in the beginning required only for CS:GO. For normal games like CS:S or TF2, all color works without space.

Code:

#include <sourcemod>

#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo =
{
        name = "Spam",
        author = "Doggg, AlliedModders Donator",
        version = "1.1"
};

public void OnPluginStart()
{
        RegConsoleCmd("sm_spam", Spam);
}

public Action Spam(int client, int args)
{
        if(args > 0)
        {
                char camount[1];
                GetCmdArg(2, camount, sizeof(camount));
               
                int iamount = StringToInt(camount);
               
                if(iamount > 0)
                {
                        char ccolor[1];
                        GetCmdArg(1, ccolor, sizeof(ccolor));
                       
                        if(StrEqual(ccolor,"r"))
                        {
                                for(int i; i <= iamount; i++) PrintToChatAll(" \x02Spam");
                        }
                        else if(StrEqual(ccolor,"g"))
                        {
                                for(int i; i <= iamount; i++) PrintToChatAll(" \x04Spam");
                        }
                }
                else
                {
                        ReplyToCommand(client, "[SM] Please enter a normal number");
                }
        }
        else
        {
                ReplyToCommand(client, "[SM] spam <color> <amount of times>");
        }
}



All times are GMT -4. The time now is 11:59.

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