Raised This Month: $ Target: $400
 0% 

set_user_health() not doing anything


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
newerakb
New Member
Join Date: Aug 2009
Old 08-12-2009 , 22:25   set_user_health() not doing anything
Reply With Quote #1

Hey, just started playing around with my first AMX MOD X mod, and true to form, I'm stuck. Just trying out a simple script that picks a random player on each team and puts them at 5 health. I'm not getting any errors, and it displays the names of the chosen players correctly, but it doesn't actually change anyones health.

Is there something else I have to do after calling set_user_health to actually apply the health change? Or is something else immediately resetting the health to 100?

Any help is appreciated, thanks.

Code:
#include <amxmodx>
#include <amxmisc>
#include <fun>

#define PLUGIN "12-Pack"
#define VERSION "1.0"
#define AUTHOR "newerakb"


public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_event("HLTV", "event_new_round", "a", "1=0", "2=0")
}

public event_new_round()
{
	new playerList[32]
	new playerCount, ctCount, tCount
	new player, team
	
	ctCount = 0
	tCount = 0
	
	get_players(playerList, playerCount)
	
	log_amx("playerCount: %d", playerCount)
	
	new i
	for (i = 0; i < playerCount; i++)
	{
		player = playerList[i]
		team = get_user_team(player)
		
		if (team == 1)
		{
			tCount++
		}
		else if (team == 2)
		{
			ctCount++
		}
	}
	
	new ctHeroIndex, tHeroIndex
	new ctHero, tHero
	new currentCT, currentT
	
	ctHeroIndex = random_num(0, ctCount - 1)
	tHeroIndex = random_num(0, tCount - 1)
	
	currentCT = 0
	currentT = 0
	
	tHero = 0
	ctHero = 0
	
	for (i = 0; i < playerCount; i++)
	{
		player = playerList[i]
		team = get_user_team(player)
		
		if (team == 1)
		{
			if (currentT == tHeroIndex)
			{
				tHero = player
			}
			
			currentT++
		}
		else if (team == 2)
		{
			if (currentCT == ctHeroIndex)
			{
				ctHero = player
			}
			
			currentCT++
		}
	}
	
	log_amx("tHero: %d", tHero)
	log_amx("ctHero: %d", ctHero)
	
	new name[20]
	
	if (tHero > 0)
	{
		set_user_health(tHero, 12)
		
		get_user_name(tHero, name, 20)
		log_amx("tHero Name: %s", name)
	}
	
	if (ctHero > 0)
	{
		set_user_health(ctHero, 12)
		
		get_user_name(ctHero, name, 20)
		log_amx("ctHero Name: %s", name)
	}
	
	return PLUGIN_HANDLED
}
newerakb is offline
newerakb
New Member
Join Date: Aug 2009
Old 08-12-2009 , 22:45   Re: set_user_health() not doing anything
Reply With Quote #2

Figured it out. Had to set a timer to delay the health change slightly. New code for reference:

Code:
#include <amxmodx>
#include <amxmisc>
#include <fun>

#define PLUGIN "12-Pack"
#define VERSION "1.0"
#define AUTHOR "newerakb"


public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_event("HLTV", "event_new_round", "a", "1=0", "2=0")
}

public event_new_round()
{
	new playerList[32]
	new playerCount, ctCount, tCount
	new player, team
	
	ctCount = 0
	tCount = 0
	
	get_players(playerList, playerCount)
	
	log_amx("playerCount: %d", playerCount)
	
	new i
	for (i = 0; i < playerCount; i++)
	{
		player = playerList[i]
		team = get_user_team(player)
		
		if (team == 1)
		{
			tCount++
		}
		else if (team == 2)
		{
			ctCount++
		}
	}
	
	new ctHeroIndex, tHeroIndex
	new ctHero, tHero
	new currentCT, currentT
	
	ctHeroIndex = random_num(0, ctCount - 1)
	tHeroIndex = random_num(0, tCount - 1)
	
	currentCT = 0
	currentT = 0
	
	tHero = 0
	ctHero = 0
	
	for (i = 0; i < playerCount; i++)
	{
		player = playerList[i]
		team = get_user_team(player)
		
		if (team == 1)
		{
			if (currentT == tHeroIndex)
			{
				tHero = player
			}
			
			currentT++
		}
		else if (team == 2)
		{
			if (currentCT == ctHeroIndex)
			{
				ctHero = player
			}
			
			currentCT++
		}
	}
	
	new params[2]
	params[0] = ctHero
	params[1] = tHero
	
	set_task(0.5, "setHealth", 0, params, 2, "a", 1)
	
	return PLUGIN_HANDLED
}

public setHealth(params[])
{
	new ctHero = params[0]
	new tHero = params[1]
	
	log_amx("tHero: %d", tHero)
	log_amx("ctHero: %d", ctHero)
	
	new name[20]
	new health
	
	if (tHero > 0)
	{
		set_user_health(tHero, 12)
		
		get_user_name(tHero, name, 20)
		log_amx("tHero Name: %s", name)
		
		health = get_user_health(tHero)
		log_amx("tHero Health: %d", health)
	}
	
	if (ctHero > 0)
	{
		set_user_health(ctHero, 12)
		
		get_user_name(ctHero, name, 20)
		log_amx("ctHero Name: %s", name)
		
		health = get_user_health(ctHero)
		log_amx("ctHero Health: %d", health)
	}
}
newerakb is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 08-13-2009 , 00:25   Re: set_user_health() not doing anything
Reply With Quote #3

http://forums.alliedmods.net/showthread.php?t=42159
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
newerakb
New Member
Join Date: Aug 2009
Old 08-13-2009 , 21:31   Re: set_user_health() not doing anything
Reply With Quote #4

Thanks. I had read that (that's where I got the syntax for the HTLV event), but I missed the part about not being called the first round. Whoops.
newerakb 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 15:15.


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