[Tutorial] Installing & Setting up SAMPGDK
#1

Installing & Setting up SAMPGDK

SAMPGDK is a library written by Zeex which allows you to write SAMP Gamemodes in C/++. It provides all the functions and callbacks which are available in PAWN except those which conflict with the C/++ standard libraries. I saw a post today(13/5/2015) where someone asked how to install SAMPGDK. I think we need to have a SAMPGDK tutorial especially for those who are new to SAMP Plugin Development.Those who are experienced with Visual Studio and C++ would probably figure this out themselves. This tutorial is exclusively for those who haven't figured out how to install SAMPGDK(or for those who are installing for the first time).

Official SAMPGDK Thread by Zeex
Official SAMPGDK GitHub Repo

SAMP SDK

Installing
I am assuming that you know how to create a project for writing a plugin.If not, read this first to have some idea about what I am talking about in this tutorial.

Another assumption that I will make is that you are using Visual Studio 2010.However if you are not using VS2010, you can still do it if you spend some time with the IDE.

First download the SAMP SDK and extract all the files to your project directory.Now download Win32 version of SAMP GDK from GitHub.Once its downloaded, extract all the files to some temporary folder.You should be able to find sampgdk4.lib in the lib folder.Copy it and paste it in your project directory.

Now follow the instructions provided below
Right Click on your project name in the Solution Explorer and click Properties.
Go to Linker->Input
Edit the text in Additional Dependencies and add sampgdk.lib

Setting up SAMPGDK
If you have browsed the sampgdk folder(include/sampgdk/), you might have seen lots of files.Copy thie sampgdk folder to your project directory.You must include them as if you were writing you gamemode in PAWN.You must also include these too.
Код:
#include <sampgdk/core.h>
#include <sampgdk/sdk.h>
You may get an error that the include is not found then add sampgdk folder to the include directory list.If you do not know how, refer to this page.

You get another version of SAMPGDK known as SAMPGDK- Amalgamated in which all the separate includes are packed into a single include.

If you are using this version, you needn't include files such as a_players,a_samp,etc.But you'll need to include this

Код:
#include "sampgdk/sampgdk.h"
Here too, copy the sampgdk folder to your project directory.

After including the necessary files, add this line given below to your script.It's optional.If this line is not added then you'll have to add the namespace scope 'sampgdk' every time you want to call logprintf.Do not use "using sampgdk;" because few functions will conflict with SAMP SDK functions and the script won't compile.

Код:
using sampgdk::logprintf;
logprintf is a function which will print text to the server window.You cannot use cout since it will print it to the standard output stream(console).

Add the following code after including the necessary files.Change sampgdk::logprintf to logprintf if you are using "using sampgdk::logprintf".

Pastebin Link for the code below.
Код:
#define PLUGIN_VERSION "1.0"
#define PLUGIN_AUTHOR "Your Name"

extern void *pAMXFunctions;
/**********************************************************************************************************************************/
PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports() 
{
	return sampgdk::Supports() | SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES | SUPPORTS_PROCESS_TICK;
}
PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData) 
{
	pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
	sampgdk::logprintf("\n\n* [Your Plugin Name] %s by %s Loaded",PLUGIN_VERSION,PLUGIN_AUTHOR);	
	return sampgdk::Load(ppData);
}
PLUGIN_EXPORT void PLUGIN_CALL Unload()
{	
	sampgdk::logprintf("\n\n* [Your Plugin Name] %s by %s Unloaded",PLUGIN_VERSION,PLUGIN_AUTHOR);
	sampgdk::Unload();
}
/**********************************************************************************************************************************/
PLUGIN_EXPORT void PLUGIN_CALL ProcessTick() 
{
	sampgdk::ProcessTick();
}
/**********************************************************************************************************************************/
AMX_NATIVE_INFO PluginNatives[] =
{
	{0, 0}
};
PLUGIN_EXPORT int PLUGIN_CALL AmxLoad( AMX *amx ) 
{
	return amx_Register(amx, PluginNatives, -1);
}
PLUGIN_EXPORT int PLUGIN_CALL AmxUnload( AMX *amx ) 
{
	return AMX_ERR_NONE;
}
Most of the code is self explanatory if you have read the Plugin Development Guide.

List of changes with sampgdk and without sampgdk:
  • Added sampgdk::Supports() to PLUGIN_CALL Supports()
  • Called sampgdk::Load(ppData); to PLUGIN_CALL Load
  • Added sampgdk::Unload(); to PLUGIN_CALL Unload
  • Added sampgdk::ProcessTick(); to PLUGIN_CALL ProcessTick
You have successfully installed SAMPGDK.You can now use SAMP Callbacks and native functions directly in your script.

