[HELP] Money System
#6

Quote:
Originally Posted by Doktor
Couldnt you just use defines for it?
Код:
// top
new
	pCash[MAX_PLAYERS];

#define GiveUserMoney(%1,%2) pCash[%1] += %2
#define GetUserMoney(%1) pCash[%1]
#define ResetUserMoney(%1) pCash[%1] = 0
Then you are able to use those functions

instead of "GivePlayerMoney(playerid, amount)", use "GiveUserMoney(playerid, amount)"
Instead of "GetPlayerMoney(playerid)", use "GetUserMoney(playerid)"
instead of "ResetPlayerMoney(playerid)" user "ResetUserMoney(playerid)"

So you could for example use
Код:
if(GetUserMoney(playerid) < 200) return SendClientMessage(playerid, COLOR, "You dont have the cash");
GiveUserMoney(playerid, -200); // Remove 200 from the cash of the player
The item System should be easy, it could for example look like ( just an example you need to change it for your needs)
Код:
// top
#define ITEM_NONE 0
#define ITEM_CONDOM 1
#define ITEM_DRUGS 2
#define ITEM_DRINKS 3
#define ITEM_PHONE 4
// more items here

#define MAX_ITEMS 5 // increase if you use more items
#define MAX_ITEMSLOT 3 // amount of items player can carry, remember you need to save each item
#define MAX_ITEMAMOUNT 20 // the max amount of an item a player can carry

new
	pItem[MAX_PLAYERS][MAX_ITEMSLOT],
	pItemAmount[MAX_PLAYERS][MAX_ITEMSLOT];


stock GiveUserItem(playerid, item, amount)
{
	/*
		returns 0 if NOT successfull
		returns 1 if successfull
	*/
	new
		slot = MAX_ITEMSLOT;
	for(new i = 0; i < MAX_ITEMSLOT; i++)
	{
		if(pItem[playerid][i] == ITEM_NONE || pItem[playerid][i] == item)
		{
			slot = i;
			break;	
		}
	}
	if(slot == MAX_ITEMSLOT) return 0;
	if((pItemAmount[playerid][slot]+amount) > MAX_ITEMAMOUNT) return 0;
	pItem[playerid][slot] = item;
	pItemAmount[playerid][slot] += amount;
	if(pItemAmount[playerid][slot] <= 0) pItem[playerid][slot] = ITEM_NONE;
	return 1;	
}

stock GetUserItem(playerid, item)
{
	/*
		returns the amount of an item
	*/
	for(new i = 0; i < MAX_ITEMSLOT; i++)
	{
		if(pItem[playerid][i] == item) return pItemAmount[playerid][i];			
	}
	return 0;
}

stock GetItem(item)
{
	new
		string[30];
	switch(item)
	{
		case ITEM_CONDOM: format(string, sizeof(string), "Condom");
		case ITEM_DRUGS: format(string, sizeof(string), "Drogen");
		case ITEM_DRINKS: format(string, sizeof(string), "Drinks");
		case ITEM_PHONE: format(string, sizeof(string), "Phone");
		// Add more items here
		default: format(string, sizeof(string), "Nothing");
	}
	return string;
}
So you can use different functions with it:
Quote:

GiveUserItem(playerid, item, amount): Give or Remove an item of a player ( returns 1 if successfull and 0 if not)
GetUserItem(playerid, item): Amount of the item in the bag ( returns the amount of the item in the bag of the player)
GetItem(item); returns the name of the item

So an basic exmaple could be a shop which sells Phones and Condoms:
Код:
// buy command
new
	item, 
	amount,
	string[80];

if(sscanf(params, "dd", item,amount)) // check the parameters with sscanf or strtok
{
	SendClientMessage(playerid, COLOR, "USAGE: /buy [item] [amount]");
	format(string, sizeof(string), "Available Items -> Phone:%d | Condom:%d ", ITEM_PHONE, ITEM_CONDOM);
	SendClientMessage(playerid, COLOR, string);
	return 1;
} 
if(amount <= 0) return SendClientMessage(playerid, COLOR, " Wrong amount");
if(item != ITEM_PHONE && item != ITEM_CONDOM) return SendClientMessage(playerid, COLOR, "You cant bought this here");

new
	bool:success = GiveUserItem(playerid, item, amount);
if(!success) return SendClientMessage(playerid, COLOR, "Check your bag, you cant put this item in the bag right now"); 

format(string, sizeof(string), "You bought %d %s", amount, GetItem(item));
SendClientMessage(playerid, COLOR, string);


// in the call command you could then check

if(GetUserItem(playerid, ITEM_PHONE) == 0) return SendClientMessage(playerid, COLOR, "You dont have a phone");
You could also do everything dynamic and save the stuff of the shop in other Arrays, so the things the shop sells are set in a database. Of course you need to save both the Player Items and the Shop Items in a Database and load them in order to create a Item System
This should be my last few questions regarding the money system.
How can I database it? (IS there a way to plug in the cash system to xtremeadmin2?)
How could i do a /inventory command to see what items i currently hold.
How do i make a player able to drop an item using /dropitem [slot]? (or item name - whichever is easier)
How do i make a player able to /use [item]? (I don't really need to learn to use the effects - maybe in future i will need to request how to get a specific object to work as i want)
Reply


Messages In This Thread
[HELP] Money System - by sacman - 28.11.2009, 06:59
Re: [HELP] Money System - by miokie - 28.11.2009, 11:41
Re: [HELP] Money System - by sacman - 02.12.2009, 05:04
Re: [HELP] Money System - by Extremo - 02.12.2009, 06:30
Re: [HELP] Money System - by Doktor - 02.12.2009, 08:01
Re: [HELP] Money System - by sacman - 02.12.2009, 09:00

Forum Jump:


Users browsing this thread: 1 Guest(s)