Define custom weapon names -
SiraBots - 01.06.2015
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.
Re: Define custom weapon names -
Konstantinos - 01.06.2015
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; i != sizeof (InvItems); ++i)
{
if(Player[playerid][pWeapon] == InvItems[i][InvItem])
{
strcat((string[0] = EOS, string), InvItems[i][InvName], sizeof (string));
break;
}
}
return string;
}
It will return the name of it if it exists, else N/A.
Re: Define custom weapon names -
SiraBots - 01.06.2015
Quote:
Originally Posted by Konstantinos
-snip-
|
I'll try it out and see what happens.
Re: Define custom weapon names -
Konstantinos - 01.06.2015
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".
Re: Define custom weapon names -
SiraBots - 01.06.2015
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?
Re: Define custom weapon names -
Konstantinos - 01.06.2015
Yes, it returns strings.
An example:
PHP код:
CMD:gunname(playerid)
{
new string[50] = "Your weapon name: ";
strcat(string, DefineGuns(playerid), sizeof (string));
SendClientMessage(playerid, -1, string);
return 1;
}
Re: Define custom weapon names -
SiraBots - 01.06.2015
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?
Re: Define custom weapon names -
Konstantinos - 01.06.2015
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.
Re: Define custom weapon names -
SiraBots - 01.06.2015
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?
Re: Define custom weapon names -
Konstantinos - 01.06.2015
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).