Game Development Community

checking a players inventory for a weapon and equiping it

by Joseph Bosch · in Game Design and Creative Issues · 04/12/2010 (10:57 am) · 8 replies

So I am starting to build my inventory system, the system is so far very simple, press the right key equip the weapon.

My key press function works and successfully tells the server that the player would like to change their weapon. The problem I have having is checking that particular player’s inventory to determine if he actually has that weapon.
Currently I have

function ServerCmduse(%client, %weaponEnum)
{
echo("entering use function");
echo ("attempting to set weapon");
if (client.getInventory("Crossbow") > 0)
%client.getControlObject().use($weaponBindkey1);
}

Currently the weapon is bound to $weaponBindkey1 when I pick it up, so I basically need a way to check a players inventory for a weapon (preferably by the weapons name or enum value that I define in its datablock) and then equip that weapon.

#1
04/12/2010 (11:23 am)
I don't think you need "" around the weapon name. Also % missing from first use of client. Also (for reasons I don't know) I've never liked >0 and prefer !=0 or >=1

Check your console for errors.

And off the top of my head ...

//key for below snippet: %obj is the object - %client.player is the player

%datatype = %obj.getdatablock().getname();
//to get the object's name

if(%datatype $= "crossbow" && %client.player.getinventory(crossbow) < 1)
//check if the player has one or not
   %client.player.setinventory(crossbow, 1);
//if not, give it to the player's inventory so they can use it
#2
04/12/2010 (12:08 pm)
good eye the % is there it just didn't make it over on the copy and paste, the only problem is that the weapon its self is not passed just an enum value that tells us which weapon to equip (1 for xbow, 2 for bow, ect.

if it helps the item is defined as such
datablock ItemData(Crossbow)
{

category = "Weapon";

className = "Weapon";

shapeFile = "~/data/models/weapons/crossbow.dts";
mass = 1;
elasticity = 0.2;
friction = 0.6;
emap = true;
pickUpName = "a crossbow";
image = CrossbowImage;
};

#3
04/12/2010 (2:01 pm)
If you want to check for something in the inventory, then you're going to have to use the weapon's name at some point because that's what it's stored in the inventory as - even if it's just to include the enum in the itemdata to reference against.

Also, give a getName/getID echo for %client.getControlObject() and check that it's returning something - preferably the player object.
#4
04/12/2010 (3:34 pm)
%client.getControlObject works fine, and it mounts the weapon successfully it is just the if statement before hand that actually checks to see if the weapon is in the inventory that seems to be the problem.

The weaponEum is not actually used at the moment, I apologize if this added to the confusion, it is merely designed as structure for latter when multiple weapons are added.

I was thinking I maybe putting to much thought into this I could just create a new crossbow object locally IE

if (%client.player.getinventory(crossbow) != 0)
{
%temp = new item(){
datablock = "Crossbow";
};
%client.getControlObject().use(%temp);
}


still this feels like a bit of a hack though a little of tweaking in other areas could compensate, I will test things out and post code latter if it all works
#5
04/12/2010 (8:16 pm)
ok you were correct with the "" being the problem, i used them because that was the code example in another persons code example, just one last thing to figure out before I post full code
#6
04/12/2010 (8:31 pm)
hmnn the last thing I appear to be having trouble with is getting the weapon to set to something usable like

%x = crossbow;
#7
04/12/2010 (8:34 pm)
... hey that actually works :P

here is what you do

function ServerCmduse(%client, %clientId)
{
echo("entering use function");
if (%client.player.getinventory(Crossbow) != 0)
{
%temp = Crossbow;
echo ("attempting to set weapon");
echo (%temp.getName() @ %temp.getID());
%client.getControlObject().use(%temp);
}
}
#8
04/12/2010 (8:37 pm)
... yawwwn ... beddy-byes in a sec ... try it as a "string" like I used %datatype above:
%x $="crossbow";
//it's a word not numbers - then you need the "" cos it ain't in brackets