PDA

View Full Version : Invalid Memory Access from OnPlayerKeyStateChange


oSAINTo
19/02/2018, 09:14 PM
Whenever I press F in-game, no matter where I stand or whatever I'm doing. I get this in my console:
[debug] Run time error 5: "Invalid memory access"
[debug] AMX backtrace:
[debug] #0 000042d4 in public OnPlayerKeyStateChange (0, 16, 0) from gymscript.amx

I'm pretty sure it's safe to say it's coming from OnPlayerKeyStateChange, which would be:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if((newkeys & KEY_SECONDARY_ATTACK) && !(oldkeys & KEY_SECONDARY_ATTACK))
{
if(usingBench[playerid] == true && canUseWeight[playerid] == true)
{
KillTimer(benchTimer[playerid]);
finishBench(playerid);
}
else
{
for(new i; i <= sizeof bench_pos; i++)
{
if(IsPlayerInRangeOfPoint(playerid, 1.2, bench_pos[i][0], bench_pos[i][1], bench_pos[i][2]))
{
if(closeBench[i] == false && usingBench[playerid] == false)
{
closeBench[i] = true;
usingBench[playerid] = true;
benchRep[playerid] = 0;

SetProgressBarValue(gymProgress[playerid], 0);

TogglePlayerControllable(playerid, 0);
if(bench_pos[i][3] == -0.000007)
{
SetPlayerPos(playerid, bench_pos[i][0]-1.0, bench_pos[i][1], bench_pos[i][2]+0.65);
SetPlayerFacingAngle(playerid, bench_pos[i][3]-90);
}
else if(bench_pos[i][3] == -0.000015)
{
SetPlayerPos(playerid, bench_pos[i][0]-1.0, bench_pos[i][1], bench_pos[i][2]+0.65);
SetPlayerFacingAngle(playerid, bench_pos[i][3]+90);
}
ApplyAnimation(playerid, "benchpress", "gym_bp_geton", 1, 0, 0, 0, 1, 0, 1);

SetTimerEx("benchBegin", 3800, 0, "ii", playerid, i);
SetPlayerCameraPos(playerid, bench_pos[i][0]-1.5, bench_pos[i][1]+1.0, bench_pos[i][2] + 3 );
SetPlayerCameraLookAt(playerid, bench_pos[i][0], bench_pos[i][1], bench_pos[i][2]);
}
else
{
GameTextForPlayer(playerid, "This weightbench is currently in use.", 2000, 1);
}
}
}
}
}

else if((newkeys & KEY_SPRINT) && !(oldkeys & KEY_SPRINT))
{
if(usingBench[playerid] == true && canUseWeight[playerid] == true)
{
SetProgressBarValue(gymProgress[playerid], GetProgressBarValue(gymProgress[playerid]) + 5);
UpdateProgressBar(gymProgress[playerid], playerid);
}
}
return 1;
}

I've never encountered this error before. I'm curious what it is and how shall I stop it?

Kane
19/02/2018, 09:18 PM
for(new i; i <= sizeof bench_pos; i++)


Change <= to < operator. Might not be it but compile with -d3 flags so we know where it actually occurs.

oSAINTo
19/02/2018, 09:28 PM
for(new i; i <= sizeof bench_pos; i++)


Change <= to < operator. Might not be it but compile with -d3 flags so we know where it actually occurs.

That fixed it, wow. Just needed a second pair of eyes. Must've missed it somehow.

Pottus
19/02/2018, 09:42 PM
You can actually make things easier on yourself by using DynamicAreas instead of IsPlayerInRangeOfPoint() . This is better because you only need to do any looping when a player is actually in a dynamic area and eliminate IsPlayerInRangeOfPoint() and makes it easier overall to code your systems.