PDA

View Full Version : Need some help


Cowboy
20/12/2011, 04:40 PM
Hello,

I am trying to make when a player creates a gang, and the player's gangname is already choosen by someone else, it should send a message, but it ain't working.
Here's the code

for(new i; i < TotalGangs; i++)
{
format(CheckName, sizeof(CheckName), Gang_File, i);
if(DOF_FileExists(CheckName))
{
new gname[128];
strcat(gname, DOF_GetString(CheckName, "Name"), 128);
if(strcmp(GangName, gname, true) == 0)
{
SendClientMessage(playerid, Red, "A gang with this name already exists");
return 1;
}
}
}

It just keeps on making the gang, please help, thanks.
Rep will be given.

Cowboy
21/12/2011, 11:10 AM
Bump.

Ash.
21/12/2011, 11:35 AM
Ignore me...

Give me a second.

Cowboy
21/12/2011, 06:46 PM
Ignore me...

Give me a second.

Hey, could you figure it out?

Ash.
21/12/2011, 08:09 PM
Are you sure the original file exists. (The 'CheckName' file?)

Cowboy
22/12/2011, 01:43 PM
Yes, it does exists

// This is my define for Gang_File

#define Gang_File "Gangs/%d.ini"

new TotalGangs;

public OnGameModeInit()
{
if(!DOF_FileExists(CFG_File))
{
DOF_CreateFile(CFG_File);
DOF_SetInt(CFG_File, "Gangs", 0);
print("CFG File created");
}
else
{
TotalGangs = DOF_GetInt(CFG_File, "Gangs");
for(new i = 0; i <= TotalGangs; i++)
{
GangInfo[i][Gangs] = i;
}
printf("CFG File loaded. %d gangs loaded", TotalGangs);
}
DOF_SaveFile();
return 1;
}


There's the TotalGangs variable which also works, so yeah, maybe something wrong with the code I provided in the first post?

Cowboy
22/12/2011, 08:30 PM
I had another attempt but failed, I don't know what I have done wrong. Maybe with the strcmp or strcat line?

skaTim
22/12/2011, 09:00 PM
for(new i; i < TotalGangs; i++)
{
format(CheckName, sizeof(CheckName), Gang_File, i);
if(DOF_FileExists(CheckName))
{
new gname[128];
strcat(gname, DOF_GetString(CheckName, "Name"), 128);
if(!strcmp(GangName, gname, true) == 0)
{
SendClientMessage(playerid, Red, "A gang with this name already exists");
return 1;
}
}
}

Cowboy
22/12/2011, 09:02 PM
It gave me a tag mismatch warning, how can I fix it?

Ash.
22/12/2011, 09:09 PM
It gave me a tag mismatch warning, how can I fix it?

The user that posted above you, has placed an exclamation mark infront of the strcmp function. This has actually broken your code.

Doing !strcmp(...) is the same as doing strcmp(...) == 0, hence there is no difference.

The reason you are getting the warning is because you're doing two checks (checking with an exclamation mark and the == 0 method.

I'm unsure on your issue, though.

Cowboy
23/12/2011, 10:28 AM
I think there's a mistake with strcat line, when I do this under OnGameMode init, it doesn't print out the name

new ChekName[128];
for(new i = 0; i <= TotalGangs; i++)
{
format(CheckName, sizeof(CheckName), Gang_File, i);
strcat(GangInfo[i][gName], DOF_GetString(Gang_File, "Name"), strlen(DOF_GetString(Gang_File, "Name")) +1);
printf("%d Gang Name: %s\n", i, GangInfo[gName]); // I tried to do GangInfo[i][gName] and GangInfo[TotalGangs][gName], none of the works, how do I do tthis correctly?
}

Cowboy
23/12/2011, 02:37 PM
Help me please

Ash.
23/12/2011, 02:38 PM
You're storing data in the 2nd dimension, yet only printing what is in the first (which should flag a compiler error).

Cowboy
23/12/2011, 02:41 PM
Doesn't it store all the data in GangInfo[i][gName] enum?

