SA-MP Forums Archive
[Tutorial] How to make an inventory system (Saves/dialogs/YSI/ZCMD)) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] How to make an inventory system (Saves/dialogs/YSI/ZCMD)) (/showthread.php?tid=492260)



How to make an inventory system (Saves/dialogs/YSI/ZCMD)) - ross8839 - 03.02.2014

First tutorial so go easy on me... i have been experimenting for the past couple of hours and made my own basic inventory system that saves the items... and are in dialogs! So here goesssss

You have to have YSI include and zcmd. You can download them here:

YSI:
https://sampforum.blast.hk/showthread.php?tid=321092
ZCMD:
https://sampforum.blast.hk/showthread.php?tid=91354

Credits to ****** and Zeex for such perfect includes!

Right, I'm making this inventory system assuming you already have an admin/login/register system setup and so you can just add this to your system, for saving.

FIRST THINGS FIRST:

define your dialog ids, for the sake of this tutorial, this is what i've gone with!


Код:
#define DIALOG_INVENTORY 5 //this makes DIALOG_INVENTORY id 5 (fifth dialog! I have 4 others from my register system and you may have some too!)
#define DIALOG_DRUGS 6 //same goes here... but id 6, instead of 5
first of all of course you have to add what you wish to save, I've done drugs and a newspaper for the sake of the tutorial xD We have added pDrugs, pNewspaper, and pInventory to our enum which will allow us to make these things save! We have added inventory so that we can check if the inventory space is full, or how much is left in the inventory!
Код:
enum pInfo
{
	pPass,
	pCash,
	pAdmin,
	pKills,
	pDeaths,
	pDrugs, //here are the 3 added variables!
	pNewspaper,
	pInventory
}
Right, now you've done that, we need to add the following to the disconnect callback! To ensure that everything works properly, saves and loads, we must add the newspaper, inventory and drugs to the callbacks that your admin system uses! This will save the variable into the file for the user of which is disconnecting... and say you have 10 drugs, it will save those 10 drugs into the ini folder after Drugs... for example : Drugs = 10... and the same goes for newspaper and inventory... but the inventory is calculated by adding the 2 variables together...
Код:
PlayerInfo[playerid][pDrugs]+PlayerInfo[playerid][pNewspaper]
we'll go into further detail later down the tutorial!

Код:
public OnPlayerDisconnect(playerid,reason)
{
	new INI:File = INI_Open(UserPath(playerid));
	INI_SetTag(File,"data");
	INI_WriteInt(File,"Cash",GetPlayerMoney(playerid));
	INI_WriteInt(File,"Admin",PlayerInfo[playerid][pAdmin]);
	INI_WriteInt(File,"Kills",PlayerInfo[playerid][pKills]);
	INI_WriteInt(File,"Deaths",PlayerInfo[playerid][pDeaths]);
	INI_WriteInt(File,"Drugs",PlayerInfo[playerid][pDrugs]); //here are the three variables added to the save function under onplayerdisconnect! saving the three variables when the player disconnects
	INI_WriteInt(File,"Newspaper",PlayerInfo[playerid][pNewspaper]);
	INI_WriteInt(File,"Inventory",PlayerInfo[playerid][pInventory]);
	INI_Close(File);
	return 1;
}
now we have to add the following code to the onplayerdeath callback. What this does is it sets the variables in the save file back to 0, meaning you lose everything once you die! Pretty self explanatory really!

Код:
PlayerInfo[playerid][pDrugs] = 0;
PlayerInfo[playerid][pNewspaper] = 0;
Now, lets make a command that gives us the drugs, adding it to the save file! This is where you can really mess around the the variables and create a nice system. For the sake of the tutorial, I've kept it very simple for you guys... So, the following script will create a command, which will check the inventory space, add to the inventory space and make sure that you have sufficient inventory space for the item you're trying to add, in this case, it's drugs (no specific drug for the sake of the tutorial...
Код:
CMD:drugs(playerid,params[])
{
	new string[69]; //this creates the string, which is used to display specific values, from the ini save file!
	if(PlayerInfo[playerid][pInventory] > 6) //this is checking if the value of the inventory space is above 6... the reason for this, is that the max inv space is 11 (tutorial sake), and the drugs take up 5 slots... therefore, if the inv value is over 6, it won't work!
	{
		format(string, sizeof(string), "You need at least 6 spaces in your inventory! You only have %d",11-PlayerInfo[playerid][pInventory]); //this is where this comes in, this will be sent, if the inventory space is above 6 when trying to get drugs! I have the %d being replaced with 11-vairable... as this will find out whats left within the inventory... 11 - 6, 5 spaces left!
    	SendClientMessage(playerid, COLOR_RED, string); //this sends the string to the player, otherwise the string is insufficient and won't work! It will send the message in red.
	}
	else //this is switching the if statement around. For example: if it's above 6, it won't work... but if it is below 6, it will! It's basically switching the > to < instead of having to create a new if statement!
	{
		PlayerInfo[playerid][pInventory] += 5; //this will check the ini file for the playerinfo for the inventory, and then will add 5 to whats already there... 
		PlayerInfo[playerid][pDrugs] += 5; //this will add to the specific part of the ini which is for the drugs... we want to add to this variable too, so that we can check how many drugs is on the player, which is used later!
		format(string, sizeof(string), "You bought some drugs! You now have %d drugs!", PlayerInfo[playerid][pDrugs]); //this is formatting the string which is formatting exactly what is going to be sent to the player, once he gathers the drugs! The %d is linked with the PlayerInfo at the end, the %d will be replaced with what's in the PlayerInfo variable... for example, if the variable is equal to 10, it will replace the %d to 10!
		SendClientMessage(playerid, -1, string); //this again will send the string to the player but in the colour white (-1 is white!)
	}
	return 1;
}
now we've made that command, we must also make a command for the newspaper... which works exactly the same, just changing the variable names!

