SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 26/04/2015, 11:20 AM   #101
lol1
Little Clucker
 
Join Date: Nov 2010
Posts: 2
Reputation: 0
Default Re: SampSharp - Write gamemodes in .NET

Sounds good to me. Will try to port my gamemode.
lol1 is offline   Reply With Quote
Old 26/04/2015, 01:03 PM   #102
ikkentim
High-roller
 
ikkentim's Avatar
 
Join Date: Jan 2010
Location: Kampen, The Netherlands
Posts: 1,076
Reputation: 199
Default Re: SampSharp - Write gamemodes in .NET

Quote:
Originally Posted by Sithis View Post
Hi nighthammer, that's very easy:

You can create a method and tag it with [Command("commandname")]. This will create a command called /commandname and the method underneath it will be used to handle the command. Example:

PHP Code:
        [Command("group")] // Displays information about a group.
        
public static void group(Player playerstring name)
        {
            
Boolean groupFound false;

            foreach (
Group group in Core.GroupList)
            {
                if (
group.Name == name)
                {
                    
// We found a math for this group, continue!
                    
groupFound true;

                    
player.SendClientMessage("[Group] " group.Name " (" group.GroupType " group)");
                }
                break;
            }

            if (!
groupFound)
            {
                
player.SendClientMessage(Color.IndianRed"[Error] Group '" name "' could not be found.");
            }
        } 
It does not matter what the name of the method is. As long as you tag it as a command using [Command("commandname")], the method will be called when the command is sent.

The arguments of the method will tell SampSharp what kind of arguments the command accepts! The first argument, player, will always be needed. You can add integers, strings, whatever as arguments and use them in the command! In the example above, string name is used to ask for the group name that the player wants information about.

If incorrent command arguments are given by a player, SampSharp will automatically correct the player by sending Usage: /group [name].

It cannot be simpler!

If you have any other questions, i'm more than happy to help you out.

This is a good explanation of the basics of the commands system, but there is a little more to know.

Defining and detection of parameter types
By default SA-MP# looks at the parameters of the method to determine the parameters of the command. The parameter types it can automatically detect are: int, float, string*, GtaPlayer or any subclass of GtaPlayer and any enumerator(e.g. VehicleModelType).

