Game Development Community

Datablock confusion

by __._ · in Torque Game Engine · 10/03/2006 (3:01 am) · 17 replies

Hello everyone,

I'm confused by the use of datablocks. In my test-project I have the StaticShapeData block from the getting started guide. While tinkering with everything I'd like to make those objects dynamic (gravity for example) but it seems that no matter what I change "StaticShapeData" into, it won't work. If it doesn't read "StaticShapeData" the engine won't show a thing. Could someone find a bit of time to explain to me how I should be using them?

datablock StaticShapeData(TorqueLogoItem)
{
   category = "Items";
   shapeFile = "~/data/shapes/3dtorquelogo/torque_logo.dts";
};

#1
10/03/2006 (4:38 am)
Your datablock is a StaticShape datablock, and if you want to change it you will have to change the actual object too. You probably created it like this:

%object = new StaticShape ()
{
// bla bla
}

StaticShapes are good for (duh) static shapes, and gravity does not really make sense then.. does it? :) Needless to say, StaticShapes do not have support for gravity for the above reason and you will have to change to another object for that to work.

Player is such an example, or item. Item would be easier, as player requires alot of other stuff to work.
#2
10/03/2006 (5:11 am)
Makes sense, but I now have:

datablock itemData(TorqueLogoItem)
{
   category = "Items";
   shapeFile = "~/data/shapes/3dtorquelogo/torque_logo.dts";
};

...

%newobj = new item("Logo") {
	dataBlock = "TorqueLogoItem";
	position = %pos;
};

and still don't get to see any objects ;)
#3
10/03/2006 (9:49 am)
Are you making sure to add the new object, if you are expecting the current logo in your exaple to become a itemdata it won't. Make sure you add the item. Also make sure it is an item you are adding the the logo will show up in two places after hitting F11 then F4 on the mission builder screen. It will appear in staticshapes and Shapes (depending on how you have things set up. When I first started making items I keep grabbing them from the StaticShape area and was having a difficult time figuring out why nothing would happen. I hope that works for you.
#4
10/03/2006 (10:01 am)
It wont add the object (using Shapes). The console says "cant find itemData::create(...)"
#5
10/03/2006 (11:23 am)
The declaration of datablocks works like this:

datablock DatablockType ( <optional-name> )
{
  <variable-values>
};

So, when you want to create a StaticShapeData instance called itemData, you should call it like:

datablock StaticShapeData( itemData )
{
};

This is very different from C++ syntax! Especially the name going in the parentheses tripped me up when starting.

Also, "datablock" is more like specialized version of "new" than, say, the "class" or "typedef" keyword.
#6
10/03/2006 (11:28 am)
This is a problem with how you manage your functions, not the datablocks themselves.

*Where* are you excecuting this code? Client or server scripts?
If you do it inside itemData::create then you have a problem with the naming.
#7
10/03/2006 (12:05 pm)
Well my point is that it works with

datablock StaticShapeData(TorqueLogoItem)
{   
category = "Items";   
shapeFile = "~/data/shapes/3dtorquelogo/torque_logo.dts";
};

...

%newobj = new StaticShape("Logo") 
{	
dataBlock = "TorqueLogoItem";	
position = %pos;
};

