Raised This Month: $51 Target: $400
 12% 

[CSS] Set player health on spawn, if they want


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ReLaX
SourceMod Donor
Join Date: Nov 2005
Location: Denamrk
Old 02-05-2014 , 11:39   [CSS] Set player health on spawn, if they want
Reply With Quote #1

Hi guys,

This is my very first attempt to create a plugin. But i have read on Source Pawn earlier and i have coding experience from PHP.

I am trying to make a plugin, that will set a players health to 150, if he has the custom6 flag, and if he has choose to get it, in the menu.

I have created a menu, where i want the player to be able to select Health boost on/off.
If he has selected On, next time he spawns he will get the boosed Health, and every time he spawns, until he enters the menu, to turn it off again.

It does not need to remember he choice if he disconnects, just for the current session (if it can remember it always, it will just be nice to have).


So far i have managed to create a menu, and the set health on spawn, if he has the custom6 flag - however, he does not have a choice right now, he will always have the boosted health.

Could you guys help me in the direction of what i need to do, to accomplish the above?

Code:
#include <sourcemod>

#define PLUGIN_VERSION "1.0"

public Plugin:myinfo = 
{
	name = "Test Plugin",
	author = "ReLaX",
	description = "Test Plugin",
	version = PLUGIN_VERSION,
	url = ""
}