Notice that by default SA-MP# thinks that you want a single word as argument when a method accepts a string:
PHP Code:
[Command("tell")]
public static 
void TellCommand(Player senderPlayer receiverstring message)
{
    
//..

In this example players can only send a single world to players using the /tell [receiver] [message] command.

If you want the string parameter to accept text instead of a single word, you need to mark it as a text parameter:
PHP Code:
[Command("tell")]
[
Text("message")]
public static 
void TellCommand(Player senderPlayer receiverstring message)
{
    
//..

It might be good to know that what actually happens when a command is loaded, is that it calls the following delegate for every parameter that does not yet have an attribute associated with it:
PHP Code:
public class DetectedCommand Command
{
    
//...
    
public static Func<TypestringParameterAttributeResolveParameterType getset; }
    
//...

    // default value:
    
ResolveParameterType = (typename) =>
            {
                if (
type == typeof (int)) return new IntegerAttribute(name);
                if (
type == typeof (string)) return new WordAttribute(name);
                if (
type == typeof (float)) return new FloatAttribute(name);
                if (
typeof (GtaPlayer).IsAssignableFrom(type)) return new PlayerAttribute(name);

                return 
type.IsEnum ? new EnumAttribute(nametype) : null;
            };

So when it has resolved every parameter of the tell command, the method internally looks like this:

PHP Code:
[Command("tell")]
[
Player("receiver")]
[
Text("message")]
public static 
void TellCommand(Player senderPlayer receiverstring message)
{
    
//..

Methods

There are two places where you can define you commands: a static method anywhere in your code, or in your player class. I haven't checked this but AFAIK the command method must be public!

-player class
If you have a Player class which is a sub class of GtaPlayer, you can put the commands straight in there:
PHP Code:
class Player GtaPlayer
{
  
//...
  
[Command("help")]
  public 
void HelpCommand()
  {
    
//...
  
}
  
//...

Notice the method is not static and should not accept a `Player sender` parameter as first argument. If you put a Player argument in there ( HelpCommand(Player x) ) SA-MP# will think it's an parameter of the command.

-a static method
Can be placed in any public class. Accepts a Player as first parameter which is the sender
PHP Code:
public class AnyClass
{
  [
Command("help")]
  public static 
void HelpCommand(Player sender)
  {
    
//...
  
}

permission check
If you create admin commands, you obviously do not want any odd player to be able to use the command.

In the Command attribute you can specify various properties, including the "PermissionCheckMethod".

PHP Code:
public class AnyClass
{
  public static 
bool IsAdmin(Player player)
  {
      return 
player.AdminLevel 0;
  }

  [
Command("adminhelp"PermissionCheckMethod="IsAdmin")]
  public static 
void AdminHelpCommand(Player sender)
  {
    
//...
  
}
}

// or

class Player GtaPlayer
{
  
// ...
  
public bool IsAdmin()
  {
      return 
AdminLevel 0;
  }

  [
Command("adminhelp"PermissionCheckMethod="IsAdmin")]
  public 
void AdminHelpCommand()
  {
    
//...
  
}
  
// ...

Notice that in the Player class, the specified permission check method is not static, and accepts no parameters.

If you return false in the permission check method, SA-MP# will not call the command. If you return true, it will.

command groups
If you don't want to use command groups, skip this chapter
You can group commands together. If you, for example, want to use the following commands structure, this can be helpful for you:

Code:
/help
/player menu -or- /p menu -or- /menu
/player status -or- /p status
/admin player kick [player] -or- /admin p kick [player] -or- /a player kick [player] -or- /a p kick [player] -or- /kick [player]
/admin player ban [player] -or- /admin p ban [player] -or- /a player ban [player] -or- /a p ban [player]
/admin serverstatus -or- /a serverstatus
/admin restartserver -or- /a restartserver
You can register groups using the following function:
PHP Code:
CommandGroup.Register(string namestring alias nullCommandGroup parent null); 
As you can see you can also nest command groups inside other command groups.

You can implement all commands listed above like this:
(I'm not checking for permissions here, just showing how command groups work)

PHP Code:
//Somewhere near you initialization code...
CommandGroup.Register("player""p");
var 
admin CommandGroup.Register("admin""a");
CommandGroup.Register("player""p"admin);

// In some class...
[Command("help")]
public static 
void Help(Player s) {}

[
Command("menu"Shortcut="menu")]
[
CommandGroup("player")] // specify the group this command is in
public static void PlayerMenu(Player s) {}

[
Command("status")]
[
CommandGroup("player")]
public static 
void PlayerStatus(Player s) {}

[
Command("kick"Shortcut="kick")]
[
CommandGroup("admin""player")]
public static 
void AdminPlayerKick(Player sPlayer kickee) {}

[
Command("ban")]
[
CommandGroup("admin""player")]
public static 
void AdminPlayerBan(Player sPlayer banee) {}

[
Command("serverstatus")]
[
CommandGroup("admin")]
public static 
void AdminServerstatus(Player s) {}

[
Command("restartserver")]
[
CommandGroup("admin")]
public static 
void AdminRestartserver(Player s) {} 
I've had problems with bugs in this system before. I am 99% sure I've fixed them all. If this is not the case, please sent me an issue or post here, I'll then fix it asap!

In the example above, I've given all command groups aliases and given the menu and kick commands a shortcut.
It is also possible to give a command an alias:
PHP Code:
[Command("kick"Alias="kick")] 
If you decide not to use command groups, there is no difference between aliases and shortcuts.
If you do use command groups, there is. For example, you have The following command:
PHP Code:
[Command("b"Alias="c"Shortcut="d")]
[
CommandGroup("a""e")]
public static 
void abc(Player x){} 
You can call this command using any of following commands:
/a b
/a c
/e b
/e c
/d

As you can see, if you decide to call the command using the shortcut, you do not need to type in the command group. If you decide to call the command using the alias, you still need to enter the command group(or an alias of the command group)


Ignore case
By default, SA-MP# ignores the case of commands both /help and /HeLP will call a command tagged with [Command("help")]. However, if you do not wish this to happen you can disable this:

PHP Code:
// the /CAPSLOCK command
[Command("CAPSLOCK"IgnoreCase=false)]
public static 
void something(Player s) {} 
Return types
You can also make a command return a boolean. If it returns true, SA-MP# will assume the command has successfully been executed. If it returns false, SA-MP# will assume the command has not been executed and will show the "unknown command" message to the player.

Example:
PHP Code:
[Command("something")]
public static 
bool something(Player s)
{
    if(!
s.CanDoSomething) return false;

    
s.SendClientMessage("You did something!");
    return 
true;

It's basically the same as the PermissionCheckMethod property in the Command attribute, but then you can check for permission within the command method itself.

Inheriting the Command class
If you do not want to use all the attributes and things explained above, you can also inherit from the Command class:
https://github.com/ikkentim/SampShar...nds/Command.cs

I'm not going to show an example of how to do this, because the system explained above is much easier, flexible and useful.

But if you decide to create a sub class of Command, you need to create an instance of it once to allow SA-MP# to find it:
PHP Code:
//Somewhere near you initialization code...
new MyAwesomeCommand(); 
Creating custom parameter attributes

Lets say you have a Street class
PHP Code:
class Street
{
  public 
string Name get; private set;}
  
//Lots more info...

and you want to allow players to type /goto [streetname]

you can create a custom attribute:

PHP Code:
    public class StreetAttribute WordAttribute
    
{
        public 
StreetAttribute(string name)
            : 
base(name)
        {
        }

        public 
override bool Check(ref string commandout object output)
        {
            
// Let the WordAttribute process the command first
            
if (!base.Check(ref commandout output))
                return 
false;

            
string word = (output as string).ToLower();

            
Street result null;
            
// TODO: find a street that matches the name stored in the word variable and store it in result
            // If no street could be found, return false.

            
output result;
            return 
true;
        }
    } 
Now you can create the following command:
PHP Code:
[Command("goto")]
[
Street("street")]
public static 
void goto(Player senderStreet street)
{

Notice you need to specify that 'street' is a Street using [Street("street")]

If you want SA-MP# to automatically detect this, you need to add it to the parameter resolve delegate:

PHP Code:
//Somewhere near you initialization code...

DetectedCommand.ResolveParameterType = (typename) => 
            {
                if (
type == typeof (Street)) return new StreetAttribute(name);

                
// I haven't tested this, but AFAIK it should not recursively call this delegate, but call the previous (default) delegate.
                
return DetectedCommand.ResolveParameterType(typename);
            }; 
Now you can use:
PHP Code:
[Command("goto")]
public static 
void goto(Player senderStreet street)
{

Default values

If you want a parameter to have a default value, it needs to be at the end of the command:
PHP Code:
[Command("tell")]
[
Text("message")]
public static 
void tell(Player senderPlayer receiverint times 1string message "I like trains!")
{

If you now type /tell. It will send the following message:
Usage: /tell [receiver] (times) (message)
Notice the ( ) brackets instead of [ ]. This indicates that these parameters are optional.

usage format message
If you enter invalid parameters you will be shown a "Usage" message. By default this message looks like this:
Quote:
Usage: /tell [receiver] (times) (message)
It is generated using the following delegate:
PHP Code:
public class DetectedCommand Command
{
    
//...
    
public static Func<stringParameterAttribute[], stringUsageFormat getset; }
    
//...

    // default value:
    
UsageFormat = (nameparameters) =>
                
string.Format("Usage: /{0}{1}{2}"nameparameters.Any() ? ": " string.Empty,
                    
string.Join(" "parameters.Select(
                        
=> p.Optional
                            
string.Format("({0})"p.DisplayName)
                            : 
string.Format("[{0}]"p.DisplayName)
                        ))
                    );

You can change it by setting the delegate:
PHP Code:
//Somewhere near you initialization code...
DetectedCommand.UsageFormat = (nameparameters) =>
{
    return 
"some usage message";

________

If you have any remaining questions, don't hesitate to ask.
__________________
Greetings,
Tim Potze

Projects
GAMEDEV | Part-time developer working on Parkitect. | Steampage
LSRES | A SA:MP server from the past, and hopefully, back in the future.
SampSharp | Plugin for writing SA:MP gamemodes in C#. | Topic Website Github
SanMap | San Andreas in G00GLE MAPS! | Topic Github Sample
ikkentim is offline   Reply With Quote
Old 01/05/2015, 01:17 PM   #103
Sithis
Gangsta
 
Join Date: Mar 2013
Location: the Netherlands
Posts: 924
Reputation: 79
Default Re: SampSharp - Write gamemodes in .NET

Since 0.3.7 has been released, this plugin is being updated. You can follow the progress via the following link: https://github.com/ikkentim/SampSharp/issues/107
__________________
Developer and owner of https://trinityrpg.com
Sithis is offline   Reply With Quote
Old 01/05/2015, 01:25 PM   #104
ikkentim
High-roller
 
ikkentim's Avatar
 
Join Date: Jan 2010
Location: Kampen, The Netherlands
Posts: 1,076
Reputation: 199
Default Re: SampSharp - Write gamemodes in .NET

Quote:
Originally Posted by Sithis View Post
Since 0.3.7 has been released, this plugin is being updated. You can follow the progress via the following link: https://github.com/ikkentim/SampSharp/issues/107
Updating it as we speak.
__________________
Greetings,
Tim Potze

Projects
GAMEDEV | Part-time developer working on Parkitect. | Steampage
LSRES | A SA:MP server from the past, and hopefully, back in the future.
SampSharp | Plugin for writing SA:MP gamemodes in C#. | Topic Website Github
SanMap | San Andreas in G00GLE MAPS! | Topic Github Sample
ikkentim is offline   Reply With Quote
Old 02/05/2015, 03:55 PM   #105
ikkentim
High-roller
 
ikkentim's Avatar
 
Join Date: Jan 2010
Location: Kampen, The Netherlands
Posts: 1,076
Reputation: 199
Default Re: SampSharp - Write gamemodes in .NET

The plugin and API have been updated to v0.5.0, this update mainly focuses on supporting the newest features of SA-MP 0.3.7. For the full changelog, look on github
__________________
Greetings,
Tim Potze

Projects
GAMEDEV | Part-time developer working on Parkitect. | Steampage
LSRES | A SA:MP server from the past, and hopefully, back in the future.
SampSharp | Plugin for writing SA:MP gamemodes in C#. | Topic Website Github
SanMap | San Andreas in G00GLE MAPS! | Topic Github Sample
ikkentim is offline   Reply With Quote
Old 01/07/2015, 01:23 PM   #106
Sithis
Gangsta
 
Join Date: Mar 2013
Location: the Netherlands
Posts: 924
Reputation: 79
Default Re: SampSharp - Write gamemodes in .NET

The plugin has been updated to v0.6.2 and includes the following changes:

v0.6.2
  • Added various Color constructors and operators (including Color * float).
  • Added Color.Lerp(), Color.Lighten() and Color.Darken().
  • Added MathHelper class.
  • Added BaseMode.CallbackException event. This event is raised if an exception has been thrown in a callback without being handled.
  • Changed sender of menu related events to player.
  • Fixed a bug where Sync.Run() causes a NullReferenceException if called before controllers are loaded.
  • Fixed a bug where LogWriter (used by Console.Write) causes buffer overflow errors when handling long strings.
  • Fixed a bug causing .mdb files not to be loaded properly.

The SampSharp.GameMode library is available on NuGet:
http://sampsharp.timpotze.nl/package-manager
__________________
Developer and owner of https://trinityrpg.com
Sithis is offline   Reply With Quote
Old 04/07/2015, 09:15 PM   #107
kaZax
Big Clucker
 
kaZax's Avatar
 
Join Date: Jan 2013
Location: Kazakhstan
Posts: 60
Reputation: 10
Default Re: SampSharp - Write gamemodes in .NET

Gamemode on c# faster than Gamemode on PAWN?
__________________
vk.com
kazax@samp.work
play.samp.work:7777
kaZax is offline   Reply With Quote
Old 05/07/2015, 11:08 AM   #108
ikkentim
High-roller
 
ikkentim's Avatar
 
Join Date: Jan 2010
Location: Kampen, The Netherlands
Posts: 1,076
Reputation: 199
Default Re: SampSharp - Write gamemodes in .NET

I've never tested it perfectly, but yes it's faster.
__________________
Greetings,
Tim Potze

Projects
GAMEDEV | Part-time developer working on Parkitect. | Steampage
LSRES | A SA:MP server from the past, and hopefully, back in the future.
SampSharp | Plugin for writing SA:MP gamemodes in C#. | Topic Website Github
SanMap | San Andreas in G00GLE MAPS! | Topic Github Sample
ikkentim is offline   Reply With Quote
Old 05/07/2015, 04:34 PM   #109
kaZax
Big Clucker
 
kaZax's Avatar
 
Join Date: Jan 2013
Location: Kazakhstan
Posts: 60
Reputation: 10
Default Re: SampSharp - Write gamemodes in .NET

How to set encoding to "1251 windows-1251 ANSI Cyrillic; Cyrillic (Windows)" ?
__________________
vk.com
kazax@samp.work
play.samp.work:7777
kaZax is offline   Reply With Quote
Old 05/07/2015, 05:12 PM   #110
SecretBoss
Gangsta
 
SecretBoss's Avatar
 
Join Date: Dec 2014
Location: Greece
Posts: 818
Reputation: 103
Default Re: SampSharp - Write gamemodes in .NET

Great work
__________________
SecretBoss is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Write on the map hejsan123 Scripting Help 3 23/09/2012 09:35 AM
How to write something in a VAR? Ihsan_Cingisiz Help Archive 4 29/05/2010 08:24 PM
How Do You Write This? sidhu123 Everything and Nothing 2 12/09/2009 05:05 AM
Write something in big and to the top TimBuk.Ekh Help Archive 3 18/04/2009 08:19 PM


All times are GMT. The time now is 08:02 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.