Game Development Community

Item creation troubles, and mounting woes

by Charlie Higdon · in Torque Game Engine · 05/28/2008 (8:16 am) · 20 replies

I was basically trying to make an object I could pick up and add to inventory, then mount it to things in the game that I had modeled.

I built item1 with a mountpoint, and item 2 with a mount.

I then took the health.cs file and tried to experiment with that. I didn't use crossbow because I hadn't planned on the items in question doing any damage or being in any way like a weapon.








// Inventory items. These objects rely on the item & inventory support
// system defined in item.cs and inventory.cs

//-----------------------------------------------------------------------------
// Trying to make a file to attach one item from inventory to another
// found in game.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Audio profiles
//-----------------------------------------------------------------------------
datablock AudioProfile(HealthUseSound)
{
filename = "~/data/sound/item1.wav";
description = AudioClose3d;
preload = true;
};
//------------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//
// item1.cs
//
//-----------------------------------------------------------------------------

datablock ItemData(Item1)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.
category = "Reinforcement";

// Basic Item properties
shapeFile = "~/data/shapes/items/item1.dts";
mass = 1;
friction = 1;
elasticity = 0.3;
emap = true;

// Dynamic properties defined by the scripts
pickupName = "item 1";

};

function Item1::onUse(%this,%user)
{
// attach item 1 to item 2 when used

%item1.mountObject(%item2,0);
%user.decInventory(%this,1);
}



Most times the Category isn't showing up in world editor under shapes like it should. I can find it under the other shapes category however. If I spawn Item1 I am unable to pick it up.

If Item1 has a handle of 2340 (random number) and I try something like %2340.mountObject (%2639,0);

Nothing happens.


I am adding a line to player.cs for maxinv, i am adding an exec to game.cs, and I just copied the
moveMap.bindCmd(keyboard, "h", "commandToServer('use',\"HealthKit\");", ""); from defaultbind.cs and changed to moveMap.bindCmd(keyboard, "l", "commandToServer('use',\"item1\");", "");

Any guidance at all would help loads. I feel like a moron, I've made all these nice models, have all my ideas laid out, got 1 or 2 to work, but those were only doors and windows that open and close, and that can break.
I know I'm probably missing alot of code, and I really don't want someone to write it for me, but I've been messing around for a week and can't seem to come across any good item creation and item inventorying tutorials. Some pointers, advice, anything would be appreciated.

I have the 3DGPAI1 books, the Multiplayer Coding, and Game Programmer's guide, but neither have much information on my issue.

#1
05/28/2008 (8:23 am)
Did you make sure to exec() your script containing your item datablock? If it's not appearing in the proper category in the editor that's usually a symptom of overlooking that.
#2
05/28/2008 (8:35 am)
Do you mean a line like

exec("./item1.cs");

in the game.cs folder?
#3
05/28/2008 (8:48 am)
Yes, that's what he's talking about. Also, your onUse function doesn't look correct because it is referencing the variable %item2 which is never defined.
#4
05/28/2008 (8:58 am)
Yes i have the exec line in game.cs.

I was unsure about the %item2.

I didn't know whether to give it a datablock since it was already a shape in the same. Basically just a door I sat up in the middle of no where that has a mount node. No reason in particular I chose a door, other than it was quick to model.

I wanted to take item1, which is a dartboard and mount it to the door. I was just using these 2 to get an understanding of how to mount and such. Realy quick to model it, small cylinder with a little dartboard jpg.

I spawn the door near where I spawn in game. I saved the mission. Then I went back and tried the item1.cs file creation. I wanted to be able to pick up item1 and stick to item2 (the door).


I'll try adding


datablock ItemData(Item2)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.
category = "Door";

// Basic Item properties
shapeFile = "~/data/shapes/items/item2.dts";
mass = 1;
friction = 1;
elasticity = 0.3;
emap = true;

and see if perhaps that helps. I was unsure whether to add it because it wasn't an item I wanted to pick up, I'm still unable to actually pick up item1. Could it be because I gave it a collision mesh?

Thank you for being patient with me.
#5
05/28/2008 (9:22 am)
Well, first of all, let's get your datablock showing up in the editor as that problem can definitely be a symptom other larger issues. Did you put your exec line with all of the other exec lines in game.cs? Are there any error messages in your console.log file?
#6
05/28/2008 (9:40 am)
Yes, I put it right before the, exec("./environment.cs");

Going to be a minute or 2, then I will open it up and check for any error.
#7
05/28/2008 (10:20 am)
%item2 and %item1 aren't being defined before you try to use it.
datablock ItemData(Item1) and %item1 are not the same thing.

You need a reference to the item before you can use it.

something like.
Function foo::oncollison(%this,%obj,%col)
{
.....

%obj.item2 = %col;
.....
)

This places a reference to the collided object,%col, into %obj.item2 that can be later accessed with your function.