How to use SAMP Callbacks?
Код:
PLUGIN_EXPORT bool PLUGIN_CALL CALLBACK_NAME(CALLBACK_PARAMETERS) 
{
    return true;
}
You must replace CALLBACK_NAME with the callback which you want to use such as OnGameModeInit,OnPlayerConnect,etc.CALLBACK_PARAME TERS are the arguments that are passed to the function.The example given below will make it clear.

Код:
PLUGIN_EXPORT bool PLUGIN_CALL OnPlayerConnect(int playerid) 
{
    return true;
}
For each callback you use, you must add it to EXPORTS in the module definition file.The example given below should make this clear.
Код:
EXPORTS
	Supports
	Load
	Unload
	AmxLoad
	AmxUnload
	ProcessTick
        OnGameModeInit
	OnPlayerConnect
We need to add them so that applications(samp server) that will use our plugin can touch these callbacks.If you do not add them to the EXPORTS then they will remain private to the DLL.

How to use SAMP Natives?
Its extremely simple to use SAMP natives using sampgdk.All you have to do is call the native as if you were calling it in PAWN.The example below should make this clear.

Код:
PLUGIN_EXPORT bool PLUGIN_CALL OnGameModeInit() 
{
    SetGameModeText("Gamemode");
    AddPlayerClass(0, 1958.3783f, 1343.1572f, 15.3746f, 269.1425f, 0, 0, 0, 0, 0, 0);
    return true;
}
PLUGIN_EXPORT bool PLUGIN_CALL OnPlayerConnect(int playerid) 
{
    SendClientMessage(playerid,-1,"Welcome to YOUR SERVER");
    return true;
}
Similarly you can use almost any native in your script.

Troubleshooting:
*Plugin does not conform to the architecture
You forgot to add create a module definition file or you did not tell the linker about it

*Server crashed due to an unknown error
Quote:

Server crashed due to an unknown error
[10:39:47] [debug] Native backtrace:
[10:39:47] [debug] #0 59822483 in amx_Register () from plugins\TEST.DLL
[10:39:47] [debug] #1 59822d9e in AmxLoad () from plugins\TEST.DLL
[10:39:47] [debug] #2 00469a75 in ?? () from samp-server.exe

You must have forgotten to add
Код:
extern void *pAMXFunctions;
or forgot to initialize it.

Solution:
Add this at the topc of your code
Код:
extern void *pAMXFunctions;
Add this under Load Function
Код:
pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
*unresolved external symbol
Quote:

1>main.obj : error LNK2019: unresolved external symbol __imp__sampgdk_Supports referenced in function "unsigned int __cdecl sampgdk::Supports(void)" (?Supports@sampgdk@@YAIXZ)
1>main.obj : error LNK2019: unresolved external symbol __imp__sampgdk_Load referenced in function "bool __cdecl sampgdk::Load(void * *)" (?Load@sampgdk@@YA_NPAPAX@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__sampgdk_vlogprintf referenced in function "void __cdecl sampgdk::logprintf(char const *,...)" (?logprintf@sampgdk@@YAXPBDZZ)
1>main.obj : error LNK2019: unresolved external symbol __imp__sampgdk_Unload referenced in function "void __cdecl sampgdk::Unload(void)" (?Unload@sampgdk@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol __imp__sampgdk_ProcessTick referenced in function "void __cdecl sampgdk::ProcessTick(void)" (?ProcessTick@sampgdk@@YAXXZ)

You forgot to add sampgdk4.lib or tell the linker about this file.If the error continuous even after adding it to the linker.I have no idea what to do.I re-installed Visual Studio to fix the problem.

Additional References:
To know more about SAMPGDK check SAMPGDK Documentation.If you still have the sampgdk zip, you will find the documentation at share/doc/sampgdk/html/index.html.

To know how to add your own natives or custom callbacks to the plugin refer to Plugin Development Guide.
Reply


Messages In This Thread
Installing & Setting up SAMPGDK - by Yashas - 12.05.2015, 18:58
Re: Installing & Setting up SAMPGDK - by RaeF - 12.05.2015, 22:04
Re: Installing & Setting up SAMPGDK - by vermaritt - 13.05.2015, 02:22
Re: Installing & Setting up SAMPGDK - by Yashas - 13.05.2015, 04:41
Re: Installing & Setting up SAMPGDK - by Sithis - 21.05.2015, 08:25
Re: Installing & Setting up SAMPGDK - by FplayerGR - 25.05.2015, 07:02
Re: Installing & Setting up SAMPGDK - by Sasino97 - 16.03.2016, 18:04
Re: Installing & Setting up SAMPGDK - by Yashas - 22.03.2016, 16:42

Forum Jump:


Users browsing this thread: 1 Guest(s)