PDA

View Full Version : Is there any safer way to do this?


Eoussama
14/07/2017, 06:17 PM
Hello, hope you're having a great day,
so is there a better way to do this,

foreach(new i : EVM){
format(gh, sizeof(gh), "iterator: %d", i);
SendClientMessage(playerid, EVMCOLOR_RED, gh);
Iter_Remove(EVM, i);
}

ofc, it doesn't not work,
Debugging

Code 1:
foreach(new I : EVM)
print(i);

Result:
Iterator: 0
Iterator: 1
Iterator: 2
Iterator: 3
Iterator: 4
Iterator: 5


Code 2:
foreach(new I : EVM){
print(i);
Iter_Remove(i);
}

Result:

sometimes it shows random numbers
Iterator: 26(The first iterator appears here)
Iterator: 0(always shows 0)
Iterator -33161(always shows this number)


some other times the server just crashes

Logic_
14/07/2017, 06:30 PM
Might showing us what EVM actually is?

Konstantinos
14/07/2017, 06:33 PM
Iter_SafeRemove inside foreach loops.

Eoussama
14/07/2017, 06:37 PM
Might showing us what EVM actually is?

Iterator:EVM<MAX_EVMs> with MAX_EVMs being 150

Paulice
14/07/2017, 06:45 PM
Iterator:EVM<MAX_EVMs> with MAX_EVMs being 150

Have you tried this (http://forum.sa-mp.com/showpost.php?p=3901924&postcount=3)?

Eoussama
14/07/2017, 06:48 PM
Have you tried this (http://forum.sa-mp.com/showpost.php?p=3901924&postcount=3)?

yes, and this is the result


foreach(new i : EVM){
printf("Iterator: %d", i);
new cur = evmID;
Iter_SafeRemove(EVM, cur, evmID);
}

Iterator: 7
Iterator: 0
Iterator: -39800


this problem only occurs if I do this

foreach(new i : EVM)
DeleteEVM(i);

DeleteEVM(evmID){

EVMInfo[evmID][e_evmName][0] = EOS;
EVMInfo[evmID][e_evmPosX] = 0;
EVMInfo[evmID][e_evmPosY] = 0;
EVMInfo[evmID][e_evmPosZ] = 0;
EVMInfo[evmID][e_evmRotX] = 0;
EVMInfo[evmID][e_evmRotY] = 0;
EVMInfo[evmID][e_evmRotZ] = 0;
EVMInfo[evmID][e_evmIsBlocked] = true;
DestroyDynamicObject(EVMInfo[evmID][e_evmObj]);
DestroyDynamic3DTextLabel(EVMInfo[evmID][e_evmLabel]);
new cur = evmID;
Iter_SafeRemove(EVM, cur, evmID);

}


but it works fine if I do this


foreach(new i : EVM){
DeleteEVM(i);
new cur = i;
Iter_SafeRemove(EVM, cur, i);
}

DeleteEVM(evmID){

EVMInfo[evmID][e_evmName][0] = EOS;
EVMInfo[evmID][e_evmPosX] = 0;
EVMInfo[evmID][e_evmPosY] = 0;
EVMInfo[evmID][e_evmPosZ] = 0;
EVMInfo[evmID][e_evmRotX] = 0;
EVMInfo[evmID][e_evmRotY] = 0;
EVMInfo[evmID][e_evmRotZ] = 0;
EVMInfo[evmID][e_evmIsBlocked] = true;
DestroyDynamicObject(EVMInfo[evmID][e_evmObj]);
DestroyDynamic3DTextLabel(EVMInfo[evmID][e_evmLabel]);
}

so there is no way I can use Iter_SafeRemove inside a separae function?

Paulice
14/07/2017, 07:09 PM
Since you're "deleting" all EVMs, simply use Iter_Clear(EVM); after the foreach loop (removing the need of Iter_Remove/Iter_SafeRemove - Iter_Remove cannot be used within a loop that uses the self iterator btw).

Eoussama
14/07/2017, 07:15 PM
Since you're "deleting" all EVMs, simply use Iter_Clear(EVM); after the foreach loop (removing the need of Iter_Remove/Iter_SafeRemove - Iter_Remove cannot be used within a loop that uses the self iterator btw).

thanks