Ash.
23/12/2011, 02:44 PM
Doesn't it store all the data in GangInfo[i][gName] enum?

Yes. (well theoretically no, but don't worry about how it's stored, just worry about how you get/set it).

Try printing 'GangInfo[i][gName]' instead, lets see what that throws up.

Cowboy
23/12/2011, 02:47 PM
[16:54:23] 0 Gang Name:

[16:54:23] 1 Gang Name:

[16:54:23] 2 Gang Name:

[16:54:23] 3 Gang Name:

[16:54:23] 4 Gang Name:

[16:54:23] 5 Gang Name:

[16:54:23] 6 Gang Name:

[16:54:23] 7 Gang Name:

[16:54:23] 8 Gang Name:

[16:54:23] 9 Gang Name:

[16:54:23] 10 Gang Name:

[16:54:23] 11 Gang Name:

[16:54:23] 12 Gang Name:

[16:54:23] 13 Gang Name:

[16:54:23] 14 Gang Name:

[16:54:23] 15 Gang Name:

Thanks for helping, really appreciated :D

Ash.
23/12/2011, 02:50 PM
Try printing 'DOF_GetString(Gang_File, "Name")' instead.

+ No problem haha :)

*cough* "This forum requires that you wait 120 seconds between posts. Please try again in 57 seconds."
I help loads of people, but the forum doesn't like that and loves to stop me from posting every 120 seconds...

Cowboy
23/12/2011, 02:58 PM
Same output, I am going to try the DOF native PrintFile and see what it prints

Edit: Nothing interested with that DOF native, I tried to add like a FileExists check, but it didn't help.

Here's the whole ongamemodeinit, maybe you can see something I don't

public OnGameModeInit()
{
new CheckName[128];
if(!DOF_FileExists(CFG_File))
{
DOF_CreateFile(CFG_File);
DOF_SetInt(CFG_File, "Gangs", 0);
print("CFG File created");
}
else
{
TotalGangs = DOF_GetInt(CFG_File, "Gangs");
for(new i = 0; i <= TotalGangs; i++)
{
GangInfo[i][Gangs] = i;
}
printf("CFG File loaded. %d gangs loaded", TotalGangs);
}
for(new i = 0; i <= TotalGangs; i++)
{
format(CheckName, sizeof(CheckName), Gang_File, i);
if(DOF_FileExists(CheckName))
{
strcat(GangInfo[i][gName], DOF_GetString(Gang_File, "Name"), strlen(DOF_GetString(Gang_File, "Name")) +1);
printf("%d Gang Name: %s\n", i, GangInfo[i][gName]);
}
}
return 1;
}

Ash.
23/12/2011, 03:08 PM
Just out of interest, how many gangs are loaded?

What does "printf("CFG File loaded. %d gangs loaded", TotalGangs);" print?

Cowboy
23/12/2011, 03:09 PM
There are 15 total gangs that I just made in-game quickly, it seems to be loading how many there are, but not the name.

Ash.
23/12/2011, 03:13 PM
There are 15 total gangs that I just made in-game quickly, it seems to be loading how many there are, but not the name.

Is the name actually being stored? Post the gang data file.

Cowboy
23/12/2011, 03:16 PM
CFG file:

Gangs=15

Any gang file:

ID=1
Name=Test
Leader=Cowboy

Ash.
23/12/2011, 03:18 PM
I'm quite confused now. Everything is there and in my eyes it should work. Are you using Double-O-Files? It might be better to ask in that topic, as this could be a bug with that include, not your code. (Or at least, I can't find an issue with your code).