Also %this in function Item1::onUse(%this,%user) is actually the item1 datablock.
#8
05/28/2008 (10:22 am)
Okay it's showing up now under shapes AND static shapes,before it was only under static shapes. I changed the key to USE the item from L to 7 on the movemap. The item2 is showing up as well. There are no console errors. I am able to pick up item1 as intended now. I am unsure what changing the movemap would have done.

There is no console error when loading.

When I pick up item 1, I receive "Item 1 picked up."

When I press 7, to mount the item1 to item2, i get

creator/editor/EditorGUI.cs (878) unable to find object, attempting to call function 'open'
starter.fps/server/scripts/item1.cs (49): Unable to find object, attempting to call function 'mountObject'

I've tried spawning it as a shape and a static shape,neither worked.


This is what I have atm.
**************************************************************

//------------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//
// item1.cs
//
//-----------------------------------------------------------------------------

datablock ItemData(Item1)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.
category = "Reinforcement";

// Basic Item properties
shapeFile = "~/data/shapes/items/item1.dts";
mass = 1;
friction = 1;
elasticity = 0.3;
emap = true;

// Dynamic properties defined by the scripts
pickupName = "item 1";
maxInventory = 1; // No pickup or throw

};

datablock ItemData(Item2)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.
category = "Door";

// Basic Item properties
shapeFile = "~/data/shapes/items/item2.dts";
mass = 1;
friction = 1;
elasticity = 0.3;
emap = true;



};

function Item1::onUse(%this,%user)
{
// attach item 1 to item 2 when used

%item1.mountObject(%item2,0);
%user.decInventory(%this,1);
}
#9
05/28/2008 (10:52 am)
In torque script variables prefixed with % , like %item1 , are only valid inside the function that defined it.

I bet your getting an error in the console.log about %item1 in your Item1::onUse function.
As far as the program knows %item1 is a variable and not an object like you are trying to use it.
#10
05/28/2008 (11:39 am)
Tried changing % to $ with no luck
#11
05/28/2008 (12:24 pm)
Ok, if you've got it showing up in the world that's a good first step. Now the problem that you have is that you need to get a reference to that Door object that you want to mount the item to. The fundamental concept here is that there is a difference between a datablock and an object that uses that datablock. There is only ever 1 instance of any particular datablock in your entire game at a time, but there can be many, many objects that use that datablock.

So, if you have a door in your world that uses the datablock item2 you someone need to get a reference to that particular object to mount. This question comes up repeatedly in the forums so it's an important concept to grasp fully. I posted an answer to someone's question about how to reference an object in Script in this thread, so you should check that out.

As to your specific case, you're just trying to do a little test here so this is what I would recommend. Open up your Mission Editor and select that door object you want to mount the item onto. Give it a name in the Name field (right beside the Apply button), like "TestDoor," click Apply, and save your mission. Now, when you Name an object, that object is accessible from anywhere within Torque Script by using that name. In your case, inside of your onUse function your code would change to look like this:
function Item1::onUse(%this, %user)
{
   // attach item 1 to item 2 when used

   %item1.mountObject(TestDoor,0);
   %user.decInventory(%this,1);
}

