Skip to content

Simple bomb spawner

KissLick edited this page May 15, 2016 · 10 revisions

Say we want to add "Spawn bomb" item into TG menu. First of all, you will need to know how to create a plugin for SourceMod (at least basic one).


Now we can move to the actual menu item module. So let's start from scratch.

#include <sourcemod>

public Plugin:myinfo =
{
	// You should name your plugin "[TG] something", so they are grouped together in plugin list
	name = "[TG] BombSpawner",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

Now you will want to add TeamGames include.

#include <sourcemod>
#include <teamgames>

public Plugin:myinfo =
{
	name = "[TG] BombSpawner",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

At this point, you need to register your menu item into TeamGames.

#include <sourcemod>
#include <teamgames>

// Just for better work with menu item id ;-)
#define ITEM_ID	"BombSpawner-Raska"

public Plugin:myinfo =
{
	name = "[TG] BombSpawner",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

// We are able to register the menu item here
public OnLibraryAdded(const String:name[])
{
	if (StrEqual(name, "TeamGames")) {
		// Register your menu item
		TG_RegMenuItem(ITEM_ID);
	}
}

// Remove/UnRegister the menu item when this plugin unloads
public OnPluginEnd()
{
	TG_RemoveMenuItem(ITEM_ID);
}

To let TeamGames know your menu item's name, just use forward TG_AskModuleName. This forward is called everytime TeamGames needs to know the module (game or menu item) name (e.g. somebody opened TG menu). And of course, everything in TeamGames should be translatable, so use translations, it's really simple ;-)

#include <sourcemod>
#include <teamgames>

#define ITEM_ID	"BombSpawner-Raska"

public Plugin:myinfo =
{
	name = "[TG] BombSpawner",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

public OnPluginStart()
{
	// Everything in TeamGames should be in translation files
	LoadTranslations("TG.BombSpawner.phrases");
}

public OnLibraryAdded(const String:name[])
{
	if (StrEqual(name, "TeamGames")) {
		TG_RegMenuItem(ITEM_ID);
	}
}

public OnPluginEnd()
{
	TG_RemoveMenuItem(ITEM_ID);
}

// TeamGames core ask for game/menu item name (probably somebody opened TG menu)
public TG_AskModuleName(TG_ModuleType:type, const String:id[], client, String:name[], maxSize, &TG_MenuItemStatus:status)
{
	// Check if it's your menu item
	if (type == TG_MenuItem && StrEqual(id, ITEM_ID)) {
		Format(name, maxSize, "%T", "SpawnBomb", client);

		if (client > 0 && client <= MaxClients && IsClientInGame(client)) {
			
			// deactivate the menu item when player has less than 100 HP
			if (GetClientHealth(client) < 100) {
				status = TG_Inactive;
			}

			// don't show the menu item when player has less than 50 HP
			if (GetClientHealth(client) < 50) {
				status = TG_Disabled;
			}
		}
	}
}

The next step is the use of forward TG_OnMenuSelected. Here we can "catch" when somebody chose our menu item in TG menu.

#include <sourcemod>
#include <teamgames>

#define ITEM_ID	"BombSpawner-Raska"

public Plugin:myinfo =
{
	name = "[TG] BombSpawner",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

public OnPluginStart()
{
	LoadTranslations("TG.BombSpawner.phrases");
}

public OnLibraryAdded(const String:name[])
{
	if (StrEqual(name, "TeamGames")) {
		TG_RegMenuItem(ITEM_ID);
	}
}

public OnPluginEnd()
{
	TG_RemoveMenuItem(ITEM_ID);
}

public TG_AskModuleName(TG_ModuleType:type, const String:id[], client, String:name[], maxSize, &TG_MenuItemStatus:status)
{
	if (type == TG_MenuItem && StrEqual(id, ITEM_ID)) {
		Format(name, maxSize, "%T", "SpawnBomb", client);

		if (client > 0 && client <= MaxClients && IsClientInGame(client)) {
			if (GetClientHealth(client) < 100) {
				status = TG_Inactive;
			}

			if (GetClientHealth(client) < 50) {
				status = TG_Disabled;
			}
		}
	}
}

// Somebody selected menu item/menu item in TG menu
public TG_OnMenuSelected(TG_ModuleType:type, const String:id[], TG_GameType:gameType, client)
{
	// Check if it's your menu item
	if (type == TG_MenuItem && StrEqual(id, ITEM_ID)) {
		new Float:position[3];
		GetClientAbsOrigin(client, position);

		new bomb = CreateEntityByName("weapon_c4");
		if (bomb != INVALID_ENT_REFERENCE) {
			DispatchSpawn(bomb);
			TeleportEntity(bomb, position, NULL_VECTOR, NULL_VECTOR);
		}
	}
}