Код:
CMD:newspaper(playerid,params[])
{
	new string[69]; //as explained before, this is creating the string variable, which allows us to create the string! and the string size is 69... i dont even know why i put 69;)
	if(PlayerInfo[playerid][pInventory] < 11) //this checks if the inventory space value is less than 11 (so not full)
	{
	    if(PlayerInfo[playerid][pNewspaper] == 1) return SendClientMessage(playerid, COLOR_RED, "You already have a newspaper!"); //if it is less than 11 (max space), it will check if the newspaper variable is equal to 1 as you can't buy more than 1 newspaper. If you try to get another newspaper, it will terminate the command and send you a message, telling you, you can't buy another newspaper!
		PlayerInfo[playerid][pNewspaper]++; //++ will add 1 to the value of the variable... so of course, you can't buy more than 1 newspaper, so there's no need to add more than 1... 
		PlayerInfo[playerid][pInventory]++; //one newspaper will only take 1 space in the inventory,rather than the 5 the drugs take up... so again, we use ++ to add one.. (if you use -- it will take one away!)
		format(string, sizeof(string), "You bought a newspaper!",PlayerInfo[playerid][pNewspaper]); //as already explained, this is formatting the string, which will be sent to the player, when you gather a newspaper!
		SendClientMessage(playerid, -1, string); //sends the string, colour white!
	}
	else //switches the if statement for < to =>... so if the inv space is => 11, it will send this message
	{
		format(string, sizeof(string), "You ran out of inv space! You have %d/11",PlayerInfo[playerid][pInventory]); //formatting string
		SendClientMessage(playerid, COLOR_RED, string); //sending string
	}
	return 1;
}
A very simple command checking the inventory! All we have done here, is show the inventory dialog (the command is the writ in ondialogresponse)
Код:
CMD:inv(playerid, params[])
{
	ShowPlayerDialog(playerid, DIALOG_INVENTORY, DIALOG_STYLE_LIST, "Inventory:", "Drugs \n Newspaper", "Okay", "Close"); //Adds a dialog with the id: DIALOG_INVENTORY and the LIST style! It will be titled inventory and it only has two items in the list! The \n within the content section will put the next thing on a new line... 
	return 1;
}
Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	switch(dialogid)
	{
		case DIALOG_REGISTER: //this is the dialog when registering!
		{
		    if(!response) return Kick(playerid); //negative response will kick them (if they choose not to sign up and click the right hand button (cancel/quit)
		    if(response)
		    {
		        if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_REGISTER,DIALOG_STYLE_INPUT,""c_white"Register:",""c_white"Type your desired password below!","Register","Quit"); 
				new INI:File = INI_Open(UserPath(playerid));
				INI_SetTag(File,"data");
				INI_WriteInt(File,"Password",udb_hash(inputtext));
				INI_WriteInt(File,"Cash",0);
				INI_WriteInt(File,"Admin",0);
				INI_WriteInt(File,"Kills",0);
				INI_WriteInt(File,"Deaths",0);
				INI_WriteInt(File,"pDrugs",0); //adding our variables to the register dialog (this is writing the INI file! The 0 will set the variable value to 0 whenever someone signs up!
				INI_WriteInt(File,"pNewspaper",0); //explained
				INI_WriteInt(File,"pInventory",0); //explained!
				INI_Close(File);

				SetSpawnInfo(playerid, 0, 0, 1958.33, 1343.12, 15.36, 269.15, 0, 0, 0, 0, 0, 0);
				SpawnPlayer(playerid);
				ShowPlayerDialog(playerid, DIALOG_SUCCESS_1, DIALOG_STYLE_MSGBOX,""c_green"Success!",""c_green"You successfully registered!", "Okay","");
			}
		}
		case DIALOG_INVENTORY:
		{
		    if(response)
		    {
		        switch(listitem)
		        {
		        	case 0:
		        	{
		        	    new string[256]; //variable string
		        	    format(string, sizeof(string), "Weed: %d", PlayerInfo[playerid][pDrugs]); //this is formatting the string for the dialog! If you wanted to add any more to this, you would just add the \n after weed %d and then carry on adding more!
		        		ShowPlayerDialog(playerid, DIALOG_DRUGS, DIALOG_STYLE_LIST, "Drugs:", string, "Okay", "Close"); //this will show the dialog, the 'string' just makes sure the script/game knows that the format(string we did, is what goes there... 
					}
				}
			}
			return 1;
		}
	}
		case DIALOG_DRUGS: //drugs dialogid!
		{
			if(response)
			{
			    switch(listitem)
			    {
			    	case 0:
			    	{
			    	    if(PlayerInfo[playerid][pDrugs] > 1) //this will check if the player has any drugs on him, if he does, the command will continue...
			    	    {
    	 			   		SendClientMessage(playerid, -1, "you consumed 1 Drug!");
	    	    			PlayerInfo[playerid][pDrugs]--; //this will take away 1 drug using the --
	    	    			PlayerInfo[playerid][pInventory]--; //this will take 1 from the inventory, making for example, 11, 10... allowing you to gather something else that equals 1 space!
						}
						else //switches the if statement! if the player has less than 1, it will send the following message and terminate the command!
						{ 
						    SendClientMessage(playerid, COLOR_RED, "You don't have any drugs on you!"); //send the message in red
						}
					}
    			        }
			}
			return 1;
		}
	}
	return 1;
}
right, we're almost there guys!!

