New to scripting, inventory help please :)
by Brandon Coleman · in Torque Game Engine · 02/11/2010 (6:59 pm) · 49 replies
Hi guys :) I'm somewhat new to scripting so I'm a little confused as to what to do next.. I have a group project for a level designing class that I'm working on, and my job is to get a gas can object to add into the inventory. As I understand, there is an inventory system already built into Torque, so that one will work fine. How would I go about doing this? This is what I have so far:
Thanks for any help :)
//-----------------------------------------------------------------------------
//Gas Can Code
//-----------------------------------------------------------------------------
datablock ItemData( GasCan )
{
category = "Gas Can";
shapeFile = "~/data/shapes/GasCan/GasCan.dts";
respawnTime = 100000;
// Dynamic properties defined by the scripts
maxInventory = 1; // No pickup or throw
};
//-----------------------------------------------------------------------------
//Here is the function to call the gas can into the game
//-----------------------------------------------------------------------------
function ItemData::create( %data )
{
echo( "ItemData::create for GasCan called --------------------------" );
%obj = new Item()
{
dataBlock = %data;
rotate = false; // Gas Can will not rotate
static = true; // Gas Can will not move
};
return %obj;
}
function ItemData::onPickup(%this,%obj,%user,%amount)
{
// Add it to the inventory, this currently ignores the request
// amount, you get what you get. If the object doesn't have
// a count or the datablock doesn't have maxIventory set, the
// object cannot be picked up.
%count = %obj.count;
if (%count $= "")
if (%this.maxInventory !$= "") {
if (!(%count = %this.maxInventory))
return;
}
else
%count = 1;
%user.incInventory(%this,%count);
// Inform the client what they got.
if (%user.client)
messageClient(%user.client, 'MsgItemPickup', 'c0You picked up %1', %this.pickupName);
// If the item is a static respawn item, then go ahead and
// respawn it, otherwise remove it from the world.
// Anything not taken up by inventory is lost.
if (%obj.isStatic())
%obj.respawn();
else
%obj.delete();
return true;
}Thanks for any help :)
About the author
#2
02/11/2010 (8:38 pm)
Oh alright, sorry.. I thought it was more appropriate here.
#3
There's actually quite a bit wrong with your script as it is, so before we go any further let's establish what it needs to do first. Is it just to collide with the gas can in the world and then add it to your inventory?
Also, just so I know, are you familiar with C++ at all? I just ask that so I don't start using terms that may confuse you.
02/12/2010 (11:19 am)
It is, but I can move threads. Anyway, on to your script which I put inside a code block to make it easier to read.There's actually quite a bit wrong with your script as it is, so before we go any further let's establish what it needs to do first. Is it just to collide with the gas can in the world and then add it to your inventory?
Also, just so I know, are you familiar with C++ at all? I just ask that so I don't start using terms that may confuse you.
#4
02/12/2010 (12:25 pm)
I'm somewhat familiar with C++, not too great with it. I can understand some basic terms though. And yes, we just need it to collide with the gas can so it adds to your inventory.
#5
The simplest way to go about this would be to create your GasCan ItemData like you are currently and then override the functions you need any custom code beyond the stock.
For example:
The other thing you have to remember to do is to add your new item to inventory array in player.cs
Do that and it should all work just fine.
02/12/2010 (3:37 pm)
Cool. I take it back that there were things wrong with that script, as it looks like that's just the stock Item code. Is that within item.cs or is copy/pasted into a new file?The simplest way to go about this would be to create your GasCan ItemData like you are currently and then override the functions you need any custom code beyond the stock.
For example:
function GasCan::onPickup(%this, %obj, %shape, %amount)
{
// The parent Item method performs the actual pickup.
if (Parent::onPickup(%this, %obj, %shape, %amount))
{
//extra stuff goes here
}
}The other thing you have to remember to do is to add your new item to inventory array in player.cs
// Allowable Inventory Items maxInv[BulletAmmo] = 20; maxInv[HealthKit] = 1; maxInv[RifleAmmo] = 100; maxInv[CrossbowAmmo] = 50; maxInv[Crossbow] = 1; maxInv[Rifle] = 1; maxInv[GasCan] = number of max gas cans;
Do that and it should all work just fine.
#6
02/12/2010 (4:16 pm)
Oh alright awesome. Let me try that. And I'm pretty sure it's copy/pasted into a new file if I understand you correctly. I'm still trying to figure all this out lol.
#7
02/12/2010 (4:24 pm)
Alright cool, here's what I got.//-----------------------------------------------------------------------------
//Gas Can Code
//-----------------------------------------------------------------------------
datablock ItemData( GasCan )
{
category = "Gas Can";
shapeFile = "~/data/shapes/GasCan/GasCan.dts";
respawnTime = 100000;
// Dynamic properties defined by the scripts
maxInventory = 1; // No pickup or throw
};
//-----------------------------------------------------------------------------
//Here is the function to call the gas can into the game
//-----------------------------------------------------------------------------
function ItemData::create( %data )
{
echo( "ItemData::create for GasCan called --------------------------" );
%obj = new Item()
{
dataBlock = %data;
rotate = false; // Gas Can will not rotate
static = true; // Gas Can will not move
};
return %obj;
}
function GasCan::onPickup(%this, %obj, %shape, %amount)
{
// The parent Item method performs the actual pickup.
if (Parent::onPickup(%this, %obj, %shape, %amount))
{
//extra stuff goes here
if (%user.client)
messageClient(%user.client, 'MsgItemPickup', '\c0You picked up %1', %this.pickupName);
// If the item is a static respawn item, then go ahead and
// respawn it, otherwise remove it from the world.
// Anything not taken up by inventory is lost.
if (%obj.isStatic())
%obj.respawn();
else
%obj.delete();
return true;
}
}Does this look right?
#8
02/12/2010 (4:27 pm)
Go ahead and delete that copy/pasted code so that there isn't an issue with redefinition of existing methods. If you need the create for the GasCan for anything specific to that item you would need to do on creation then you'll need to make for just the GasCan. Ex. GasCan::create etc.
#9
Everything looks good except for the following changes:
So, there the changes were to make the create a method of the GasCan and what you added to the extra stuff section of the onPickup gets called by the Parent::onPickup already.
Since this is a new file you also have to remember to exec() it. Just do a find in files for where crossbow.cs is getting called and place it along with that bunch. Don't forget to add it to the maxInv array in player.cs as well.
With that you should be good to go.
02/12/2010 (4:35 pm)
Whoops, you got your last post in before I was done with mine.Everything looks good except for the following changes:
function GasCan::create( %data )
{
echo( "ItemData::create for GasCan called --------------------------" );
%obj = new Item()
{
dataBlock = %data;
rotate = false; // Gas Can will not rotate
static = true; // Gas Can will not move
};
return %obj;
}
function GasCan::onPickup(%this, %obj, %shape, %amount)
{
// The parent Item method performs the actual pickup.
if (Parent::onPickup(%this, %obj, %shape, %amount))
{
//extra stuff goes here
return true;
}
}So, there the changes were to make the create a method of the GasCan and what you added to the extra stuff section of the onPickup gets called by the Parent::onPickup already.
Since this is a new file you also have to remember to exec() it. Just do a find in files for where crossbow.cs is getting called and place it along with that bunch. Don't forget to add it to the maxInv array in player.cs as well.
With that you should be good to go.
#10
Also, I looked at player.cs and there was no maxInv array in it. I added it myself but it just gave me an error. Here's a copy of my player.cs.
02/12/2010 (10:36 pm)
Thanks for the response, I just replaced everything I had before with what you gave me. That should work, right?Also, I looked at player.cs and there was no maxInv array in it. I added it myself but it just gave me an error. Here's a copy of my player.cs.
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
// Load dts shapes and merge animations
datablock TSShapeConstructor(PlayerDts)
{
baseShape = "~/data/shapes/player/player.dts";
sequence0 = "~/data/shapes/player/player_root.dsq root";
sequence1 = "~/data/shapes/player/player_forward.dsq run";
sequence2 = "~/data/shapes/player/player_back.dsq back";
sequence3 = "~/data/shapes/player/player_side.dsq side";
sequence4 = "~/data/shapes/player/player_fall.dsq fall";
sequence5 = "~/data/shapes/player/player_land.dsq land";
sequence6 = "~/data/shapes/player/player_jump.dsq jump";
sequence7 = "~/data/shapes/player/player_standjump.dsq standjump";
sequence8 = "~/data/shapes/player/player_lookde.dsq look";
sequence9 = "~/data/shapes/player/player_head.dsq head";
sequence10 = "~/data/shapes/player/player_headside.dsq headside";
sequence11 = "~/data/shapes/player/player_celwave.dsq celwave";
};
datablock PlayerData(PlayerBody)
{
renderFirstPerson = false;
shapeFile = "~/data/shapes/player/player.dts";
};
{
// Allowable Inventory Items
maxInv[BulletAmmo] = 20;
maxInv[HealthKit] = 1;
maxInv[RifleAmmo] = 100;
maxInv[CrossbowAmmo] = 50;
maxInv[Crossbow] = 1;
maxInv[Rifle] = 1;
maxInv[GasCan] = 1;
};
//----------------------------------------------------------------------------
// PlayerBody Datablock methods
//----------------------------------------------------------------------------
function PlayerBody::onAdd(%this,%obj)
{
// Called when the PlayerData datablock is first 'read' by the engine (executable)
}
function PlayerBody::onRemove(%this, %obj)
{
if (%obj.client.player == %obj)
%obj.client.player = 0;
}
function PlayerBody::onNewDataBlock(%this,%obj)
{
// Called when this PlayerData datablock is assigned to an object
}
#11
02/12/2010 (10:42 pm)
That's the wrong player.cs, there are two. One for TSShapeConstructor which sits where the art is and defines the model and it's animations, and one for everything else which is in the server folder. Your GasCan script needs to be in the server folder as well.
#12
Thanks :)
02/12/2010 (11:02 pm)
Wow, quick response. The GasCan script is already in the server folder, so I think that should be fine. I found the other player.cs in the demo/data/shapes/player folder, but it seems there isn't a player.cs in the same folder for our project. Should I just copy this player.cs over and add the inventory part?Thanks :)
#13
02/12/2010 (11:07 pm)
No, the one you want, and should be there already, is server/scripts/player.cs. The player.cs that is in the data/shapes/player folder is the one that holds the TSShapeConstructor.
#14
02/12/2010 (11:11 pm)
There isn't a server/scripts folder, but there are a lot of cs files in the server folder. That's the player.cs code I posted earlier, about 3 posts up.
#15
The maxInv stuff should be the last inside of the player's datablock, not sitting outside of which will cause errors.
02/13/2010 (2:13 am)
Is that what the school gave you or is that after your team made changes? That's not the typical layout. The maxInv stuff should be the last inside of the player's datablock, not sitting outside of which will cause errors.
#16
02/13/2010 (3:03 am)
I guess it's the team made changes, but I don't see it in the demo folder either (which is what we got from the school). I can put it at the bottom of the player's datablock, that would work right?
#17
02/13/2010 (3:56 pm)
It should work if you put it there. Go ahead and email me the player.cs that you have in the server folder. I can get a better idea of how you've got it set up that way.
#18
02/13/2010 (4:21 pm)
Done. Did you get the email?
#19
Assuming of course that there aren't other elements missing that my advice may be dependent on.
02/13/2010 (4:24 pm)
Yep, I was just looking it over. You guys are working with a seriously barebones build it looks like. Add the maxInv line for the GasCan after the shapefile line in the player datablock and that should do it.Assuming of course that there aren't other elements missing that my advice may be dependent on.
#20
I'll be home soon so I'll let you know if I get any errors. Thanks again :)
02/13/2010 (4:33 pm)
If it errors I'll let you know. We are working with a very barebones build, it's a pretty big project but it's mainly just so we know how to do it. It doesn't have to be intricate and extremely detailed. I'll be home soon so I'll let you know if I get any errors. Thanks again :)
Associate Scott Burns
GG Alumni
The others will be locked with a redirection to this one.