View Issue Details

IDProjectCategoryView StatusLast Update
0008400Multi Theft Auto : San AndreasScriptingpublic2014-08-27 14:46
ReporterDrakathAssigned Tosbx320 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version 
Target VersionFixed in Version1.4.1 
Summary0008400: Bug with setElementHealth in onClientPlayerDamage when hurt by a ped
Description

When setting element health lower than it is using onClientPlayerDamage when a pedestrian attacks you and you have over 100 health points, it always sets it to 100 and then starts to work properly.
For example if you have 200 health points and a pedestrian attacks you, the script will set your health to 100 even though it should just lower your health by 5 points:
setElementHealth ( getLocalPlayer(), playerHealth - 5 )

Steps To Reproduce

function damage ( attacker, weapon, bodypart )
local playerHealth = getElementHealth ( getLocalPlayer() )
setElementHealth ( getLocalPlayer(), playerHealth - 5 )
end
addEventHandler ( "onClientPlayerDamage", getLocalPlayer(), damage )

function t()
local x,y,z = getElementPosition(localPlayer)
local ped = createPed(10, x+3, y+3, z+1)
setTimer ( function (ped) if ( isElement ( ped ) ) then setPedControlState ( ped, "fire", true) end end, 5000, 3, ped )
end

t()

Additional Information

It is best seen if your health is 200. (with max_health stat 1000)
Let the pedestrian hit you and your health will be set to 100 instead of 195.

Only tested with 1.3.5-release-6162.3.000

TagsNo tags attached.

Users sponsoring this issue
Sponsors List Total Sponsorship = EUR 1

2014-08-20 20:41: Drakath (EUR 1)
Users sponsoring this issue (Total Sponsorship = EUR 1)

Activities

Drakath

2014-07-31 15:35

viewer   ~~0021564

Correction: using setElementHealth onClientPlayerDamage when a pedestrian attacks
you can not set more than 100 health points.

arranTuna

2014-07-31 17:18

manager   ~~0021565

Confirmed with v1.4-release-6751

Very strange bug, before setting the health it returns the correct value, after setting the health it returns 100.

sbx320

2014-07-31 23:25

administrator   ~~0021590

Apparently something resets the health stat to 569 during onClientPlayerWeaponDamage. This leads to CClientPed::GetMaxHealth returning 100 as maximum health for setElementHealth.

Has nothing to do with peds hitting the player.

sbx320

2014-08-27 14:46

administrator   ~~0021880

Alright, after another few hours of debugging I finally found the cause of this issue.

During world processing MTA switches peds and remote players into local player mode (CPlayerPed::ProcessControl instead of CPed::ProcessControl) to allow additional features. When this happens the stats array is temporarily being replaced by the ped's stats. CClientPlayer::GetStat however isn't aware of it and reads the (replaced) stats array if you try to access the localPlayer's stats during a remote ped's CPlayerPed::ProcessControl.

When a ped is hit or shot by another ped like in this case onClientPlayerDamage will be triggered from the attacking ped's ProcessControl function. This leads to CClientPlayer::GetMaxHealth returning the currently processed ped's max health, rather than the localPlayer's max health when called under the circumstances described in the bug report.

Fixed in https://code.google.com/p/mtasa-blue/source/detail?r=6829

Issue History

Date Modified Username Field Change