Define custom weapon names
#1

Hey there.
I'm working on a new weapon system at the moment, where I'm using custom weapon names for each weapon. So I for example use Desert Eagle three times with three different weapon names and weapon damages.

Issue is, how can I define the custom names and item IDs?
This is something I made really quick, but I assume it won't work:

Код:
enum InventoryItems
{
	InvName[32],
	InvItem
};

static const InvItems[][InventoryItems] = 
{
	// Desert Eagle (24)
	{"Beretta 92", 1000},
	{"SIG P226", 1001},
	{"Walther P5", 1002},
	
	// M4 (31)
	{"M4A1", 1003},
	{"Barrett REC7", 1004},
	
	// MP5 (29)
	{"AMP-69", 1005},
	{"MAS-38", 1006}
};

stock DefineGuns(playerid)
{
	if(Player[playerid][pWeapon] == "1001")
	{
		new string[128];
		format(string, sizeof(string), "Your weapon name: %s", InvItems[0001]);
		SendClientMessage(playerid, COLOR_WHITE, string);
	}
}
Would this work? If not, how can I make a weapon system like this?
Please help me out with this, and I'll do anything in return.

Thanks a lot in advance.
Reply
#2

As you can add more data to the array, comparing one by one would be bad so a loop is just what you need.

PHP код:
DefineGuns(playerid)
{
    new 
string[32] = "N/A";
    for (new 
i!= sizeof (InvItems); ++i)
    {
        if(
Player[playerid][pWeapon] == InvItems[i][InvItem])
        {
            
strcat((string[0] = EOSstring), InvItems[i][InvName], sizeof (string));
            break;
        }
    }
    return 
string;

It will return the name of it if it exists, else N/A.
Reply
#3

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
-snip-
I'll try it out and see what happens.
Reply
#4

Don't use "," at the last one in the array and those zeros won't work "000x", it will just check if it's "x".
Reply
#5

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Don't use "," at the last one in the array and those zeros won't work "000x", it will just check if it's "x".
Alright, I have updated the code. Butn how do I display the weapon name? Your function returns string, right?

Let's say I have to display the weapon name in a command.
Код:
CMD:gunname(playerid)
{

}
How do I get the name of the gun in here?
Reply
#6

Yes, it returns strings.

An example:
PHP код:
CMD:gunname(playerid)
{
    new 
string[50] = "Your weapon name: ";
    
strcat(stringDefineGuns(playerid), sizeof (string));
    
SendClientMessage(playerid, -1string);
    return 
1;

Reply
#7

All right. That works just fine, thanks a lot for your assistance.

But I'll take it a bit further. Let's say I load the inventory items from the database. How would that be?
Here is my "samp_inventory" table:


And here is my code:
Код:
enum InventoryItems
{
	ObjectName[32],
	ObjectModel
};

static const Inventory[][InventoryItems] = 
{
	// Desert Eagle (24)
	{"Beretta 92", 1000},
	{"SIG P226", 1001},
	{"Walther P5", 1002},
	
	// M4 (31)
	{"M4A1", 1003},
	{"Barrett REC7", 1004},
	
	// MP5 (29)
	{"AMP-69", 1005},
	{"MAS-38", 1006}
};

stock DefineGuns(playerid) 
{
	new string[128];
	for(new i; i != sizeof(Inventory); ++i) 
	{
		if(Player[playerid][pTest] == Inventory[i][ObjectModel])
		{
			format(string, sizeof(string), "%s", Inventory[i][ObjectName]);
			break; // Do I need this?
		}
	}
	return string; 
}

CMD:gunname(playerid)
{
	new string[50];
	format(string, sizeof(string), "Your weapon is: %s", DefineGuns(playerid));
	SendClientMessage(playerid, -1, string);
        return 1;
}
Do I need to make a loop? Or how does this work?
Reply
#8

By loading you mean the inventory of a player, right? If so, don't change anything to the above.

When a player connects, you need to execute a query that selects the items and the amount for the player with that user ID. Then load them and assign them to some global (per-player) variables.

PS: "break;" stops the loop. If the model was found and string now stores the name of it, no need to keep looping.
Reply
#9

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
By loading you mean the inventory of a player, right? If so, don't change anything to the above.

When a player connects, you need to execute a query that selects the items and the amount for the player with that user ID. Then load them and assign them to some global (per-player) variables.

PS: "break;" stops the loop. If the model was found and string now stores the name of it, no need to keep looping.
So let's say I have three guns in my inventory. Gun 1001, 1004 and 1002 (image). How do I load these guns to one enum?
I guess I have to make a loop, but how would it look?

Before we did: 1 gun (1001) --> stored in Player[playerid][pTest]
Now we do: 3 guns (1001, 1004, 1002) --> stored in what?
Reply
#10

pTest should be an array with the same size as Inventory. For example:
pawn Код:
// enum and array Inventory here..

enum e_Player
{
    pTest[sizeof (InvItems)]
};

new Player[MAX_PLAYERS][e_Player];
Then you execute the query, you get the rows and loop until the iterator variable is less or not equal to rows. Get the data for that rowid (rowid = iterator variable) and assign them to the variable:
pawn Код:
for (new i; i != rows; ++i)
{
    if (i >= sizeof (Inventory)) break; // just in case to prevent run time error

    Player[playerid][pTest][i] = cache_get_*_int(i, ...);
}
cache_get_*_int -> for cache_get_row_int or cache_get_field_content_int (R33+ functions, I don't know how to use R5 or R6).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)