[Tutorial] Creating Dynamic Saved Groups
#1

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.
pawn Code:
#define                 MAX_GROUPS                          10
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.

pawn Code:
LoadGroups();
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:

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;
}
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.
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;
}
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

pawn Code:
enum 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.

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.
};
Now lets jump back to our stock. Remember? LoadGroups? And add these two functions too it.

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;
}
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.

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;
}
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.
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);
    }
}
To use this, lets put this stock function under the create group 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;
    }
        SaveGroup(value); // This will save the group depending upon the value placed in the value section of the command.
    return 1;
}
Finally, lets add a save groups function. Which will save all groups.

pawn Code:
stock SaveGroups()
{
    for(new i = 0; i < MAX_GROUPS; i++)
    {
        SaveGroup(i);
    }
    return 1;
}
CONGRATULATIONS!

If this helped, or you're just a generally nice person, please +REP

Got a question? Post below!

Thanks!
Reply
#2

Simple but good for a beginner.
Reply
#3

Nice.

Good luck in next tutorials.
Reply
#4

Quote:
Originally Posted by BornHuman
THIS IS UNTESTED.
Nice Tutorial, but you need to test your code before posting it.
Reply
#5

Nice and simple, Good work.
Reply
#6

you have a big mistake in your tutorial if you found it tell me else i will tell you here
i wanna you to search yourself to learn what is it
Reply
#7

Quote:
Originally Posted by amirab
View Post
you have a big mistake in your tutorial if you found it tell me else i will tell you here
i wanna you to search yourself to learn what is it
Believe me. It's probably not an error I have never seen before. However, if you have noticed an error that isn't loose identification, please feel free to share with the class.
Reply
#8

Dude. Fuck yes.
Reply
#9

Thank you! Works wonderful!

REP+
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)