[Tutorial] Inventory System (new.pwn)
#1

Basic Inventory On Dialogs

Hello there! My previous tutorial was really awful, and I don't promise that this will be better. But I try to correct my mistakes as much as I can.

So, The reason why I DO NOT recommend u to use this code:

- It's absolutely on dialog, without any textdraw.
- This code is a bit outdated.


The reason why I DO recommend u to use this code:

- This code is very useful for absolutely new.pwn (For test, after that u can modify it or re-write on textdraws)
- Some dayz servers use this code



Tutorial contain:
- System of Registration/Login and Saving account by using Dini.
- System of Inventory
- System of Hunger and Thirst
- Screenshots
- Archive with Includes and Plugins

How do it look like
https://imgur.com/a/gr6JI







To all includes
PHP Code:
#include <a_samp> // Include Basic Samp Functions
#include <dini> // Our read/write (also u can use y_ini)
#include <streamer> // Use streamer instead of standard CreateObject (streamer for loot) 
After that make defines of our dialogs:
PHP Code:
#define DIALOG_REGISTER 1
#define DIALOG_LOGIN 2
#define MAX_HUNGER_AMOUNT 3000 // Hunger Stat
#define MAX_THIRST_AMOUNT 3600 // Thirsty Stat
#define COLOR_GREY 0xAFAFAFAA
#define Dialog_Inventory 3 // Inventory Dialog
#define Dialog_Choose 5 // Use/Drop/Info Dialog
#define Dialog_TakeInv 6 // 
To all news we need to add variables
PHP Code:
new total_vehicles_from_files=3000// for our loot system (It will use LoadStaticVehiclesFromFile)
new backpack[MAX_PLAYERS];
new 
file128 ]; // Using for our file ( format( file, sizeof( file ), "Users/%s.ini", GetName(playerid)); ) 
PHP Code:
#define MAX_OBJ 10000 // Max obj 
#define INVNUMBERS 6 // up this number to make more items
new PlayerAdminMassage[MAX_PLAYERS][INVNUMBERS];
new 
playermassagetoadmin[MAX_PLAYERS]; 
Add enum of PlayerInfo, and PlayerInv
PHP Code:
enum pInfo
{
    
pPass// Player Password
    
pAdmin,
    
pHungry// Hunger stat
    
pThirsty// Thirst stat
    
pPack// Player Backpack
    
pSlots // Player Slots in Inventory
}
new 
PlayerInfo[MAX_PLAYERS][pInfo];
enum wInfo //weapon slots info
{
    
wSlot0,
    
wSlot1,
    
wSlot2,
    
wSlot3,
    
wSlot4,
    
wSlot5,
    
wSlot6,
    
wSlot7,
    
wSlot8,
    
wSlot9,
    
wSlot10,
    
wSlot11,
    
wSlot12,
}
new 
Weapon[500][wInfo];
enum dInvEnum
{
Float:ObjPos[3], // Object pos in map
    
ObjID// Object ID
    
ObjData // Object data info for dialog_choose -> info
};
new 
PlayerInv[MAX_PLAYERS][INVNUMBERS]; // Use for giving player loot (for example PlayerInv[playerid][3] = 2; - we give player Bean Can x2 ) 
After that we need to create arrays which will include information about Name of items, object ID's, and what is it for (dialog_choose -> info)
PHP Code:
new InvNames[INVNUMBERS][] = // Names of items in inventory (I also recommend to write down object id near the name)
//2040 iao?iiu
    
"Empty"//1581
    
"Dry Food",//2663
    
"Bean Can",//1666
    
"Sprunk",//1546
    
"Roast Meat",//1546
    
"Alice Backpack"// 3026
};
new 
InvObjects[INVNUMBERS] = // When u'll drop items it will use this ID's.
{
    
1581//empty
    
2663//
    
1666//bean
    
1546//Sprunk
    
2806//meat
    //backpacks (for instance)
    
3026
};
enum subinfo // Give information about items for example ( dialogmsgbox ) - Dry Food - Up your hunger stat
{
    
iD_sub[64],//noaouy
    
iD_text[256]//Iacaaiea
};
new 
InfoSub[INVNUMBERS][subinfo] = {
    {
"Empty""Empty."},
    {
"Dry Food""Up your hunger stat"},
    {
"Bean Can""Up your hunger stat"},
    {
"Sprunk""Up your thirsty stat"},
    {
"Roast Meat""Up your hunger stat"},
    {
"Alice BackPack""You have more slots for items (loot)"}
};
new 
dInvData[MAX_OBJ][dInvEnum]; 
Go to the end of your mode and put this stock

