AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   What Is Wrong About This Code (https://forums.alliedmods.net/showthread.php?t=298642)

kasikadam 06-18-2017 19:15

What Is Wrong About This Code
 
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.

Weetabix 06-18-2017 19:31

Re: What Is Wrong About This Code
 
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.

B3none 06-19-2017 04:09

Re: What Is Wrong About This Code
 
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

kasikadam 06-19-2017 04:41

Re: What Is Wrong About This Code
 
Quote:

Originally Posted by Weetabix (Post 2529715)
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.

hmmmmm 06-19-2017 07:10

Re: What Is Wrong About This Code
 
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");

kasikadam 06-19-2017 11:27

Re: What Is Wrong About This Code
 
Quote:

Originally Posted by hmmmmm (Post 2529789)
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!

ambn 06-19-2017 11:47

Re: What Is Wrong About This Code
 
Quote:

Originally Posted by kasikadam (Post 2529837)
Thanks for your reply. What if I want to use more than one color in same sentence? Will it work?
Thanks!

Yes it will.

AmazingDay 06-19-2017 13:50

Re: What Is Wrong About This Code
 
Quote:

Originally Posted by kasikadam (Post 2529763)
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 (Post 2529763)
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

kasikadam 06-19-2017 17:28

Re: What Is Wrong About This Code
 
Quote:

Originally Posted by AmazingDay (Post 2529877)
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

AmazingDay 06-20-2017 12:29

Re: What Is Wrong About This Code
 
Quote:

Originally Posted by kasikadam (Post 2529933)
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


All times are GMT -4. The time now is 17:06.

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