public OnPluginStart()
{
	/* Creating ConVars */
	CreateConVar( "sm_testmenu_version", PLUGIN_VERSION, "Testmenu Plugin Version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY );
	
	/* Registering Commands */
	RegConsoleCmd("sm_testmenu", test_menu);
	
	/* Hooks */
	HookEvent("player_spawn", Event_Spawn);
}

/* This is a event that will change properties on player spawn. */
public Event_Spawn(Handle:event, const String:name[], bool:dontBroadcast)
{
	new valueHealth = 150;
	
	new clientId = GetClientOfUserId(GetEventInt(event, "userid"));
	
	if (clientId != 0 && IsClientInGame(clientId) && IsPlayerAlive(clientId))
	{
		/* Set Health */
		if (CheckCommandAccess(clientId, "test_health", ADMFLAG_CUSTOM6, false))
		{
			SetEntityHealth(clientId, valueHealth);
		}
	}
	
	return bool:Plugin_Handled;
}
 
public MenuHandler1(Handle:menu, MenuAction:action, param1, param2)
{
	/* If an option was selected, tell the client about the item. */
	if (action == MenuAction_Select)
	{
		new String:info[32];
		new bool:found = GetMenuItem(menu, param2, info, sizeof(info));
		PrintToConsole(param1, "You selected item: %d (found? %d info: %s)", param2, found, info);
	}
	/* If the menu was cancelled, print a message to the server about it. */
	else if (action == MenuAction_Cancel)
	{
		PrintToServer("Client %d's menu was cancelled.  Reason: %d", param1, param2);
	}
	/* If the menu has ended, destroy it */
	else if (action == MenuAction_End)
	{
		CloseHandle(menu);
	}
}
 
public Action:test_menu(client, args)
{
	new Handle:menu = CreateMenu(MenuHandler1);
	SetMenuTitle(menu, "Test Menu");
	AddMenuItem(menu, "health", "Health");
	SetMenuExitButton(menu, true);
	DisplayMenu(menu, client, 20);
 
	return Plugin_Handled;
}
The "menu" code above is just a modified version of an example from the SourceMod Documentation
__________________
//ReLaX - Get free help for your Counter-Strike: Source server in Danish!
Microsoft Certified IT Professional

Last edited by ReLaX; 02-05-2014 at 11:40.
ReLaX is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 02-05-2014 , 15:53   Re: [CSS] Set player health on spawn, if they want
Reply With Quote #2

- Global array variable bool:g_bBoost[MAXPLAYERS+1];,
for store boolean value by using client indexs as array index.
-snip-

- Changed RegConsoleCmd to RegAdminCmd, can be override to different flag(s) or public from admin_overrides.cfg

Code:
#include <sourcemod>

#define PLUGIN_VERSION "1.0"

public Plugin:myinfo = 
{
	name = "Test Plugin",
	author = "ReLaX",
	description = "Test Plugin",
	version = PLUGIN_VERSION,
	url = ""
}

new bool:g_bBoost[MAXPLAYERS+1];

public OnPluginStart()
{
	/* Creating ConVars */
	CreateConVar( "sm_testmenu_version", PLUGIN_VERSION, "Testmenu Plugin Version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY );
	
	/* Registering Commands */
	//RegConsoleCmd("sm_testmenu", test_menu);
	RegAdminCmd("sm_testmenu", test_menu, ADMFLAG_CUSTOM6);
	
	/* Hooks */
	HookEvent("player_spawn", Event_Spawn);
}

/* This is a event that will change properties on player spawn. */
public Event_Spawn(Handle:event, const String:name[], bool:dontBroadcast)
{
	new valueHealth = 150;
	
	new clientId = GetClientOfUserId(GetEventInt(event, "userid"));
	
	//if (clientId != 0 && IsClientInGame(clientId) && IsPlayerAlive(clientId))
	if (GetClientTeam(clientId) >= 2) // player spawn team T = 2 or CT = 3
	{
		/* Set Health */
		//if (CheckCommandAccess(clientId, "test_health", ADMFLAG_CUSTOM6, false))
		if (g_bBoost[clientId])
		{
			SetEntityHealth(clientId, valueHealth);
		}
	}
	else // Player spawn on map first time after connecting, in team NONE = 0. Lets disable boost.
	{
		g_bBoost[clientId] = false;
	}
	
	return bool:Plugin_Handled;
}
 
public MenuHandler1(Handle:menu, MenuAction:action, param1, param2)
{
	/* If an option was selected, tell the client about the item. */
	if (action == MenuAction_Select)
	{
		if(param2 == 1) // First MenuItem
		{
			g_bBoost[param1] = !g_bBoost[param1]; // Toggle on/off
		}

		new String:info[32];
		new bool:found = GetMenuItem(menu, param2, info, sizeof(info));
		PrintToConsole(param1, "You selected item: %d (found? %d info: %s)", param2, found, info);
		test_menu(param1, 0): // Re-open same menu using cmd callback
	}
	/* If the menu was cancelled, print a message to the server about it. */
	else if (action == MenuAction_Cancel)
	{
		PrintToServer("Client %d's menu was cancelled.  Reason: %d", param1, param2);
	}
	/* If the menu has ended, destroy it */
	else if (action == MenuAction_End)
	{
		CloseHandle(menu);
	}
}
 
public Action:test_menu(client, args)
{
	new Handle:menu = CreateMenu(MenuHandler1);
	SetMenuTitle(menu, "Test Menu");
	//AddMenuItem(menu, "health", "Health");

	new String:text[30];
	Format(text, sizeof(text), "Health Boost - %s", g_bBoost[client] ? "Disable":"Enable")
	AddMenuItem(menu, "health", text);
	SetMenuExitButton(menu, true);
	DisplayMenu(menu, client, 20);
 
	return Plugin_Handled;
}

Last edited by Bacardi; 02-05-2014 at 15:54.
Bacardi is offline
ReLaX
SourceMod Donor
Join Date: Nov 2005
Location: Denamrk
Old 02-05-2014 , 16:20   Re: [CSS] Set player health on spawn, if they want
Reply With Quote #3

Thanks Bacardi!

I will take a look at this tomorrow, it seems like a better way then what i have done right now.

i found this https://forums.alliedmods.net/showthread.php?t=233860 which i tried to implement in my code.

But i think your way looks a bit smoother.
__________________
//ReLaX - Get free help for your Counter-Strike: Source server in Danish!
Microsoft Certified IT Professional
ReLaX is offline
ReLaX
SourceMod Donor
Join Date: Nov 2005
Location: Denamrk
Old 02-06-2014 , 12:56   Re: [CSS] Set player health on spawn, if they want
Reply With Quote #4

Just to update, it works very well, i had to make a very small change, but that was easy to figure out

Thanks for your help!

Code:
if(param2 == 1) // First MenuItem
should be changed to
Code:
if(param2 == 0) // First MenuItem
as the first select in the menu returned 0

Luckly me, i could see that in the console because of this line
Code:
PrintToConsole(param1, "You selected item: %d (found? %d info: %s)", param2, found, info);
__________________
//ReLaX - Get free help for your Counter-Strike: Source server in Danish!
Microsoft Certified IT Professional

Last edited by ReLaX; 02-06-2014 at 12:57. Reason: Tags added
ReLaX is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 02-06-2014 , 13:07   Re: [CSS] Set player health on spawn, if they want
Reply With Quote #5

ou goodie, I did assume it was 1, but seems wasn't. I didn't have time to test (and install test server + SM in this computer)
Bacardi is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 23:08.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode