PDA

View Full Version : Going from racename to raceid


knackworst
21/09/2013, 06:52 PM
Hello

I made a race system a while ago and it has this command to start or join races: /joinrace <id>
However I wanna change it to: /joinrace <racename>

So here's what I tried doing:

CMD:joinrace(playerid, params[])
{
JoinRace(playerid, raceid);*/

new name[56], raceid;
if(PInfo[playerid][VortexJump] == 1 ) return SendClientMessage(playerid,COLOR_RED,"You cannot use this command here, finish your jump please");
if(sscanf(params,"s[56]",name)) return SendClientMessage(playerid, COLOR_RED, "Invalid race name");

for(new x = 0;x<currentraceslot; x++)
{
if(!strcmp(RaceInfo[x][racename], name, false))
{
raceid = x;
}
//break;
}

if(raceid == 0) return SendClientMessage(playerid, COLOR_RED, "Invalid race name");
if(RaceInfo[raceid][racejoinable] == false && RaceInfo[raceid][racerunning] == false && PInfo[playerid][Racing] == -1) OpenRace(raceid);
JoinRace(playerid, raceid);

SendClientMessage(playerid, COLOR_GREEN, "racename called");



return 1;
}

So I loop through all the races and then compare their name with the params, however for some reason for every name I type after /joinrace it always calls the race with raceid 0....

Do you know what could be wrong?
Thanks in advance :)

EiresJason
21/09/2013, 07:01 PM
Are you typing in the exact name of the race's name? With the exact same cases? Your strcmp is checking to see if the races name is the exact same string as the name of the race you type in after /joinrace.
EG: Races name is 'firsttoLosSantos'; and you type in 'firsttoLossantos'; it will return false.

Maybe try this. Also; add in some debuggers so you know whats going wrong.


CMD:joinrace(playerid, params[])
{
JoinRace(playerid, raceid);*/

new name[56], raceid;
if(PInfo[playerid][VortexJump] == 1 ) return SendClientMessage(playerid,COLOR_RED,"You cannot use this command here, finish your jump please");
if(sscanf(params,"s[56]",name)) return SendClientMessage(playerid, COLOR_RED, "Invalid race name");

for(new x = 0;x<currentraceslot; x++)
{
if(!strcmp(RaceInfo[x][racename], name, true)) //changed to true so it doesn't compare capital letters; etc.
{
raceid = x;
}
//break;
}

if(raceid == 0) return SendClientMessage(playerid, COLOR_RED, "Invalid race name");
if(RaceInfo[raceid][racejoinable] == false && RaceInfo[raceid][racerunning] == false && PInfo[playerid][Racing] == -1) OpenRace(raceid);
JoinRace(playerid, raceid);

SendClientMessage(playerid, COLOR_GREEN, "racename called");



return 1;
}

Konstantinos
21/09/2013, 07:03 PM
Give a try!

CMD:joinrace(playerid, params[])
{
if(PInfo[playerid][VortexJump] == 1 ) return SendClientMessage(playerid,COLOR_RED,"You cannot use this command here, finish your jump please");
if(isnull(params)) return SendClientMessage(playerid, COLOR_RED, "/joinrace <race name>");
new raceid = GetRaceidFromName( params );
if(raceid != -1)
{
if(raceid == 0) return SendClientMessage(playerid, COLOR_RED, "Invalid race name");
if(RaceInfo[raceid][racejoinable] == false && RaceInfo[raceid][racerunning] == false && PInfo[playerid][Racing] == -1) OpenRace(raceid);
JoinRace(playerid, raceid);

SendClientMessage(playerid, COLOR_GREEN, "racename called");
}
else SendClientMessage(playerid, COLOR_RED, "Invalid race name");
return 1;
}

stock GetRaceidFromName( race_name[ ] )
{
if( !strlen( race_name ) ) return -1;
for( new r = 0; r < currentraceslot; r++ ) if( !strcmp( RaceInfo[ r ][ racename ], race_name, false ) ) return r;
return -1;
}

knackworst
21/09/2013, 07:03 PM
@first response

Omg it worked...

I feel very stupid for asking now...
Anyways thanks and rep+

Konstant: yours worked too, thanks :)

EiresJason
21/09/2013, 07:08 PM
Np :)