Cowboy
23/12/2011, 03:19 PM
I use DOF 1, not the DOF 2, I heard it is bugged, and DOF 1 should be fully working and stable. :(

Ash.
23/12/2011, 03:21 PM
I use DOF 1, not the DOF 2, I heard it is bugged, and DOF 1 should be fully working and stable. :(

Try (temporarily?) updating to DOF 2, as I'm quite sure that there were some issues with DOF 1 and string handling. I could be wrong though.

It may also be more beneficial for you to switch to something like y_files, to give you more customisation over the files and such - this would also fix the string retrieval issue as I know y_files is awesome at file handling.

Cowboy
23/12/2011, 03:37 PM
DOF 2 same results. I also tried with dini same results, I really do think something is wrong with my code, but I don't know what

Ash.
23/12/2011, 03:59 PM
DOF 2 same results. I also tried with dini same results, I really do think something is wrong with my code, but I don't know what

I can't see anything!

Just try, using format() rather than strcat(). Many people will say this is bad preference, but it's worth a try (especially if you think it could be something to do with the strcat() section of your code).

Cowboy
23/12/2011, 04:13 PM
Something like this

format(GangInfo[i][gName], sizeof(string), "%s", DOF_GetString(Gang_File, "Name"));
// strcat(GangInfo[i][gName], DOF_GetString(Gang_File, "Name"), strlen(DOF_GetString(Gang_File, "Name")) +1);
printf("%d Gang Name: %s\n", i, string);

If yes, then it doesn't work, maybe you can show me?

Ash.
23/12/2011, 04:24 PM
Something like this

format(GangInfo[i][gName], sizeof(string), "%s", DOF_GetString(Gang_File, "Name"));
// strcat(GangInfo[i][gName], DOF_GetString(Gang_File, "Name"), strlen(DOF_GetString(Gang_File, "Name")) +1);
printf("%d Gang Name: %s\n", i, string);

If yes, then it doesn't work, maybe you can show me?

What is 'string'? You're printing it but you never assign anything to it!

Cowboy
23/12/2011, 04:29 PM
Well, I don't how to use the format this way, show me please?

Cowboy
23/12/2011, 08:17 PM
Hey,

I got it to print with strcat.

Now back to my first post. I have tried the following but all failed

for(new i; i < TotalGangs; i++)
{
new CheckName[128];
format(CheckName, sizeof(CheckName), Gang_File, i);
// strcat(GangName, DOF_GetString(CheckName, "Name"), 128);
if(strcmp(GangName, GangInfo[i][gName], true) == 0)
{
SendClientMessage(playerid, Red, "A gang with this name already exists");
return 1;
}
}

// this GangInfo[i][gName] with DOF_GetString(CheckName, "Name")


But it does not work, please help me

Ash.
23/12/2011, 08:49 PM
Hey,

I got it to print with strcat.

Now back to my first post. I have tried the following but all failed

for(new i; i < TotalGangs; i++)
{
new CheckName[128];
format(CheckName, sizeof(CheckName), Gang_File, i);
// strcat(GangName, DOF_GetString(CheckName, "Name"), 128);
if(strcmp(GangName, GangInfo[i][gName], true) == 0)
{
SendClientMessage(playerid, Red, "A gang with this name already exists");
return 1;
}
}

// this GangInfo[i][gName] with DOF_GetString(CheckName, "Name")


But it does not work, please help me

Print out both "GangName" and "GangInfo[i][gName]" just before you begin the if statement.

Cowboy
23/12/2011, 09:10 PM
Hey, thanks again

GangName prints what I have written as 'gangname', though GAngInfo[i][gName] prints out nothing, just empty, so something wrong with GangInfo[i][gName], what should I replace it with?

Cowboy
23/12/2011, 09:33 PM
Hey, I tried this, just because it gives more sense:

for(new i; i < TotalGangs; i++)
{
new CheckName[128], gname[128];
format(CheckName, sizeof(CheckName), Gang_File, i);
strcat(gname, DOF_GetString(CheckName, "Name"), strlen(DOF_GetString(CheckName, "Name")) +1);
printf("Gang Name: %s - Gang Name 2: %s", GangName, gname);
if(strcmp(GangName, gname, true) == 0)
{
SendClientMessage(playerid, Red, "A gang with this name already exists");
return 1;
}
}

First one prints correctly, but second do not print anything.. I think it stores all the data into the gname variable, but how would I be able to open all the files, find the one that equals 'GangName'?