SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 29/10/2014, 11:43 PM   #1
BornHuman
Huge Clucker
 
BornHuman's Avatar
 
Join Date: Sep 2013
Location: 127.0.0.1
Posts: 360
Reputation: 33
Default Creating Dynamic Saved Groups

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!
__________________


_________________________________________________

Retired NGRP Director of Development
_________________________________________________


Projects:
Peak Gaming Roleplay
Vortex Roleplay 1 [EDIT]
Jason's Dynamic Housing System


Last edited by BornHuman; 30/05/2016 at 05:20 PM.
BornHuman is offline   Reply With Quote
Old 29/10/2014, 11:49 PM   #2
Alex Magaņa
High-roller
 
Alex Magaņa's Avatar
 
Join Date: Oct 2012
Location: 𝕭𝖗𝖔𝖙𝖍𝖊𝖗𝖍𝖔𝖔𝖉
Posts: 2,443
Reputation: 211
Default Re: Creating Dynamic Saved Groups

Simple but good for a beginner.
__________________
Alex Magaņa is online now   Reply With Quote
Old 30/10/2014, 04:17 AM   #3
Glossy42O
High-roller
 
Join Date: Oct 2014
Location: in my house
Posts: 1,853
Reputation: 231
Default Re: Creating Dynamic Saved Groups

Nice.

Good luck in next tutorials.
Glossy42O is offline   Reply With Quote
Old 30/10/2014, 09:34 AM   #4
seanny
High-roller
 
seanny's Avatar
 
Join Date: Oct 2010
Location: Derry City, Northern Ireland
Posts: 1,493
Reputation: 41
Default Re: Creating Dynamic Saved Groups

Quote:
Originally Posted by BornHuman
THIS IS UNTESTED.
Nice Tutorial, but you need to test your code before posting it.
__________________
Left the SAMP scene.
seanny is offline   Reply With Quote
Old 30/10/2014, 10:16 AM   #5
Rudy_
High-roller
 
Rudy_'s Avatar
 
Join Date: Nov 2011
Location: Pakistan
Posts: 1,512
Reputation: 228
Default Re: Creating Dynamic Saved Groups

Nice and simple, Good work.
__________________
And now my Watch has Ended.


[Tutorial]
[TuT]How to create a car shooting Neon

[Filterscript]
Neon Vehicle Laser


__________________________________________________
Rudy_ is offline   Reply With Quote
Old 30/10/2014, 12:46 PM   #6
amirab
Banned
 
Join Date: Apr 2013
Location: UK maybe :P
Posts: 509
Reputation: 69
Default Re: Creating Dynamic Saved Groups

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
amirab is offline   Reply With Quote
Old 30/10/2014, 07:51 PM   #7
BornHuman
Huge Clucker
 
BornHuman's Avatar
 
Join Date: Sep 2013
Location: 127.0.0.1
Posts: 360
Reputation: 33
Default Re: Creating Dynamic Saved Groups

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.
__________________


_________________________________________________

Retired NGRP Director of Development
_________________________________________________


Projects:
Peak Gaming Roleplay
Vortex Roleplay 1 [EDIT]
Jason's Dynamic Housing System

BornHuman is offline   Reply With Quote
Old 30/10/2014, 08:37 PM   #8
xXRealLegitXx
Big Clucker
 
Join Date: Sep 2012
Posts: 154
Reputation: 1
Default Re: Creating Dynamic Saved Groups

Dude. Fuck yes.
xXRealLegitXx is offline   Reply With Quote
Old 02/11/2014, 01:43 PM   #9
sexybutliteral
Little Clucker
 
Join Date: Jun 2014
Posts: 4
Reputation: 1
Default Re: Creating Dynamic Saved Groups

Thank you! Works wonderful!

REP+
sexybutliteral is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Object Creating AphexCCFC Scripting Help 6 29/04/2014 12:52 AM
Creating player groups Stuneris Scripting Help 2 09/03/2014 04:05 AM
Hello I know for some people it's easy.. For me it's hard to figure out how to save Groups correctly(y groups) Scrillex Scripting Help 0 26/02/2013 03:20 PM
How to set a groups car rank, And creating a despawn saved car.. patfay Help Archive 1 20/11/2010 02:46 PM
Creating saved 3DTextLabel OnGameModeInit AirGuy92 Help Archive 1 05/11/2010 09:11 PM


All times are GMT. The time now is 01:25 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.