Raised This Month: $32 Target: $400
 8% 

What Is Wrong About This Code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
kasikadam
Member
Join Date: Jun 2016
Old 06-18-2017 , 19:15   What Is Wrong About This Code
Reply With Quote #1

I'm new at sourcepawn and general coding. I was trying to make a plugin that when someone type;
!slotol then it prints to all that "user(who typed !slotol) blablabla something". Here is my code
Code:
#include <sourcemod>
#include <clients>

public Plugin myinfo =
 {
	name        = "[CSGO] ",
	author      = "Eren ",
	description = "",
	version     = "1.0",
	url         = ""
};

public OnPluginStart()
{
	RegConsoleCmd("sm_slotol", Command_slotol, "");
}

public Action:Command_slotol(client, args)
{
	new String:clientname[32];
	GetClientName(client, clientname, 32);
	PrintToChatAll("[Player] %d blablbalbalba !, clientname");
	PrintToChatAll("[Player] %d blablbalbalba!, clientname");
	return Plugin_Handled; 
}
When I compile this, upload and load and type !slotol nothing happens.

Last edited by kasikadam; 06-18-2017 at 19:17.
kasikadam is offline
Weetabix
Member
Join Date: Feb 2017
Location: United Kingdom
Old 06-18-2017 , 19:31   Re: What Is Wrong About This Code
Reply With Quote #2

PHP Code:
#include <sourcemod>

public Plugin myinfo =
 {
    
name        "[CSGO] ",
    
author      "Eren ",
    
description "",
    
version     "1.0",
    
url         ""
};

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_slotol"Command_slotol);
}

