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

[TF2] Multiplying Percents (Decimals) & EmitSound


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DarkPyro
Member
Join Date: Aug 2013
Old 08-14-2019 , 19:38   [TF2] Multiplying Percents (Decimals) & EmitSound
Reply With Quote #1

Hi! I've been messing around with a plugin for my tf2 server and I needed to mimic the characteristics of a health kit dropping on death, only except I can't really do that correctly with the small and medium health kits because for some reason when I try and take a percentage of health from a player (e.g. 125 * 0.2) it gives them 112500... health. The only reason I'm creating a "physical" prop entity instead of the actual med kit entity is because the med kit dropped has a custom model, and when Halloween is forced on, it overrides that model. No plugin that has tried to fix the game overriding the health model has been successful, so I opted to go this path, and I'm very close to having it perfect, just these 2 issues are present. I'm more of a glua coder so some of my old habits don't work in SourcePawn, plus I haven't coded in a really long time.

Yes, this code is kinda poop and I'm embarrassed over sharing how much copy paste happened but I just need a working plugin, nothing more or less

Code:
(iEntity = client)

--

new MaxHealth = GetEntData( iEntity, FindDataMapOffs( iEntity, "m_iMaxHealth" ), 4 );
SetEntProp( iEntity, Prop_Send, "m_iHealth", MaxHealth * 0.2, 4 );
Then the 2nd issue is with EmitSound. The sound is precached during map start and is downloaded correctly and plays fine with other versions of this plugin, including being able to play the sound using the "play items/asdf.wav" command. But, for some reason, whenever I use the version that I edited, it just will not play the sound no matter what I do.

Code:
(iEntity = client)
(iMushroom = the health prop entity being dropped)

--

new Float:pos[ 3 ];
decl String:ModelName[ PLATFORM_MAX_PATH ];

GetEntPropString( iMushroom, Prop_Data, "m_ModelName", ModelName, sizeof( ModelName ) );

if ( StrEqual( MDL_MUSHSMALL, ModelName, false ) )
{
	GetEntPropVector( iMushroom, Prop_Send, "m_vecOrigin", pos );
	EmitSoundToAll( SND_MUSH, 0, _, _, _, _, _, _, pos );
	SetEntProp( iEntity, Prop_Send, "m_iHealth", 65, 4 );
	AcceptEntityInput( iMushroom, "Kill" );
}
I've spent hours trying to do this without asking for help but I've spent so much time into such a non-essential plugin, I need advice lol. Thanks for reading!
__________________



Last edited by DarkPyro; 08-14-2019 at 19:40.
DarkPyro is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 08-14-2019 , 20:00   Re: [TF2] Multiplying Percents (Decimals) & EmitSound
Reply With Quote #2

Here's an example of what I use for EmitSoundToAll...

Code:
#define TAPS "/misc/taps_02.wav"

public OnMapStart()
{
     PrecacheSound(TAPS);
}

and when you need to play the sound within your code, use:

     EmitSoundToAll(TAPS);
PC Gamer is offline
Sreaper
髪を用心
Join Date: Nov 2009
Old 08-14-2019 , 20:21   Re: [TF2] Multiplying Percents (Decimals) & EmitSound
Reply With Quote #3

Have you already tried this plugin for the models? https://forums.alliedmods.net/showthread.php?p=2416912

I tested it back when it came out and it seemed to have worked fine.

Last edited by Sreaper; 08-14-2019 at 20:21.
Sreaper is offline
kking117
Junior Member
Join Date: Jan 2017
Location: Australia
Old 08-14-2019 , 21:30   Re: [TF2] Multiplying Percents (Decimals) & EmitSound
Reply With Quote #4

Quote:
Originally Posted by DarkPyro View Post
I try and take a percentage of health from a player (e.g. 125 * 0.2) it gives them 112500... health.

Code:
(iEntity = client)

--