Using that code will spawn the new objects (note that I HAVE to specify a name, or it won't spawn them). But I don't want to add static objects, I'd like to add objects with interaction. In an earlier post it was mentioned I should use "item" instead of "staticshape".
#8
10/03/2006 (12:15 pm)
I said it was an example, not that it was the way to go. (:

The code above is correct, so it should work. Try it on stock Torque because it looks like you have mangled item.cs or something, considering that the engine cannot find ItemData::create - which *should* be available unless you broke something.
#9
10/03/2006 (11:23 pm)
Ok now you have completely lost me.

Was it an example or should I really use ItemData? Either way it's not working. I used a fresh installation of Torque and the tutorial.base project. Added the logoitem.cs file with the following code:

datablock ItemData(TorqueLogoItem)
{   
	category = "Items";   
	shapeFile = "~/data/shapes/3dtorquelogo/torque_logo.dts";
};

function TorqueLogoItem::onCollision(%this, %obj, %col)
{ 
echo("collision");
	if(%col.getClassName() $= "Player")
	{
		%obj.delete();
		%logoCount = logos.getCount();
	
		%newobj = new Item("Logo") 
		{
			dataBlock = "TorqueLogoItem";	
			position = %col.position;
		};
	
		if(%logoCount > 0)
			return;

	}
}

The logo shows up though, but in the world editor it's still called a static shape (no I'm not using the static shape tree, but the shape tree).

Edit:

weird. A few minutes after posting this I run it again, without altering the above code, and it no longer works? The objects I placed are now gone (also can't place them again) and I get the error ItemData::Create doesn't exist again. My code still reads identical to the code above when it did work... partially. Changing it back to StaticShape makes it work again, so my code got cached or something? (Seeing how it still showed up as staticshape before).

Anyway, what am I doing wrong? Is ItemData just an example or should I use it? It's not making any sense to me so far.

Quote:
The code above is correct, so it should work
I know. I had that from the start, but it's not doing what I want. I want an object that can do something. Fall to the ground for example.

Quote:
which *should* be available unless you broke something.
Which one is it now, an example, or the actual name to use?
#10
10/04/2006 (3:10 am)
Quote:
Was it an example or should I really use ItemData?

It was an example, which means you can use it if it fits your needs. I am not making your game so I can not possibly know. The item class sounds like your best bet based on your description above.

Quote:
Either way it's not working.

Of course not! It will work if you follow my directions and answer my questions. (:

Previously, I asked: Where do you execute the code? Do you spawn these objects via the World Editor, perhaps?
- Like I told you before, you need item.cs. Tutorial.base does *not* contain item.cs and for that reason you can not spawn the object via the World Editor.

starter.fps has item.cs so you can just copy it over, make sure it executes and then it will work.
#11
10/04/2006 (5:23 am)
This file is logoitem.cs and is part of the getting started tutorial (therefore in the server folder). This is all I have basically. It's no real game, I'm just beginning to study the engine.

//-----------------------------------------------------------------------------
// Torque Game Engine 
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

datablock StaticShapeData(TorqueLogoItem)
{
	category = "Items";
	shapeFile = "~/data/shapes/3dtorquelogo/torque_logo.dts";
};

function TorqueLogoItem::onCollision(%this, %obj, %col)
{
echo("Collision");
	if(%col.getClassName() $= "Player")
	{
		%client = %col.client;
		%client.score++;
		commandToClient(%client, 'SetScoreCounter', %client.score);

		%obj.delete();
		%logoCount = logos.getCount();

		%x = %col.position[0] - 5 + getRandom(10);
		%z = 100 + getRandom(1.0);
		%y = %col.position[2] - 5 + getRandom(10);

		%pos = %x SPC %y SPC %z;

		%newobj = new StaticShape("Logo") {
			dataBlock = "TorqueLogoItem";
			position = %pos;
		};

		MissionCleanup.add(%newobj);

		if(%logoCount > 0)
			return;

		commandToClient(%client, 'ShowVictory', %client.score);
	}
}

The above code works, though kindof pointles. You run into a logo and it's desroyed, a new one is created near the player. But still a static object.

I took a look at the item.cs file but the item::create basically does the same as the code above?
#12
10/04/2006 (6:41 am)
Quote:
I took a look at the item.cs file but the item::create basically does the same as the code above?

This is the problem. No offence, RJ - but posting a thread asking for help is pointless if you refuse to try the solutions people suggest to you. Again, dont take this the wrong way - It's just that the solution has been presented to you twice.

Quote:
I took a look at the item.cs file but the item::create basically does the same as the code above?

True. But you need it to place items in the editor. You can place your item manually by editing your missionName.mis file, but to get the editor to place it - it needs to know HOW to create it. Every type of object you want to add in the World Editor is required to have a object::Create () function or else it will not know how to create it.

You can still create the item yourself, by manually editing the mission file. Simply paste this:

new Item("Logo") 		
{	
    dataBlock = "TorqueLogoItem";				
    position = ""; // adjust to the position you want the logo to initially begin at
};

But this way you will not be able to place it in the World Editor. If that is no big deal, then you are fine. :)

You following?
#13
10/04/2006 (7:33 am)
Yo RJ, I think the point Stefan is pushing that you seem to be missing is that you need an item in order to make the object spin.

A StaticShape is, well guess what... static however you can assign an animation to a StaticShape.

//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
function ItemData::create(%data)
{
   // The mission editor invokes this method when it wants to create
   // an object of the given datablock type.  For the mission editor
   // we always create "static" re-spawnable rotating objects.
   %obj = new Item() {
      dataBlock = %data;
      static = true;
      rotate = true;
   };
   return %obj;
}

datablock ItemData(TorqueLogoItem)
{
   // Mission editor category
   category     = "Items";

   // Basic Item properties
   shapeFile = "~/data/shapes/3dtorquelogo/torque_logo.dts";
   mass         = 1;
   elasticity   = 0.2;
   friction     = 0.6;
};

function TorqueLogoItem::onCollision(%this, %obj, %col)
{
        echo("Collision");

	if(%col.getClassName() $= "Player")
	{
		%client = %col.client;
		%client.score++;
		commandToClient(%client, 'SetScoreCounter', %client.score);

		%obj.delete();
		%logoCount = logos.getCount();

		%x = %col.position[0] - 5 + getRandom(5);
		%z = 100 + getRandom(1.0);
		%y = %col.position[2] - 5 + getRandom(5);

		%pos = %x SPC %y SPC %z;

		%newobj = new Item() {
			dataBlock = TorqueLogoItem;
			position = %pos;
                        rotate = true;
		};

		MissionCleanup.add(%newobj);

		if(%logoCount > 0)
			return;

		commandToClient(%client, 'ShowVictory', %client.score);
	}
}

That should work for you though your positioning code may place the item some distance away.
#14
10/04/2006 (7:46 am)
That would work too. As long as you make sure you include itemData::create somewhere, then you should be fine!

Good luck :)
#15
10/04/2006 (8:42 am)
Ok I'm starting to grasp the logic of Torque. Sorry for whining :D Just not yet used to the engine. I'm going to start a new testproject based on the fps starter kit and try your suggestions.

Thanks for being so patient ;) The point I indeed was missing is that the editor needs the ItemData function if I create a datablock with that name.
#16
10/04/2006 (9:16 am)
The script I posted above is all you need to get it working in the tutorial.base. Just replace the one you were currently using with what I posted ^^
#17
10/04/2006 (9:37 am)
It sure is confusing about the editor. All these things should be part of it, IMO - but for some reason they are kept in seperate files (like item.cs).

Good luck and make sure to post if you got any other questions.