public 
Action Command_slotol(int iClientint iArgs)
{
    
PrintToChatAll("[Player] %N blablbalbalba!"iClient);
    
    return 
Plugin_Handled

1. The include "sourcemod" automatically includes some other include files so there's no need to include "clients" for instance since this is automatically included.

2. Please stick to using one syntax, preferably the new one. I updated it to this.

3. When using formatting of strings, you can use the format "%N" and then a client's index which will print the name of the client.

4. "%d" is for decimal values, not strings.

5. The reason "nothing happens" is because when you execute that command and it tries to format a string where a decimal is expected, you will get an error in the plugin. You should always check the server console.

Last edited by Weetabix; 06-18-2017 at 19:59.
Weetabix is offline
B3none
AlliedModders Donor
Join Date: Oct 2016
Location: United Kingdom
Old 06-19-2017 , 04:09   Re: What Is Wrong About This Code
Reply With Quote #3

You're also writing in the old Syntax, I'd recommend using

PHP Code:
#pragma newdecls required 
This means that the code will only accept the new style syntax Purely preference but it's best to start with the latest syntax
__________________
B3none is offline
kasikadam
Member
Join Date: Jun 2016
Old 06-19-2017 , 04:41   Re: What Is Wrong About This Code
Reply With Quote #4

Quote:
Originally Posted by Weetabix View Post
1. The include "sourcemod" automatically includes some other include files so there's no need to include "clients" for instance since this is automatically included.

2. Please stick to using one syntax, preferably the new one. I updated it to this.

3. When using formatting of strings, you can use the format "%N" and then a client's index which will print the name of the client.

4. "%d" is for decimal values, not strings.

5. The reason "nothing happens" is because when you execute that command and it tries to format a string where a decimal is expected, you will get an error in the plugin. You should always check the server console.
Thanks man ! and have few other questions. What if i want some colors for PrintToChatAll and why we used iClient instead of Client ? Thanks !
I tried to use morecolors but it just prints numbers to chat.

Last edited by kasikadam; 06-19-2017 at 05:16.
kasikadam is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 06-19-2017 , 07:10   Re: What Is Wrong About This Code
Reply With Quote #5

iClient and Client are just variable names, doesn't really make a difference which you use but some people like to prefix their variable names with its data type to make code clearer.

e.g.
int Args becomes iArgs
char Letter becomes cLetter
char[] Name becomes sName (for string, since char arrays are just strings)
float Number becomes fNumber

Again you don't have to do all this, but its a coding convention that makes code clearer and easier to read

As for colours in CSGO you're more limited than other games with what you can use, but to add colour you just use these characters, and anything written after them will be that colour (got these from ColorVariables include)

default (white): \x01
teamcolour (will be purple if message from server): \x03
red: \x07
lightred: \x0F
darkred: \x02
bluegrey: \x0A
blue: \x0B
darkblue: \x0C
purple: \x03
orchid: \x0E
yellow: \x09
gold: \x10
lightgreen: \x05
green: \x04
lime: \x06
grey: \x08
grey2: \x0D

Example:
Say I want to make the word "red" red
PrintToChatAll("This is the colour \x07red");

Last edited by hmmmmm; 06-19-2017 at 07:12.
hmmmmm is offline
kasikadam
Member
Join Date: Jun 2016
Old 06-19-2017 , 11:27   Re: What Is Wrong About This Code
Reply With Quote #6

Quote:
Originally Posted by hmmmmm View Post
iClient and Client are just variable names, doesn't really make a difference which you use but some people like to prefix their variable names with its data type to make code clearer.

e.g.
int Args becomes iArgs
char Letter becomes cLetter
char[] Name becomes sName (for string, since char arrays are just strings)
float Number becomes fNumber

Again you don't have to do all this, but its a coding convention that makes code clearer and easier to read

As for colours in CSGO you're more limited than other games with what you can use, but to add colour you just use these characters, and anything written after them will be that colour (got these from ColorVariables include)

default (white): \x01
teamcolour (will be purple if message from server): \x03
red: \x07
lightred: \x0F
darkred: \x02
bluegrey: \x0A
blue: \x0B
darkblue: \x0C
purple: \x03
orchid: \x0E
yellow: \x09
gold: \x10
lightgreen: \x05
green: \x04
lime: \x06
grey: \x08
grey2: \x0D

Example:
Say I want to make the word "red" red
PrintToChatAll("This is the colour \x07red");
Thanks for your reply. What if I want to use more than one color in same sentence? Will it work?
Thanks!
kasikadam is offline
ambn
Veteran Member
Join Date: Feb 2015
Location: Fun servers
Old 06-19-2017 , 11:47   Re: What Is Wrong About This Code
Reply With Quote #7

Quote:
Originally Posted by kasikadam View Post
Thanks for your reply. What if I want to use more than one color in same sentence? Will it work?
Thanks!
Yes it will.
__________________
ambn is offline
AmazingDay
Junior Member
Join Date: Jun 2017
Location: Book
Old 06-19-2017 , 13:50   Re: What Is Wrong About This Code
Reply With Quote #8

Quote:
Originally Posted by kasikadam View Post
why we used iClient instead of Client ?
Hey to get client name you can also do this:
PHP Code:
#include <sourcemod>
//your info
public void OnPluginStart()
{
    
RegConsoleCmd("sm_slotol"Command_slotol"");
}

public 
Action Command_slotol(int clientint args)
{
    
char cname[32]//size of the name
    
GetClientName(clientcnamesizeof(cname));
    
PrintToChatAll("[PLAYER]%s blabla!"cname);
    return 
Plugin_Handled;

Quote:
Originally Posted by kasikadam View Post
Thanks man ! and have few other questions. What if i want some colors for PrintToChatAll
Just use CPrintToChat or CPrintToChatAll with the include <morecolors>
e.g If you want green text:
PHP Code:
CPrintToChatAll("{green}blabla"
https://forums.alliedmods.net/showthread.php?t=185016

Last edited by AmazingDay; 06-19-2017 at 14:12.
AmazingDay is offline
kasikadam
Member
Join Date: Jun 2016
Old 06-19-2017 , 17:28   Re: What Is Wrong About This Code
Reply With Quote #9

Quote:
Originally Posted by AmazingDay View Post
Hey to get client name you can also do this:
PHP Code:
#include <sourcemod>
//your info
public void OnPluginStart()
{
    
RegConsoleCmd("sm_slotol"Command_slotol"");
}

public 
Action Command_slotol(int clientint args)
{
    
char cname[32]//size of the name
    
GetClientName(clientcnamesizeof(cname));
    
PrintToChatAll("[PLAYER]%s blabla!"cname);
    return 
Plugin_Handled;

Just use CPrintToChat or CPrintToChatAll with the include <morecolors>
e.g If you want green text:
PHP Code:
CPrintToChatAll("{green}blabla"
https://forums.alliedmods.net/showthread.php?t=185016
Thanks for advice and when I use morecolors it prints colors hashes to chat not working for me. I will go with \xx probably
kasikadam is offline
AmazingDay
Junior Member
Join Date: Jun 2017
Location: Book
Old 06-20-2017 , 12:29   Re: What Is Wrong About This Code
Reply With Quote #10

Quote:
Originally Posted by kasikadam View Post
Thanks for advice and when I use morecolors it prints colors hashes to chat not working for me. I will go with \xx probably
Hmm did you write the code correctly ?
CPrintToChatAll("{gold}blabla");
if you gtting error then you did something wrong
AmazingDay 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 17:42.


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