11.11.2017, 23:17
(
Last edited by Adain; 12/11/2017 at 06:49 AM.
)
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
After that make defines of our dialogs:
To all news we need to add variables
Add enum of PlayerInfo, and PlayerInv
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)
Go to the end of your mode and put this stock
Quick Registration/Login System
public OnPlayerConnect(playerid) // When Player connect
public OnPlayerDialogResponse
public OnPlayerDisconnect - when player disconnect
We have created Reg/Log system
Inventory (Picking up/Dropping objects)
public OnPlayerKeyStateChange
Go again to the end of the mode and add this stocks
public OnDialogResponse
Hunger and Thirst System
Add to all forward
public OnPlayerGameModeInit
Create this publics
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)
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 //
PHP Code:
new total_vehicles_from_files=3000; // for our loot system (It will use LoadStaticVehiclesFromFile)
new backpack[MAX_PLAYERS];
new file[ 128 ]; // 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];
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 )
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];
PHP Code:
stock GetName(playerid) // This stock more better than always use GetPlayerName
{
new name[ 32 ]; // array which write down player nickname
GetPlayerName(playerid, name, sizeof( name )); // Get player name
return name; // return this name for %s
}
Quick Registration/Login System
public OnPlayerConnect(playerid) // When Player connect
PHP Code:
format( file, sizeof( file ), "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(playerid, DIALOG_LOGIN, DIALOG_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(playerid, DIALOG_REGISTER, DIALOG_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(playerid, DIALOG_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
{
format( file, sizeof( file ), "Users/%s.ini", GetName(playerid)); // Get Player Name and path to our file
dini_Create( file ); // Dini create file in folder scriptfiles/Users/%s.ini
dini_Set( file, "Password", inputtext); // In this file write down player new password entered in dialog of register
dini_IntSet( file, "Admin", 0 ); // Write down other PlayerInfo stat
dini_IntSet( file, "Pack", 0 );
dini_IntSet( file, "Slots", 0 );
dini_IntSet( file, "Hungry", 0 );
dini_IntSet( file, "Thirsty", 0 );
PlayerInfo[ playerid ][ pHungry ] = MAX_HUNGER_AMOUNT; // After register give player full amount of Hunger
PlayerInfo[ playerid ][ 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 pass[ 256 ]; // Create array to get player pass
format( file, sizeof( file ), "Users/%s.ini", GetName(playerid)); // Get player path to fil
format( pass, sizeof( pass ), "%s", dini_Get(file, "Password")); // array 'pass' will contain info about Password from player file
//new lenght = strlen(inputtext);
if(strcmp(inputtext, pass) == 0) // If inputtext = Player Password from file - setup players pInfo stats
{
PlayerInfo[ playerid ][ pAdmin ] = dini_Int( file, "Admin");
PlayerInfo[ playerid ][ pPack ] = dini_Int( file, "Pack");
PlayerInfo[ playerid ][ pSlots ] = dini_Int( file, "Slots");
PlayerInfo[ playerid ][ pHungry ] = dini_Int( file, "Hungry");
PlayerInfo[ playerid ][ pThirsty ] = dini_Int( file, "Thirsty");
for(new i=0;i<INVNUMBERS;i++) // For every INVNUMBERS slots get info about inventory
{
new idstr[256]/*, ammostr[128]*/;
format( file, sizeof( file ), "Users/%s.ini", GetName(playerid)); // Get file path
format(idstr, sizeof(idstr), "id%d", i);
PlayerInv[playerid][i] = dini_Int( file, idstr ); //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(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,"Log In","Incorrect pass!!!","Enter","Quit");
}
return 1;
}
}
}
public OnPlayerDisconnect - when player disconnect
PHP Code:
format(file, sizeof(file), "Users/%s.ini", GetName(playerid)); // Get file path
if( dini_Exists( file ) ) // If player file exist write down his new stats to file
{
dini_IntSet( file, "Admin", PlayerInfo[ playerid ][ pAdmin ]);
dini_IntSet( file, "Pack", PlayerInfo[playerid][pPack]);
dini_IntSet( file, "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(idstr, sizeof(idstr), "id%d",i); // For example id1 - it's 'Dry Food', id2 - 'Bean Can'
dini_IntSet( file, idstr, PlayerInv[playerid][i]); // Write down it to every slots
}
}
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;
}
PHP Code:
stock Equipment(playerid) // Loot point
{
if(GetPlayerState(playerid) != PLAYER_STATE_ONFOOT) return 1;
new f = total_vehicles_from_files+1;
new str[1024], listitems[1024], adm=0;
for(new a = 0; a < total_vehicles_from_files; a++) // total_vehicles_from_files it's our object loaded from file
{
if(IsPlayerInRangeOfPoint(playerid, 1.8, dInvData[a][ObjPos][0], dInvData[a][ObjPos][1], dInvData[a][ObjPos][2])) // If is player near the loot
{
f = a;
PlayerAdminMassage[playerid][adm] = f; // Get info
adm ++;
format(str, sizeof(str), "%s\n", InvNames[dInvData[f][ObjData]]); // Get Names of object
if(dInvData[f][ObjData] > 0) strins(listitems, str, strlen(listitems), strlen(str)); // And put it for dialog TakeInv
}
}
if(f > 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] > 0 && slot != 0) // If player inventory is not empty
{
PlayerAdminMassage[playerid][adm] = slot;
adm ++;
format(str, sizeof(str), "[%d]\t%s\n", PlayerInv[playerid][slot],InvNames[slot]); // Get info amount - name (For instance [1] - Bean Can)
strins(listitems, str, strlen(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] == 1) format(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(id, Float:gPosX, Float:gPosY, Float:gPosZ) // When player drop items from his inventory
{
if(id == 0) return 1;
new f = total_vehicles_from_files+1;
for(new a = 0; a < total_vehicles_from_files; a++)
{
if(dInvData[a][ObjPos][0] == 0.0) // Object pos 0.0 will be use for respawning loot
{
f = a;
break;
}
}
if(f > 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_l, dInvData[f][ObjPos][1]+y_l, dInvData[f][ObjPos][2]-1.0, 0.0, 360.0, 250.0);
if(id == 78) SetDynamicObjectMaterial(dInvData[f][ObjID], 0, 2060, "cj_ammo", "CJ_CANVAS2", 0);
//printf("%d,%f",id,dInvData[f][ObjPos][0]);
return 1;
}
stock RemovePlayerWeapon(playerid, weaponid)
{
if(!IsPlayerConnected(playerid) || weaponid < 0 || weaponid > 50)
return;
new saveweapon[13], saveammo[13];
for(new slot = 0; slot < 13; slot++)
GetPlayerWeaponData(playerid, slot, saveweapon[slot], saveammo[slot]);
ServerResetPlayerWeapons(playerid);
for(new slot; slot < 13; slot++)
{
if(saveweapon[slot] == weaponid || saveammo[slot] == 0)
continue;
ServerGivePlayerWeapon(playerid, saveweapon[slot], saveammo[slot]);
}
ServerGivePlayerWeapon(playerid, 0, 1);
}
//
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(playerid, health+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;
}
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(playerid, COLOR_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_sub, InfoSub[playermassagetoadmin[playerid]][iD_sub], 0, strlen(InfoSub[playermassagetoadmin[playerid]][iD_sub]), 255);
strmid(Desc_sub, InfoSub[playermassagetoadmin[playerid]][iD_text], 0, strlen(InfoSub[playermassagetoadmin[playerid]][iD_text]), 512);
format(nazvanie_one, sizeof(nazvanie_one),"%s", Name_sub);
format(string, sizeof(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_one, sizeof(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(playerid, 0xA9A9A9FF, "* Sorry, sbs take this object");
new buffer[120];
format(buffer, sizeof(buffer), "* %s now in your backpack.", InvNames[dInvData[playermassagetoadmin[playerid]][ObjData]]);
if(PlayerInfo[playerid][pSlots] >= PlayerInfo[playerid][pPack]*10) return SendClientMessage(playerid, 0xA9A9A9FF, "* Your backpack is
full, drop something.");
if(PlayerInv[playerid][dInvData[playermassagetoadmin[playerid]][ObjData]] > 0) PlayerInv[playerid][dInvData[playermassagetoadmin
[playerid]][ObjData]] +=1, PlayerInfo[playerid][pSlots] ++;
else PlayerInv[playerid][dInvData[playermassagetoadmin[playerid]][ObjData]] +=1, PlayerInfo[playerid][pSlots] ++;
SendClientMessage(playerid, 0xA9A9A9FF, buffer);
}
//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
PHP Code:
SetTimer("ProcessAllPlayers",1000,1); // Timer with repeating every seconds
PHP Code:
public ProcessPlayerHunger(playerid)
{
PlayerInfo[ playerid ][ pHungry ]--; // every second -1 hunger stat
if(PlayerInfo[ playerid ][ pHungry ] <= 0)// If player hunger stat <= 10 - kill player
{
SetPlayerHealth(playerid, -10);
}
return 1;
}
public ProcessPlayerThirsty(playerid)
{
PlayerInfo[ playerid ][ pThirsty ]--; // every second -1 thirst stat
if(PlayerInfo[ playerid ][ pThirsty ] <= -10) // If player thirst stat <= 10 - kill player
{
SetPlayerHealth(playerid, -10);
}
return 1;
}
public ProcessAllPlayers()
{
for (new i = 0; i < MAX_PLAYERS; i++)
{
if (PlayerInfo[ i ][ pLogged ] == true)
{
ProcessPlayerHunger(i);
ProcessPlayerThirsty(i);
}
}
return 1;
}