29.10.2014, 23:43
(
Last edited by BornHuman; 30/05/2016 at 05:20 PM.
)
You're going to need ZCMD, SSCANF, and DINI for this tutorial.
NOTE: Some commands may have messed up indenting. This is due to vbulletin. Please just use your indenting skills to put it in the right place.
Section 1
If you're here, then you are wondering how you may create factions dynamically (which means, in game). Which is completely fine, but there is a few things you should know. This is not a simple thing to do. While yes, it is mostly copy & paste, you will have a hard time understanding it if you don't have a basis for how PAWN general works. Proceeding means you have a general idea on how DINI and saving generally works. (For those of you who say, "Dini is outdated", please refer to this link.) Now for those of you who are ready, lets proceed
Section 2
Lets set up the basis. First, we're going to need to set our script to save them. So lets set up the initiation system. (Note: Please do not compile your script until we are done.)
First, lets add this to your defines.
This defines how many groups can be loaded into the script.
We're going to create a stock to initiate our groups. You should place the following anywhere under OnGameModeInit.
This function will be called with the gamemode is called to initiate, so basically when the server is loaded/booted.
Let's jump to the bottom, or whereever you keep your stocks, and place the following:
Section 3
Now of course, we can't initiate groups that don't exist, so lets start to add some functions that will allow for creating them in game.
Lets add this command.
Section 4
Okay, you now have groups. Lets start adding some saving variables to these groups. We're going to introduce something called ENUMERATORS, or ENUMs for short. Lets add an enum called GroupData
Now lets add a group name, and a group type to it. They both use different variables. One is an integer, the other one is a string.
Now lets jump back to our stock. Remember? LoadGroups? And add these two functions too it.
Now the groups are reading from the file, and setting its variables to what the file has in it. You're basically done! However, lets add some commands in game to where you can change & save these functions.
Section 5
We'll add a group name command.
Good, now we can change the group name. Lets jump to the final thing, and we'll make a stock for saving the groups.
This stock is going to save from a loop. I'll get to that in a minute when we use the stock.
To use this, lets put this stock function under the create group command.
Finally, lets add a save groups function. Which will save all groups.
CONGRATULATIONS!
If this helped, or you're just a generally nice person, please +REP
Got a question? Post below!
Thanks!
NOTE: Some commands may have messed up indenting. This is due to vbulletin. Please just use your indenting skills to put it in the right place.
Section 1
If you're here, then you are wondering how you may create factions dynamically (which means, in game). Which is completely fine, but there is a few things you should know. This is not a simple thing to do. While yes, it is mostly copy & paste, you will have a hard time understanding it if you don't have a basis for how PAWN general works. Proceeding means you have a general idea on how DINI and saving generally works. (For those of you who say, "Dini is outdated", please refer to this link.) Now for those of you who are ready, lets proceed
Section 2
Lets set up the basis. First, we're going to need to set our script to save them. So lets set up the initiation system. (Note: Please do not compile your script until we are done.)
First, lets add this to your defines.
pawn Code:
#define MAX_GROUPS 10
We're going to create a stock to initiate our groups. You should place the following anywhere under OnGameModeInit.
pawn Code:
LoadGroups();
Let's jump to the bottom, or whereever you keep your stocks, and place the following:
pawn Code:
stock LoadGroups()
{
new FileName[128]; // This creates a new variable string for the filename to be carried in
for(new i = 0; i < MAX_GROUPS; i++) // This will run through the MAX_GROUPS earlier defined at the top of the script.
{
format(FileName, sizeof(FileName), "Groups/Group_%d.ini", i); // This formats the string in which the groups will be searched for in.
if(fexist(FileName)) // If the file exists...
{
printf("[system] Group %d spawned.", i); // We'll just place this here just to have something here.
}
}
return 1;
}
Now of course, we can't initiate groups that don't exist, so lets start to add some functions that will allow for creating them in game.
Lets add this command.
pawn Code:
CMD:gcreate(playerid, params[])
{
new value, string[255];
if(sscanf(params, "ds[128]DS[255]", value, Name, Amount, string))
{
SendClientMessage(playerid, 0xFFFFFF, "SYNTAX: /gcreate [value]"); // If the player doesn't enter a value, this message will show up.
return 1;
}
if(value < 1 || value > 10) // checks what the player put in the value column, and if it is 1-10
{
SendClientMessage(playerid, WHITE, "SYNTAX: /gcreate [value] [names]"); // if its not, then this message is displayed.
SendClientMessage(playerid, GREY, "Values: Group IDs start at 1 and end at 10.");
return 1;
}
format(string, sizeof(string), "Groups/%d.ini", value); // Formatting the string, to create the file.
if(!fexist(string))
{
dini_Create(string); // Please tell me you know what this does.
SendClientMessage(playerid, 0xFFFFFF, "[system] File created successfully."); // k.
return 1;
}
return 1;
}
Okay, you now have groups. Lets start adding some saving variables to these groups. We're going to introduce something called ENUMERATORS, or ENUMs for short. Lets add an enum called GroupData
pawn Code:
enum GroupData
{
};
pawn Code:
enum GroupData
{
GroupName[255], // the [255] represents how long the string can be.
GroupType // you never put anything after the last item in the enum.
};
pawn Code:
stock LoadGroups()
{
new FileName[128];
for(new i = 0; i < MAX_GROUPS; i++)
{
format(FileName, sizeof(FileName), "Groups/Group_%d.ini", i);
if(fexist(FileName)) // If the file exists...
{
Groups[i][GroupName] = dini_Get(FileName, "GroupName"); // We use dini_Get because it is a string.
Groups[i][GroupType] = dini_Int(FileName, "GroupType"); // We use dini_Int because it is an (int)eger.
printf("[system] Group %d spawned.", i);
}
}
return 1;
}
Section 5
We'll add a group name command.
pawn Code:
CMD:groupname(playerid, params[])
{
new value, Name[255], string[128];
if(sscanf(params, "d, s[255]", value, Name))
{
SendClientMessage(playerid, WHITE, "SYNTAX: /groupname [value] [name]");
return 1;
}
format(string, sizeof(string), "Groups/%d.ini", value);
if(!fexist(string))
{
SendClientMessage(playerid, 0xFFFFFF, "Invalid Faction ID.");
return 1;
}
Groups[value][GroupName] = Name;
}
return 1;
}
This stock is going to save from a loop. I'll get to that in a minute when we use the stock.
pawn Code:
stock SaveGroup(i)
{
new FileName[21];
format(FileName, sizeof(FileName), "Groups/Group_%d.ini", i);
if(fexist(FileName))
{
dini_Set(FileName, "GroupName", Groups[i][GroupName]);
dini_IntSet(FileName, "GroupTypes", Groups[i][GroupTypes]);
printf("[system] Group %d saved.", i);
}
else
{
printf("Could not execute SaveGroup(%d).", i);
}
}
pawn Code:
CMD:groupname(playerid, params[])
{
new value, Name[255], string[128];
if(sscanf(params, "d, s[255]", value, Name))
{
SendClientMessage(playerid, WHITE, "SYNTAX: /groupname [value] [name]");
return 1;
}
format(string, sizeof(string), "Groups/%d.ini", value);
if(!fexist(string))
{
SendClientMessage(playerid, 0xFFFFFF, "Invalid Faction ID.");
return 1;
}
Groups[value][GroupName] = Name;
}
SaveGroup(value); // This will save the group depending upon the value placed in the value section of the command.
return 1;
}
pawn Code:
stock SaveGroups()
{
for(new i = 0; i < MAX_GROUPS; i++)
{
SaveGroup(i);
}
return 1;
}
If this helped, or you're just a generally nice person, please +REP
Got a question? Post below!
Thanks!