Just one more thing! We have to add the variables to the load user function...

Код:
forward LoadUser_data(playerid,name[],value[]);
public LoadUser_data(playerid,name[],value[])
{
	INI_Int("Password", PlayerInfo[playerid][pPass]);
	INI_Int("Cash",PlayerInfo[playerid][pCash]);
	INI_Int("Admin",PlayerInfo[playerid][pAdmin]);
	INI_Int("Kills",PlayerInfo[playerid][pKills]);
	INI_Int("Deaths",PlayerInfo[playerid][pDeaths]);
	INI_Int("Drugs",PlayerInfo[playerid][pDrugs]); //.adds the variable to the load function, loading the variable that was saved! same goes for the other variables that you add/added!
	INI_Int("Newspaper",PlayerInfo[playerid][pNewspaper]);
	INI_Int("Inventory",PlayerInfo[playerid][pInventory]);
	return 1;
}
thats the tutorial guys! if you have any tips on how i can improve this tutorial or code... then sure do say so!

I'd also appreciate it if you wouldn't turn around and just tell me it's shit, let me know why it's not so good! remember it's my first tutorial and from all i can tell, through me testing it, it works fine


Re: How to make an inventory system (Saves/dialogs/YSI/ZCMD)) - Pottus - 03.02.2014

I think the main problem with this tutorial is it's not really an inventory system you have login stuff mixed in and really all this is a shoddy register system with a few extras tossed in (inventory examples).


Re: How to make an inventory system (Saves/dialogs/YSI/ZCMD)) - ross8839 - 03.02.2014

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
I think the main problem with this tutorial is it's not really an inventory system you have login stuff mixed in and really all this is a shoddy register system with a few extras tossed in (inventory examples).
Okay, yeah I fail to see how it isn't an inventory system? You can take what you learn in this tutorial, and add more to the inventory system... and I'm using the login/register system for the saving of the items within the inventory... how doesn't that make it an inventory system?


Re: How to make an inventory system (Saves/dialogs/YSI/ZCMD)) - Pottus - 03.02.2014

It should be completely separate from your login system your crossing systems which is a very bad practice.


Re: How to make an inventory system (Saves/dialogs/YSI/ZCMD)) - ross8839 - 03.02.2014

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
It should be completely separate from your login system your crossing systems which is a very bad practice.
I personally think merging them both together gives good practice... having them both as one, (which works the same as having them seperate, right?) means there all part of the master file for that player, saving the system from having to save twice? And from this tutorial, they will be able to make this without the login system..


Re: How to make an inventory system (Saves/dialogs/YSI/ZCMD)) - Pottus - 03.02.2014

It's never good to mix systems it causes a lot of problems in the long run.


Re: How to make an inventory system (Saves/dialogs/YSI/ZCMD)) - ross8839 - 03.02.2014

I cant expect the inventory saving through the same system as the register is going to mess anything up in the long run, if so what?