new MaxHealth = GetEntData( iEntity, FindDataMapOffs( iEntity, "m_iMaxHealth" ), 4 );
SetEntProp( iEntity, Prop_Send, "m_iHealth", MaxHealth * 0.2, 4 );
The reason it's giving a large amount of health is because it's reading MaxHealth * 0.2 as a float, but it's reading the float as an integer because health is an integer (a tag mismatch). To turn the float into an integer you must turn it into a whole number afterwards.

Example:
Code:
SetEntProp(iEntity, Prop_Send, "m_iHealth", RoundToNearest(MaxHealth * 0.2));
Also in TF2 getting the m_iMaxHealth directly from the player is not recommended because it doesn't take into account attributes that modify max health. I'd suggest using this instead:

Code:
stock GetClientMaxHealth(client)
{
	return GetEntProp(GetPlayerResourceEntity(), Prop_Send, "m_iMaxHealth", _, client);
}
kking117 is offline
DarkPyro
Member
Join Date: Aug 2013
Old 08-14-2019 , 23:49   Re: [TF2] Multiplying Percents (Decimals) & EmitSound
Reply With Quote #5

All these replies are great! Thank you @PC Gamer, @Sreaper, and @kking117. I finally got it to a point where it's 100% perfect for me, I fixed the sound and rounded the decimals. Although, there's one issue with GetClientMaxHealth() where it spams relentless errors for whatever reason. It works fine, but I don't know why it would be doing that. I am just reloading the plugin so it could just be because the map hasn't restarted. Will post an update on that once I restart the map and stuff.

Code:
L 08/14/2019 - 23:42:21: [SM] Exception reported: Element 361 is out of bounds (Prop m_iMaxHealth has 34 elements).
L 08/14/2019 - 23:42:21: [SM] Blaming: tf2-mushroom-health.smx
L 08/14/2019 - 23:42:21: [SM] Call stack trace:
L 08/14/2019 - 23:42:21: [SM]   [0] GetEntProp
L 08/14/2019 - 23:42:21: [SM]   [1] Line 301, /home/groups/sourcemod/upload_tmp/phpmSsr8n.sp::GetClientMaxHealth
L 08/14/2019 - 23:42:21: [SM]   [2] Line 187, /home/groups/sourcemod/upload_tmp/phpmSsr8n.sp::MushroomTouch
L 08/14/2019 - 23:42:21: [SM] Exception reported: Element 1460 is out of bounds (Prop m_iMaxHealth has 34 elements).
L 08/14/2019 - 23:42:21: [SM] Blaming: tf2-mushroom-health.smx
L 08/14/2019 - 23:42:21: [SM] Call stack trace:
L 08/14/2019 - 23:42:21: [SM]   [0] GetEntProp
L 08/14/2019 - 23:42:21: [SM]   [1] Line 301, /home/groups/sourcemod/upload_tmp/phpmSsr8n.sp::GetClientMaxHealth
L 08/14/2019 - 23:42:21: [SM]   [2] Line 187, /home/groups/sourcemod/upload_tmp/phpmSsr8n.sp::MushroomTouch
L 08/14/2019 - 23:42:24: [SM] Exception reported: Element 1570 is out of bounds (Prop m_iMaxHealth has 34 elements).
L 08/14/2019 - 23:42:24: [SM] Blaming: tf2-mushroom-health.smx
L 08/14/2019 - 23:42:24: [SM] Call stack trace:
L 08/14/2019 - 23:42:24: [SM]   [0] GetEntProp
L 08/14/2019 - 23:42:24: [SM]   [1] Line 301, /home/groups/sourcemod/upload_tmp/phpmSsr8n.sp::GetClientMaxHealth
L 08/14/2019 - 23:42:24: [SM]   [2] Line 187, /home/groups/sourcemod/upload_tmp/phpmSsr8n.sp::MushroomTouch
L 08/14/2019 - 23:42:32: [SM] Exception reported: Element 1511 is out of bounds (Prop m_iMaxHealth has 34 elements).
L 08/14/2019 - 23:42:32: [SM] Blaming: tf2-mushroom-health.smx
L 08/14/2019 - 23:42:32: [SM] Call stack trace:
L 08/14/2019 - 23:42:32: [SM]   [0] GetEntProp
L 08/14/2019 - 23:42:32: [SM]   [1] Line 301, /home/groups/sourcemod/upload_tmp/phpmSsr8n.sp::GetClientMaxHealth
L 08/14/2019 - 23:42:32: [SM]   [2] Line 187, /home/groups/sourcemod/upload_tmp/phpmSsr8n.sp::MushroomTouch
L 08/14/2019 - 23:42:32: [SM] Exception reported: Element 1460 is out of bounds (Prop m_iMaxHealth has 34 elements).
L 08/14/2019 - 23:42:32: [SM] Blaming: tf2-mushroom-health.smx
L 08/14/2019 - 23:42:32: [SM] Call stack trace:
L 08/14/2019 - 23:42:32: [SM]   [0] GetEntProp
L 08/14/2019 - 23:42:32: [SM]   [1] Line 301, /home/groups/sourcemod/upload_tmp/phpmSsr8n.sp::GetClientMaxHealth
L 08/14/2019 - 23:42:32: [SM]   [2] Line 187, /home/groups/sourcemod/upload_tmp/phpmSsr8n.sp::MushroomTouch
__________________