PHP Code:
stock GetName(playerid// This stock more better than always use GetPlayerName
{
    new 
name32 ]; // array which write down player nickname
    
GetPlayerName(playeridnamesizeofname )); // Get player name
    
return name// return this name for %s


Quick Registration/Login System

public OnPlayerConnect(playerid) // When Player connect
PHP Code:
formatfilesizeoffile ), "Users/%s.ini"GetName(playerid)); // Use format for getting player file. For example Mr_Bean.ini
    
if(dini_Exists(file)) // If player file exist show him dialog of login
    
{
        new 
string[128];
        
format(string,sizeof(string),"Hello %s.\nWrite down you pass below:",GetName(playerid)); // Get Player Name and show it in dialog
          
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_INPUT,"Log in",string,"Enter","Quit");
    }
    else 
// If else - Player file not exist show him dialog of register
    
{
        new 
string[128];
        
format(string,sizeof(string),"Write down you pass below:",GetName(playerid));// Get Player Name and show it in dialog
         
ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_INPUT,"Registration",string,"Register","Quit");
    } 

public OnPlayerDialogResponse
PHP Code:
switch( dialogid )
    {
        case 
DIALOG_REGISTER// Register dialog
        
{
            if (!
response) return Kick(playerid); // If player click 'Esc' or Quit button - kick him
            
if(response// If Player click 'Enter' or Register button - do next step
            
{
                if(!
strlen(inputtext)) {new string[128];format(string,sizeof(string),"Incorrect pass type, make new pass:");ShowPlayerDialog(playeridDIALOG_REGISTER
DIALOG_STYLE_INPUT"Register",string,"Register","Quit");} // If the line in dialog is empty show player this dialog again with message
                
else // If the line in dialog NOT empty do next step
                
{
                
formatfilesizeoffile ), "Users/%s.ini"GetName(playerid)); // Get Player Name and path to our file
                
dini_Createfile ); // Dini create file in folder scriptfiles/Users/%s.ini
                
dini_Setfile"Password"inputtext); // In this file write down player new password entered in dialog of register
                
dini_IntSetfile"Admin"); // Write down other PlayerInfo stat
                
dini_IntSetfile"Pack");
                
dini_IntSetfile"Slots");
                
dini_IntSetfile"Hungry");
                
dini_IntSetfile"Thirsty");
                        
PlayerInfoplayerid ][ pHungry ] = MAX_HUNGER_AMOUNT// After register give player full amount of Hunger
                        
PlayerInfoplayerid ][ pThirsty ] = MAX_THIRST_AMOUNT;
                        
PlayerInfo[playerid][pPack] = 1;
                
PlayerInfo[playerid][pSlots] = 3;// Give player 3 slots for items ('cuz other string is items )
                
PlayerInv[playerid][1] = 1// 1 item - Dry Food x1
                
PlayerInv[playerid][2] = 1// 2 item - Bean Can x1
                
PlayerInv[playerid][3] = 1// 3 item - Sprunk x1
                //Spawn(playerid);
                
}
            }
        }
        case 
DIALOG_LOGIN// Login dialog
        
{
            if ( !
response ) return Kick playerid ); // If player click 'Esc' or Quit button - kick him
            
if( response // If Player click 'Enter' or Login button - do next step
            
{
                new 
pass256 ]; // Create array to get player pass
                
formatfilesizeoffile ), "Users/%s.ini"GetName(playerid)); // Get player path to fil
                
formatpasssizeofpass ), "%s"dini_Get(file"Password")); // array 'pass' will contain info about Password from player file
                //new lenght = strlen(inputtext); 
                
if(strcmp(inputtextpass) == 0// If inputtext = Player Password from file - setup players pInfo stats
                
{
                               
PlayerInfoplayerid ][ pAdmin ] = dini_Intfile"Admin");
                
PlayerInfoplayerid ][ pPack ] = dini_Intfile"Pack");
                
