pointerboy / PySAMP

Use the SAMP API in a python script to create a python-based gamemode for 0.3.7.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PySAMP

Using PySAMP in your SA-MP server allows you to create gamemodes with the python language. All API-functions, callbacks and constants except timers and http functions can be accessed in python. In case of the call-by-reference functions like GetPlayerName are returning the value instead of using a reference, since call-by-reference isn't possible in python. The following example shows the difference.

public OnPlayerConnect(playerid)
{
  new name[MAX_PLAYER_NAME], string[MAX_PLAYER_NAME + 24];
  GetPlayerName(playerid, name, MAX_PLAYER_NAME);
  format(string, sizeof(string), "%s has joined the server.", name);
  SendClientMessageToAll(0x000000FF, string);
}
from samp import *

def OnPlayerConnect(playerid):
    name = GetPlayerName(playerid, Const('MAX_PLAYER_NAME'))
    SendClientMessageToAll(0x000000FF,  '%s has joined the server.'.format(name))
    return 1

As you can see, all referenced return values are removed and instead the method returns either a value or a tuple. The corresponding python gamemode has to be saved as gamemode.py in a python/ subfolder of your server directory. By the way, I imported the samp module here, which is actually a python module. You can find it in gamemodes/empty/. It wraps the SA-MP functions and handles the encoding and decoding of strings, so you don't have to bother. Caution: with this module, you also get the decode method. Use it to decode the bytestrings as seen in the example gamemode in gamemodes/empty/.

Using

Make sure, that you installed the 32-bit Python 3.5m version on your server! For debian and ubuntu, this should work:

sudo dpkg --add-architecture i386
sudo apt-get update

and then:

sudo apt install python3-dev:i386

Compiling

If the uploaded binaries don't suite your needs, you might have to compile the project on your own. You can also create an issue, so I can compile it for your system, just mention your system architecture.

So, if you want to compile it on your own, note the following things.

  • You read the SAMPGDK tutorial.
  • Python (3.5 Linux)/(3.6 Windows) (32 bit version!) is installed on your computer
  • You copied the sampsdk and sampgdk files into the src folder, as defined in CMakeLists.txt

Use cmake to create a project and then compile it as you're used to it.

Thanks to

  • SA:MP Team for developing SA:MP
  • Zeex for developing the SAMPGDK which is used by pySAMP
  • Python Software Foundation

About

Use the SAMP API in a python script to create a python-based gamemode for 0.3.7.

License:Apache License 2.0


Languages

Language:C++ 84.9%Language:Python 14.7%Language:CMake 0.3%Language:C 0.0%