DarkPyro is offline
kking117
Junior Member
Join Date: Jan 2017
Location: Australia
Old 08-15-2019 , 01:41   Re: [TF2] Multiplying Percents (Decimals) & EmitSound
Reply With Quote #6

Element in this case seems to mean the entity number. Which means it's trying to get the max health of something that isn't a client (probably). Just to make sure, does your code look something like this?

Code:
new MaxHealth = GetClientMaxHealth(iEntity);
SetEntProp(iEntity, Prop_Send, "m_iHealth", RoundToNearest(MaxHealth * 0.2));
kking117 is offline
DarkPyro
Member
Join Date: Aug 2013
Old 08-15-2019 , 10:07   Re: [TF2] Multiplying Percents (Decimals) & EmitSound
Reply With Quote #7

Quote:
Originally Posted by kking117 View Post
Element in this case seems to mean the entity number. Which means it's trying to get the max health of something that isn't a client (probably). Just to make sure, does your code look something like this?

Code:
new MaxHealth = GetClientMaxHealth(iEntity);
SetEntProp(iEntity, Prop_Send, "m_iHealth", RoundToNearest(MaxHealth * 0.2));
Yeah it does, it did have a 4 after the RoundToNearest function but I removed it and it didn't make a difference. This is the check that it does in that Touch function.

Code:
	if ( iEntity > 0 && iEntity <= MaxClients && IsPlayerAlive( iEntity ) && CurHealth < MaxHealth )
And this is the code that calls the Touch function

Code:
SDKHook( Mushroom, SDKHook_StartTouch, MushroomTouch );
The thing is, I create those health checks BEFORE I call the Touch check that I posted above, and that could be the reason why it's also targeting other things it's not supposed to (I also created the same function to check for current health, albeit redundant, I wanted to be consistent. But, it was yielding the same errors before and after I did that):

Code:
public MushroomTouch( iMushroom, iEntity ) {
	new CurHealth = GetClientCurrentHealth( iEntity );
	new MaxHealth = GetClientMaxHealth( iEntity );

	// Continue only if the touching entity is a player
	if ( iEntity > 0 && iEntity <= MaxClients && IsPlayerAlive( iEntity ) && CurHealth < MaxHealth ) {
I'm not sure if that would be the issue. I'll check it out in a little bit if placing CurHealth and MaxHealth after the touching check would solve it.

EDIT: After I moved CurHealth and MaxHealth after the check, all the errors have stopped (as far as I tested anyway). Thanks for all the help!
__________________



Last edited by DarkPyro; 08-15-2019 at 10:49.
DarkPyro 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 07:12.


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