PlayerInfoplayerid ][ pSlots ] = dini_Intfile"Slots");
                
PlayerInfoplayerid ][ pHungry ] = dini_Intfile"Hungry");
                
PlayerInfoplayerid ][ pThirsty ] = dini_Intfile"Thirsty");
                    for(new 
i=0;i<INVNUMBERS;i++) // For every INVNUMBERS slots get info about inventory
                
{
                    new 
idstr[256]/*, ammostr[128]*/;
                    
formatfilesizeoffile ), "Users/%s.ini"GetName(playerid)); // Get file path
                    
format(idstrsizeof(idstr), "id%d"i);
                    
PlayerInv[playerid][i] = dini_Intfileidstr ); //Load player invetory from file
                    //dini_Int(file, idstr, PlayerInv[playerid][i]);
                    //Player[playerid][pSlots] = i;
                
}
                }
                else 
// If inputtext is not equal to Player Password from file - show this dialog again
                
{
                    
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_INPUT,"Log In","Incorrect pass!!!","Enter","Quit");
                }
                return 
1;
            }
        }
    } 

public OnPlayerDisconnect - when player disconnect
PHP Code:
format(filesizeof(file), "Users/%s.ini"GetName(playerid)); // Get file path
    
if( dini_Existsfile ) ) // If player file exist write down his new stats to file
    
{
        
dini_IntSetfile"Admin"PlayerInfoplayerid ][ pAdmin ]);
        
dini_IntSetfile"Pack"PlayerInfo[playerid][pPack]);
        
dini_IntSetfile"Slots"PlayerInfo[playerid][pSlots]);
        
        for(new 
i=0;i<INVNUMBERS;i++) // For every slots get info about inventory and write down it to file
        
{
        new 
idstr[256];
        
format(idstrsizeof(idstr), "id%d",i); // For example id1 - it's 'Dry Food', id2 - 'Bean Can'
        
dini_IntSetfileidstrPlayerInv[playerid][i]); // Write down it to every slots
        
}
    } 
We have created Reg/Log system


Inventory (Picking up/Dropping objects)