Now, that is the proper syntax, but there might also be a fundamental problem with your test right from the beginning. In order for one object to mount another, the object being mounted to needs a "node" called "mount#" (where # is some number: 0, 1, 2). In your case, if your door object doesn't have a "mount0" node then your code is going to fail even though it has the correct syntax. But I hope this experiment helped you learn some of the principles behind Torque Script.
#12
05/28/2008 (1:41 pm)
Spawned and renamed item2(door) in game as TestDoor.

Saved mission and exited.

Changed %item1.mountObject(item2,0); to %item1.mountObject(TestDoor,0);

Deleted .dso files.

Launched game, was still getting, starter.fps/server/scripts/item1.cs (49):unable to find object, attempting Function mountObject.

Spawned an item1, renamed it dartboard.

Saved mission and exited.

Changed %item1.mountObject(TestDoor,0); to dartboard.mountObject(TestDoor,0);

no more , starter.fps/server/scripts/item1.cs (49):unable to find object, attempting Function mountObject.
mappingstring

I now get:

mappingstring:msgitempickup to index: 13
mappingstring:you picked up %1 to index: 14
mappingstring: use to index: 3

but no mounting occurs. I hate being such a pest.
-----------------------------------------------------------------------------------------
Item2 schematic

bounds

base
|--------Collision-1
|--------Detail02
|
|--------Start01
----------|-------Door
--------------------|------Col-1
--------------------|------mount0

-----------------------------------------------------------------------------------------------------------

Item 1 Schematic



bounds

base
|--------Collision-1
|--------Detail02
|
|--------Start01
-------------|-------dartboard
----------------------------|------Col-1
----------------------------|------mountPoint
----------------------------------------------------------------------------------------------

Anything wrong with schematic maybe?
#13
05/29/2008 (10:16 am)
The item1 is able to be picked up, and it capping itself at a max of 1 like i wanted, pressing the use key does take it from my inventory and allow me to pick up another.

least i got somethin right =(
#14
05/29/2008 (10:22 am)
Ahh, I think I've figured it out. When you pick up the object is it gone from the world? You'll need to create a new instance of it (from inventory into the world) in order to mount it to another object.
function Item1::onUse(%this, %user)
{
   // attach item 1 to item 2 when used
   %item1 = new Item() {
      position = "0 0 0"; // this is just really temporary as it will be move right away
      dataBlock = %this;
   };
   %item1.mountObject(TestDoor,0);
   %user.decInventory(%this,1);
}
#15
05/29/2008 (11:16 am)
Yes, it indeed disappears from the world.

This is what I now have

//=================================================================
//------------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//
// item1.cs
//
//-----------------------------------------------------------------------------

datablock Itemdata(Item1)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.

category = "Reinforcement";

// Basic Item properties
shapeFile = "~/data/shapes/items/item1.dts";
mass = 1;
friction = 1;
elasticity = 0.3;
emap = true;


// Dynamic properties defined by the scripts
pickupName = "item to be mounted";
maxInventory = 1; // No pickup or throw

};

// -------------------------------------------------------------------------------------------------------------------------------
//-----------------------ITEM2, UNSURE IF DATABLOCK NEEDED IN THIS FILE---------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------

datablock Itemdata(Item2)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.

category = "Door";

// Basic Item properties
shapeFile = "~/data/shapes/items/item2.dts";
mass = 1;
friction = 1;
elasticity = 0.3;
emap = true;


// Dynamic properties defined by the scripts
// NONE AT THE MOMENT

};


//---------------------------------------------------------------------------------------------------------------------------
//-----------------------------------FUNCTION WHICH I HOPE WILL MOUNT ITEM1 TO ITEM2-----------------
//---------------------------------------------------------------------------------------------------------------------------
function Item1::onUse(%this, %user)
{

// attach item 1 to item 2 when used

%item1 = new Item() {
position = "0 0 0"; // this is just really temporary as it will be move right away
dataBlock = %this;
};
%item1.mountObject(TestDoor,0);
%user.decInventory(%this,1);
}

//----------------------------------------------------------------------------------------------------------------------

Item1 is still being picked up, and disappearing, and being cleared from inventory when I use it. But Testdoor just sits there all by it's lonesome, nothing mounting still. No errors however.

When I bring up world creator, and spawn the shape item1, should I be spawning item2(testdoor) as a staticshape or an item. I've got it as a staticshape atm, I was told in chat I couldn't mount an item to an item. I don't care if item1 has collision or not, but it is important to my game that item2 does. It's meant to bar the way, item1 will eventually be a barricade of sorts. I was just using it because I already had it modeled and didn't have the barricade yet.

I am spawning item1 as a normal item in all it's rotating glory.
#16
05/29/2008 (11:42 am)
I changed

datablock Itemdata(Item2)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.

category = "Door";

// Basic Item properties
shapeFile = "~/data/shapes/items/item2.dts";
mass = 1;
friction = 1;
elasticity = 0.3;
emap = true;




to




datablock StaticShapeData(Item2)
{
// Mission editor category, this datablock will show up in the
// specified category under the "shapes" root category.

category = "Door";

// Basic Item properties
shapeFile = "~/data/shapes/items/item2.dts";
mass = 1;
friction = 1;
elasticity = 0.3;
emap = true;



and it worked. I pick up item1, press 7, and item 2 disappeared. Instead of item 1 going to 0,0,0 and being mounted when used at the location of item2(testdoor), item1 goes to 0,0,0 and brings item2(testdoor) to it and mounts.

BUT HEEEEYY it's working. Thanks for all the help!!!!

I'll keep messin with it.
#17
05/29/2008 (11:50 am)
Well poo, i was wrong about it mounting. it actually moved both items to 0,0,0 without mounting them.


*CRIES*

thought I had it.

I can even pick up item1 after both are moved.
#18
05/29/2008 (12:24 pm)
Try reversing the mountObject call:
TestDoor.mountObject(%item1, 0);
#19
05/29/2008 (1:12 pm)
Doing that resulted in a staticshape of item1 being spawned that the location and dropped onto another item1 that can be picked up.

I changed the location to near the spawn point, so it'd be easier to see what happens.

Pressing "7" causes the staticshape TestDoor to POOF to where item1 was set to appear (0 0 0 now set as 320, 320, 330).

Both appear about 10 feet off the ground then fall. I can pick them up individually and stack them with the world creator, instead of one object being picked up.
#20
05/30/2008 (12:30 pm)
Heck, i give up on that. guess i'm gonna look for some way to replace the existing door with the one with the object already modeled on it when i use the item