Raised This Month: $52 Target: $400
 13% 

How to change string into int?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Doggg
Member
Join Date: Jun 2018
Old 07-11-2019 , 07:41   How to change string into int?
Reply With Quote #1

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");
}

}

Last edited by Doggg; 07-11-2019 at 07:42.
Doggg is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 07-11-2019 , 08:06   Re: How to change string into int?
Reply With Quote #2

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.
__________________
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)
nosoop is offline
Doggg
Member
Join Date: Jun 2018
Old 07-11-2019 , 10:10   Re: How to change string into int?
Reply With Quote #3

Quote:
Originally Posted by nosoop View Post
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?).
Doggg is offline
Whai
Senior Member
Join Date: Jul 2018
Location: in the daze
Old 07-11-2019 , 11:23   Re: How to change string into int?
Reply With Quote #4

Quote:
Originally Posted by Doggg View Post
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 View Post
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 View Post
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 View Post
have you done these kind of plugins just for practice?
Yes and no, I did plugins for training but also for the community
__________________
My GitHub repo. (Plugins are often up to date on GitHub than here)
My plugins
Whai is offline
I am inevitable
Member
Join Date: May 2019
Location: 0xA6DA34
Old 07-11-2019 , 16:31   Re: How to change string into int?
Reply With Quote #5

You might wanna have a space on PrintToChatAll(""); in the beginning, because for some reason, color codes as first character isn't readable.
__________________
I do make plugins upon requests, so hit me up on discord if you're interested: cucklord67xD#3823
I am inevitable is offline
CrazyHackGUT
AlliedModders Donor
Join Date: Feb 2016
Location: Izhevsk, Russia
Old 07-11-2019 , 23:44   Re: How to change string into int?
Reply With Quote #6

Space in the beginning required only for CS:GO. For normal games like CS:S or TF2, all color works without space.
__________________
My english is very bad. I am live in Russia. Learning english language - very hard task for me...
CrazyHackGUT is offline
Send a message via ICQ to CrazyHackGUT Send a message via Skype™ to CrazyHackGUT
Kellan123
AlliedModders Donor
Join Date: Aug 2012
Old 07-12-2019 , 02:50   Re: How to change string into int?
Reply With Quote #7

Quote:
Originally Posted by CrazyHackGUT View Post
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>");
	}
}

Last edited by Kellan123; 07-12-2019 at 02:51.
Kellan123 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 22:21.


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