public OnPlayerKeyStateChange
PHP Code:
if(newkeys == KEY_YES// If player click 'Y' button - Show Player Inventory
    
{
        
Inventory(playerid);//open inventory
        
return 1;
    }
    
    if(
newkeys == KEY_CROUCH// If Player click 'C' button - Show Player Loot point
    
{
        
Equipment(playerid); // Player need to be near loot point to use this
        
return 1;
    } 
Go again to the end of the mode and add this stocks

PHP Code:
stock Equipment(playerid// Loot point
{
    if(
GetPlayerState(playerid) != PLAYER_STATE_ONFOOT) return 1;
    new 
total_vehicles_from_files+1;
    new 
str[1024], listitems[1024], adm=0;
    for(new 
0total_vehicles_from_filesa++) // total_vehicles_from_files it's our object loaded from file
    
{
        if(
IsPlayerInRangeOfPoint(playerid1.8dInvData[a][ObjPos][0], dInvData[a][ObjPos][1], dInvData[a][ObjPos][2])) // If is player near the loot
        
{
            
a;
            
PlayerAdminMassage[playerid][adm] = f// Get info
            
adm ++;
            
format(strsizeof(str), "%s\n"InvNames[dInvData[f][ObjData]]); // Get Names of object 
            
if(dInvData[f][ObjData] > 0strins(listitemsstrstrlen(listitems), strlen(str)); // And put it for dialog TakeInv
        
}
    }
    if(
total_vehicles_from_files) return 1;
    
ShowPlayerDialog(playerid,Dialog_TakeInv,DIALOG_STYLE_LIST,"Equipment",listitems,"Take","Quit"); // Show Player TakeInv dialog
    
return 1;
}
stock Inventory(playerid// Inventory (main core)
{
   new 
str[1024], slotstr[1024];
    new 
listitems[1024], adm=0;
    for(new 
slot;slot<INVNUMBERS;slot++) // For every slots of INVNUMBERS
    
{
        if(
PlayerInv[playerid][slot] > && slot != 0)  // If player inventory is not empty
        
{
            
PlayerAdminMassage[playerid][adm] = slot;
            
adm ++;
            
format(strsizeof(str), "[%d]\t%s\n"PlayerInv[playerid][slot],InvNames[slot]); // Get info amount - name (For instance [1] - Bean Can)
            
strins(listitemsstrstrlen(listitems), strlen(str));
        }
    }
//    if(PlayerInfo[playerid][pPack] == 0) format(slotstr,sizeof(slotstr),"No BackPack [%d/%d]",PlayerInfo[playerid][pSlots],PlayerInfo[playerid][pPack]*10);
    
if(PlayerInfo[playerid][pPack] == 1format(slotstr,sizeof(slotstr),"No Backpack [%d/%d]",PlayerInfo[playerid][pSlots],PlayerInfo[playerid][pPack]*10);
    
ShowPlayerDialog(playerid,Dialog_Choose,DIALOG_STYLE_LIST,slotstr,listitems,"Choose","Quit"); // Show Player Inventory
    
return 1;
}
stock CreateDroppedInv(idFloat:gPosXFloat:gPosYFloat:gPosZ// When player drop items from his inventory
{
    if(
id == 0) return 1;
    new 
total_vehicles_from_files+1;
    for(new 
0total_vehicles_from_filesa++)
    {
        if(
dInvData[a][ObjPos][0] == 0.0// Object pos 0.0 will be use for respawning loot
        
{
            
a;
            break;
        }
    }
    if(
total_vehicles_from_files) return 1;
    
    
dInvData[f][ObjData] = id// Get information about object which we want to drop
    
dInvData[f][ObjPos][0] = gPosX// GetPlayerPos X - to drop near the player
    
dInvData[f][ObjPos][1] = gPosY// GetPlayerPos Y
    
dInvData[f][ObjPos][2] = gPosZ// GetPlayerPos Z
    
new x_l random(2);
    new 
y_l random(2);
    
//Use 'CreateDynamicObject' from streamer because standard is limited
    
dInvData[f][ObjID] = CreateDynamicObject(InvObjects[id], dInvData[f][ObjPos][0]+x_ldInvData[f][ObjPos][1]+y_ldInvData[f][ObjPos][2]-1.00.0360.0250.0);
    if(
id == 78SetDynamicObjectMaterial(dInvData[f][ObjID], 02060"cj_ammo""CJ_CANVAS2"0);
    
//printf("%d,%f",id,dInvData[f][ObjPos][0]);
    
return 1;
}
stock RemovePlayerWeapon(playeridweaponid)
{
    if(!
IsPlayerConnected(playerid) || weaponid || weaponid 50)
    return;
    new 
saveweapon[13], saveammo[13];
    for(new 
slot 0slot 13slot++)
    
GetPlayerWeaponData(playeridslotsaveweapon[slot], saveammo[slot]);
    
ServerResetPlayerWeapons(playerid);
    for(new 
slotslot 13slot++)
    {
        if(
saveweapon[slot] == weaponid || saveammo[slot] == 0)
        continue;
        
ServerGivePlayerWeapon(playeridsaveweapon[slot], saveammo[slot]);
    }
    
ServerGivePlayerWeapon(playerid01);
}
//
stock UseItem(playerid,itemid// Use item (Here's main core for adding new items etc.)
{
    if(
itemid == 1)//Dry Food
    
{
        new 
Float:health;
        
GetPlayerHealth(playerid,health);
        
SetPlayerHealth(playeridhealth+15);
        
PlayerInfo[playerid][pHungry] = MAX_HUNGER_AMOUNT;
        
PlayerInv[playerid][itemid]--;
        
PlayerInfo[playerid][pSlots] --;
        
SendClientMessage(playerid,0xA9A9A9ff,"* You have ate Dry Food");
    }
    if(
itemid == 2)//Bean can
    
{
        new 
Float:health;
        
GetPlayerHealth(playerid,health);
        
SetPlayerHealth(playerid,health+5);
        
PlayerInfo[playerid][pHungry] = MAX_HUNGER_AMOUNT;
        
PlayerInv[playerid][itemid]--;
        
PlayerInfo[playerid][pSlots] --;
        
SendClientMessage(playerid,0xA9A9A9ff,"* You have ate Bean Can");
    }
    if(
itemid == 3)//Sprunk
    
{
        new 
Float:health;
        
GetPlayerHealth(playerid,health);
        
SetPlayerHealth(playerid,health+2.0);
        
PlayerInfo[playerid][pThirsty] = MAX_THIRST_AMOUNT;
        
PlayerInv[playerid][itemid]--;
        
PlayerInfo[playerid][pSlots] --;
        
SendClientMessage(playerid,0xA9A9A9ff,"* You have drunk Sprunk");
    }
    if(
itemid == 4)//Roast Meat
    
{
        new 
Float:health;
        
GetPlayerHealth(playerid,health);
        
SetPlayerHealth(playerid,health+20);
        
PlayerInfo[playerid][pHungry] = MAX_HUNGER_AMOUNT;
        
PlayerInv[playerid][itemid]--;
        
PlayerInfo[playerid][pSlots] --;
        
SendClientMessage(playerid,0xA9A9A9ff,"* You have ate Roast Meat");
    }
        return 
1;

public OnDialogResponse

PHP Code:
if(dialogid == Dialog_Inventory// Inventory Dialog
    
{
        if(
response == 1)
        {
            if(
listitem == 0// Player click 'Use' - Use this item
            
{
                
UseItem(playerid,playermassagetoadmin[playerid]);
            }
            if(
listitem == 1// Player click 'Drop' - Drop this item near player
            
{
                if(
IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playeridCOLOR_GREY"You can't do this in car!"); // Forbid player drop 
item in vehicle
                
if(PlayerInv[playerid][playermassagetoadmin[playerid]] == 0) return 1;
                new 
str[128],Float:pos[3]; // new array for format and player pos
                
GetPlayerPos(playerid,pos[0],pos[1],pos[2]); // Get info about player position
                
CreateDroppedInv(playermassagetoadmin[playerid],pos[0],pos[1],pos[2]); // Create items near player
                
format(str,sizeof(str),"{A9A9A9}* You have dropped %s from your inventory",InvNames[playermassagetoadmin[playerid]]); 
                
PlayerInv[playerid][playermassagetoadmin[playerid]]--; // Remove this item from inventory
                
PlayerInfo[playerid][pSlots] --; // Remove 1 slots, because we have dropped 1 item ( 1 item - 1 slot )
                
SendClientMessage(playerid,0xFF0000FF,str);
                
total_vehicles_from_files++; // +1 to world loot object
            
}
            if(
listitem == 2// Player click 'Info' - Show him dialog with info about this item
            
{
                new 
string[512], Desc_sub[512], nazvanie_one[64], Name_sub[64];
                
strmid(Name_subInfoSub[playermassagetoadmin[playerid]][iD_sub], 0strlen(InfoSub[playermassagetoadmin[playerid]][iD_sub]), 255);
                
strmid(Desc_subInfoSub[playermassagetoadmin[playerid]][iD_text], 0strlen(InfoSub[playermassagetoadmin[playerid]][iD_text]), 512);
                
format(nazvanie_onesizeof(nazvanie_one),"%s"Name_sub);
                
format(stringsizeof(string), "%s"Desc_sub);
                
ShowPlayerDialog(playerid,465,DIALOG_STYLE_MSGBOX,nazvanie_one,string,"OK","");
            }
        }
        return 
1;
    }
    if(
dialogid == Dialog_Choose)
    {
        if(
response == 1)
        {
            new 
nazvanie_one[64];
            
playermassagetoadmin[playerid] = PlayerAdminMassage[playerid][listitem];
            
format(nazvanie_onesizeof(nazvanie_one),  "%s"InvNames[playermassagetoadmin[playerid]]);
            new 
listitems[] = "- Use\n- Drop\n- Info";
            
ShowPlayerDialog(playerid,Dialog_Inventory,DIALOG_STYLE_LIST,nazvanie_one,listitems,"Choose","Back");
        }
        if(
response == 0)
        {
        }
        return 
1;
    }
    if(
dialogid == Dialog_TakeInv)
    {
        
//new slots = PlayerInfo[playerid][pPack]*10-PlayerInfo[playerid][pSlots];
        
playermassagetoadmin[playerid] = PlayerAdminMassage[playerid][listitem];
        if(
response == 1)
        {
            if(
dInvData[playermassagetoadmin[playerid]][ObjData] == 0) return SendClientMessage(playerid0xA9A9A9FF"* Sorry, sbs take this object");
            new 
buffer[120];
            
format(buffersizeof(buffer), "* %s now in your backpack."InvNames[dInvData[playermassagetoadmin[playerid]][ObjData]]);
                if(
PlayerInfo[playerid][pSlots]  >= PlayerInfo[playerid][pPack]*10) return SendClientMessage(playerid0xA9A9A9FF"* Your backpack is 
full, drop something."
);
                if(
PlayerInv[playerid][dInvData[playermassagetoadmin[playerid]][ObjData]] > 0PlayerInv[playerid][dInvData[playermassagetoadmin
[playerid]][ObjData]] +=1PlayerInfo[playerid][pSlots] ++;
                else 
PlayerInv[playerid][dInvData[playermassagetoadmin[playerid]][ObjData]] +=1PlayerInfo[playerid][pSlots] ++;
                
SendClientMessage(playerid0xA9A9A9FFbuffer);
            }
            
//DestroyDynamic3DTextLabel(loot[playermassagetoadmin[playerid]]);
            
total_vehicles_from_files--;
            
DestroyDynamicObject(dInvData[playermassagetoadmin[playerid]][ObjID]);
            
dInvData[playermassagetoadmin[playerid]][ObjPos][0] = 0.0;
            
dInvData[playermassagetoadmin[playerid]][ObjPos][1] = 0.0;
            
dInvData[playermassagetoadmin[playerid]][ObjPos][2] = 0.0;
            
dInvData[playermassagetoadmin[playerid]][ObjID] = -1;
            
dInvData[playermassagetoadmin[playerid]][ObjData] = 0;
            
Equipment(playerid);//Iiyou ioe?uoea
            
return 1;
        }
        } 

Hunger and Thirst System

Add to all forward

PHP Code:
forward ProcessAllPlayers(); // All player process
forward ProcessPlayerHunger(playerid); // Hunger
forward ProcessPlayerThirsty(playerid); // Thirst 
public OnPlayerGameModeInit

PHP Code:
SetTimer("ProcessAllPlayers",1000,1); // Timer with repeating every seconds 
Create this publics

PHP Code:
public ProcessPlayerHunger(playerid)
{
    
PlayerInfoplayerid ][ pHungry ]--; // every second -1 hunger stat
    
if(PlayerInfoplayerid ][ pHungry ] <= 0)// If player hunger stat <= 10 - kill player
    
{
    
SetPlayerHealth(playerid, -10);
    }
    return 
1;
}
public 
ProcessPlayerThirsty(playerid)
{
    
PlayerInfoplayerid ][ pThirsty ]--; // every second -1 thirst stat
    
if(PlayerInfoplayerid ][ pThirsty ] <= -10// If player thirst stat <= 10 - kill player
    
{
    
SetPlayerHealth(playerid, -10);
    }
    return 
1;
}
public 
ProcessAllPlayers()
{
    for (new 
0MAX_PLAYERSi++)
    {
        if (
PlayerInfo][ pLogged ] == true)
        {
        
            
ProcessPlayerHunger(i);
            
            
ProcessPlayerThirsty(i);
        }
    }
    return 
1;

Reply


Messages In This Thread
Inventory System (new.pwn) - by Adain - 11.11.2017, 23:17
Re: Inventory System (new.pwn) - by Kane - 12.11.2017, 00:00
Re: Inventory System (new.pwn) - by Ponga - 12.11.2017, 01:13
Re: Inventory System (new.pwn) - by Zeth - 12.11.2017, 02:59
Re: Inventory System (new.pwn) - by Adain - 12.11.2017, 07:18
Re: Inventory System (new.pwn) - by Eoussama - 12.11.2017, 08:24
Re: Inventory System (new.pwn) - by Janoz12 - 22.06.2018, 21:07
Re: Inventory System (new.pwn) - by CodeStyle175 - 22.06.2018, 21:22
Re: Inventory System (new.pwn) - by Logic_ - 22.06.2018, 21:32
Re: Inventory System (new.pwn) - by JasonRiggs - 22.06.2018, 22:24

Forum Jump:


Users browsing this thread: 2 Guest(s)