Arrays and enums...
#1

Hi everyone.
Alright, my problem is:
I have the following code:
pawn Код:
enum Stuntz
{
    Float:XS,
    Float:YS,
    Float:ZS,
    Rewardcash,
    RewardSP,
    Rewardscore
}
new StuntPositions[3][Stuntz] =
{
    {1712.2190,1448.4867,10.7327,1500,1,2},
    {-2245.0986,321.5719,34.7447,1650,1,2},
    {2507.3184,1793.3164,10.2443,1800,2,2}
};
Everything is working fine, until I call either Rewardcash, RewardSP, or Rewardscore, which happens here:
pawn Код:
public OnPlayerEnterCheckpoint(playerid)
{
    new lawl = sizeof(StuntPositions);
    PlaySound(playerid,1057);
    PInfo[playerid][Stuntsdone]++;
    static tempstr[128];
    GivePlayerSP(playerid,StuntPositions[lawl][RewardSP]);
    GivePlayerMoney(playerid,StuntPositions[lawl][Rewardcash]);
    SetPlayerScore(playerid,GetPlayerScore(playerid)+StuntPositions[lawl][Rewardscore]);
    UpdateRank(playerid);
    PInfo[playerid][Stunting] = 0;
    format(tempstr,128,"~g~~h~Stunt completed! ~y~~h~Earned: ~n~~r~~h~+%i Stunt Points ~n~+%i Score ~n~+$%i cash",StuntPositions[lawl][RewardSP],StuntPositions[lawl][Rewardscore],StuntPositions[lawl][Rewardcash]);
    GameTextForPlayer(playerid,tempstr,6000,3);
    CurrStunt[playerid][stuntid[playerid]] = 0;
    DisablePlayerCheckpoint(playerid);
    SetTimerEx("CanDoStunt",600000,false,"i",playerid);
    return 1;
}
I debugged my script and I noticed that the script stops either at the following lines:
pawn Код:
GivePlayerSP(playerid,StuntPositions[lawl][RewardSP]);
    GivePlayerMoney(playerid,StuntPositions[lawl][Rewardcash]);
    SetPlayerScore(playerid,GetPlayerScore(playerid)+StuntPositions[lawl][Rewardscore]);
I've been trying to fix this for a while now.
However, there is another weird problem.
If I do
pawn Код:
new lawl = sizeof(StuntPositions);
StuntPositions[lawl][Rewardcash]
Compiles fine.

But if I just do
pawn Код:
StuntPositions[sizeof(StuntPositions)][Rewardcash]
I get the array index out of bounds error.
Any help would be highly appreciated.
Reply
#2

StuntPositions[(sizeof(StuntPositions))][Rewardcash];

I have no idea if that would help, I am not familiar if PAWN allows a function call as the array index argument.
Reply
#3

Quote:
Originally Posted by GrimR
Посмотреть сообщение
StuntPositions[(sizeof(StuntPositions))][Rewardcash];

I have no idea if that would help, I am not familiar if PAWN allows a function call as the array index argument.
I already tried that, and resulted in an error; read the first post again.
Reply
#4

pawn Код:
new Float: StuntPositions[3][Stuntz] =
{
    {1712.2190,1448.4867,10.7327,1500,1,2},
    {-2245.0986,321.5719,34.7447,1650,1,2},
    {2507.3184,1793.3164,10.2443,1800,2,2}
};
I'm pretty sure they're suppose to be floats.

and "GivePlayerMoney" afaik doesn't support floats.
Reply
#5

I'm pretty sure that sizeof will return 3 and you can only access 0, 1, 2 as there is only 3 values and you start at 0. The data is out of the range causing a server crash. I'm not sure what exactly you are trying to do here, but the best way to combat the issue is

pawn Код:
new lawl = sizeof(StuntPositions) - 1;
Reply
#6

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
I'm pretty sure that sizeof will return 3 and you can only access 0, 1, 2 as there is only 3 values and you start at 0. The data is out of the range causing a server crash. I'm not sure what exactly you are trying to do here, but the best way to combat the issue is

pawn Код:
new lawl = sizeof(StuntPositions) - 1;
Thanks, this fixed the issue... however it is still returning the same line (the last one) because I get the same amount of rewards in every checkpoint I enter.
Reply
#7

What are you aiming to do? From looking at your code I take it that you want to give rewards when they enter the checkpoint. Show the checkpoint creation code please.
Reply
#8

I'm not sure if this has been already said although, "sizeof" returns the specific value of the array/cell size.

pawn Код:
new string[128];
printf("%d is the string size", sizeof(string));
//will return 128; hopefully.
That's the wrong way you're doing it there, you might as well use a checkpoint streamer and detect which checkpoint a player is in because the SA-MP default checkpoint limit is 1 in viewing, afaik.
Reply
#9

Problem solved.
Thanks everyone.
Reply
#10

Glad you could fix it mate!

Just on a side note (hold on, that damn avatar of that trololol guy is putting me off lol!........), my code posted had additional brackets around the sizeof function to force evaluation of it's resultant first, I didn't see anything
in your first post that has those extra brackets. But that obviously wasn't the problem anyway :P.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)