[Plugin] SampSharp - Write gamemodes in .NET
#61

Sorry for all the questions, but here we go:

I have a class, public class Property. It represents a house. I have three public methods for creating different types of houses. The code for the most used struct (a player owned house) is as follows:

PHP код:
        public Property(int dbIDfloat xfloat yfloat zstring nametrinityRPG.Definitions.PropertyType typestring ownerfloat interiorxfloat interioryfloat interiorzint interiorid)
        {
            
createPickup(xyztype);
            
createLabel(xyz"Property: " name "\n Owner: " owner);
            
this.ownerName owner;
            
this.databaseID dbID;
            
this.interiorID interiorid;
            
this.interiorx interiorx;
            
this.interiory interiory;
            
this.interiorz interiorz;
        } 
The createPickup and createLabel methods are private methods for rendering the pickup and 3dtextlabel, both using the Streamer Wrapper.

PHP код:
        private void createPickup(float xfloat yfloat ztrinityRPG.Definitions.PropertyType type)
        {
            
int modelid = (int)type;
            var 
createdPickup = new DynamicPickup(modelid1, new Vector(xyz), 100f);
            
createdPickup.PickedUp += (senderargs) =>
            {
                
Console.WriteLine("The pickup was picked up.");
            };
        } 
My problem: whenever someome picks up the DynamicPickup, nothing happens! How would I go about fixing that?
Reply
#62

Another one of this projects eh. I do like the idea I hope you don't leave this project! Good Luck
Reply
#63

Quote:
Originally Posted by Sithis
Посмотреть сообщение
Sorry for all the questions, but here we go:

I have a class, public class Property. It represents a house. I have three public methods for creating different types of houses. The code for the most used struct (a player owned house) is as follows:

PHP код:
        public Property(int dbIDfloat xfloat yfloat zstring nametrinityRPG.Definitions.PropertyType typestring ownerfloat interiorxfloat interioryfloat interiorzint interiorid)
        {
            
createPickup(xyztype);
            
createLabel(xyz"Property: " name "\n Owner: " owner);
            
this.ownerName owner;
            
this.databaseID dbID;
            
this.interiorID interiorid;
            
this.interiorx interiorx;
            
this.interiory interiory;
            
this.interiorz interiorz;
        } 
The createPickup and createLabel methods are private methods for rendering the pickup and 3dtextlabel, both using the Streamer Wrapper.

PHP код:
        private void createPickup(float xfloat yfloat ztrinityRPG.Definitions.PropertyType type)
        {
            
int modelid = (int)type;
            var 
createdPickup = new DynamicPickup(modelid1, new Vector(xyz), 100f);
            
createdPickup.PickedUp += (senderargs) =>
            {
                
Console.WriteLine("The pickup was picked up.");
            };
        } 
My problem: whenever someome picks up the DynamicPickup, nothing happens! How would I go about fixing that?
Do other streamer callbacks work? If not , you might have forgot to load the streamer controller
https://github.com/ikkentim/SampShar...merTest.cs#L15
Reply
#64

Thanks for your reply.

Must I load the controller in the Property class or just in the general gamemode class?
Reply
#65

Quote:
Originally Posted by Sithis
Посмотреть сообщение
Thanks for your reply.

Must I load the controller in the Property class or just in the general gamemode class?
I suggest in you load it in your gamemode class. The LoadControllers method is meant to be used for that kind of stuff:

PHP код:

        
protected override void LoadControllers(ControllerCollection controllers)
        {
            
base.LoadControllers(controllers);
            
Streamer.Load(controllers);
            
// Load other controllers here as well, e.g.:
            // controllers.Remove<GtaPlayerController>();
            // controllers.Add(new PlayerController());
            
        

Reply
#66

Sweet, everything worked as you said! The performance is great, even a little faster than pawn from what I can see. What settings do you use in Sandcastle to generate your documentation? I'm thinking about opensourcing my gamemode after it is finished, tested and deployed in my own community.
Reply
#67

Quote:
Originally Posted by Sithis
Посмотреть сообщение
Sweet, everything worked as you said! The performance is great, even a little faster than pawn from what I can see. What settings do you use in Sandcastle to generate your documentation? I'm thinking about opensourcing my gamemode after it is finished, tested and deployed in my own community.
Glad to hear it.

I think I use the default sandcastle settings. Here is my configuration file: https://gist.github.com/ikkentim/c5fc26b7a2789e6dd898
Reply
#68

Is this thread-safe? Can I use async-await without worry?
Are there any drawbacks using this instead of pawn?
Reply
#69

Quote:
Originally Posted by lol1
Посмотреть сообщение
Is this thread-safe? Can I use async-await without worry?
Are there any drawbacks using this instead of pawn?
You can use async, but not worry free. For various reasons you can't call native functions from threads other than the main thread. This means you can not call most functions and properties of native wrappers(e.g. player, vehicle, textdraw, etc.) from other threads. Even printing to the console (Console.* methods) can't be used from other threads.

I have however made a Sync function:
PHP код:
Sync.Sync.Run(() => {
    
// do something on main thread
  
});
// - or -
var result await Sync.RunAsync(() => {
    
// do calls to native functions on the main thread and return the value...
    
return 0;
  });
// I know the method names of of Sync.Run and Sync.RunAsync are dodgy. They should be named the other way around. Will change this soon. 
I did some testing with asynchronous programming here:
https://github.com/ikkentim/SampShar...s/ASyncTest.cs

I advise only to use multi-threading for awaiting database access (and syncing directly afterwards) and for heavy calculations.


The only drawbacks I can think of is that most if not all scripts and plugins on these forums are made for use with pawn. Although most includes are easily converted, and the streamer plugin I've already written a wrapper for, you cannot simply copy and paste much from these forums without some thinking.

EDIT: side note: the console functions internally check whether it's called on the main thread. If not, it will automatically sync. This means you can use the console functions even from different threads.
Reply
#70

Is it possible to show me a short example how to make Player Commands with Arguments?
I tried to figure it out for a while now.
Would help me out big time.
Reply
#71

Quote:
Originally Posted by nighthammer
Посмотреть сообщение
Is it possible to show me a short example how to make Player Commands with Arguments?
I tried to figure it out for a while now.
Would help me out big time.
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 код:
        [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.
Reply
#72

Sounds good to me. Will try to port my gamemode.
Reply
#73

Quote:
Originally Posted by Sithis
Посмотреть сообщение
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 код:
        [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 код:
[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 код:
[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 код:
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 код:
[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 код:
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 код:
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 код:
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:

Код:
/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 код:
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 код:
//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 код:
[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 код:
[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 код:
// 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 код:
[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 код:
//Somewhere near you initialization code...
new MyAwesomeCommand(); 
Creating custom parameter attributes

Lets say you have a Street class
PHP код:
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 код:
    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 код:
[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 код:
//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 код:
[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 код:
[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 код:
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 код:
//Somewhere near you initialization code...
DetectedCommand.UsageFormat = (nameparameters) =>
{
    return 
"some usage message";

________

If you have any remaining questions, don't hesitate to ask.
Reply
#74

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
Reply
#75

Quote:
Originally Posted by Sithis
Посмотреть сообщение
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.
Reply
#76

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
Reply
#77

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
Reply
#78

Gamemode on c# faster than Gamemode on PAWN?
Reply
#79

I've never tested it perfectly, but yes it's faster.
Reply
#80

How to set encoding to "1251 windows-1251 ANSI Cyrillic; Cyrillic (Windows)" ?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)