Newbie help setting up a building datablock and script
by Alastair Paterson · in Torque Game Engine · 03/19/2003 (9:08 am) · 6 replies
Hi, I've got a bit stuck and would appreciate any help:
I want to put buildings on the landscape that can be 'infected' and 'cured' with special weapons (as opposed to being damaged). I've created my own infector/curer weapons and bullets, and my building .dts successfully. Now I need to write the script to make this happen. So far I have:
datablock StaticShapeData(City1)
{
category = "Cities";
shapeFile = "~/data/shapes/buildings/city1/city.dts";
isInfected = true;
hitsRequired = 3;
hitsSoFar = 0;
};
function City1::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
{
//city was hit
if (%damageType $= "curerTurnerBullet")
{
if (%obj.isInfected)
{
%obj.hitsSoFar++;
if (%obj.hitsSoFar == %obj.hitsRequired)
{
echo("cured");
%obj.isInfected = 0;
...
}
}
}
if (%damageType $= "infectorTurnerBullet")
{
...
}
}
Then I placed the building using the editor and tried shooting it.
This doesn't completely work. The %damageType works, so i can check what sort of weapon hit the building. However the other if's fail, and printing the variables with:
echo("Infected:");
echo(%obj.isInfected);
echo("Hits so far:");
echo(%obj.hitsSoFar);
echo("Required:");
echo(%obj.hitsRequired);
...
prints out an empty newline for each %obj.x.
If instead of using %obj i use %this it prints out the numbers i would expect. I thought that %this represented the datablock being used, while %obj represented the instance of the object using my datablock? Surely I should be using %obj for this, because I have several different buildings. I'm sure I'm doing something simple wrong.
Any help would be greatly appreciated.
I want to put buildings on the landscape that can be 'infected' and 'cured' with special weapons (as opposed to being damaged). I've created my own infector/curer weapons and bullets, and my building .dts successfully. Now I need to write the script to make this happen. So far I have:
datablock StaticShapeData(City1)
{
category = "Cities";
shapeFile = "~/data/shapes/buildings/city1/city.dts";
isInfected = true;
hitsRequired = 3;
hitsSoFar = 0;
};
function City1::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
{
//city was hit
if (%damageType $= "curerTurnerBullet")
{
if (%obj.isInfected)
{
%obj.hitsSoFar++;
if (%obj.hitsSoFar == %obj.hitsRequired)
{
echo("cured");
%obj.isInfected = 0;
...
}
}
}
if (%damageType $= "infectorTurnerBullet")
{
...
}
}
Then I placed the building using the editor and tried shooting it.
This doesn't completely work. The %damageType works, so i can check what sort of weapon hit the building. However the other if's fail, and printing the variables with:
echo("Infected:");
echo(%obj.isInfected);
echo("Hits so far:");
echo(%obj.hitsSoFar);
echo("Required:");
echo(%obj.hitsRequired);
...
prints out an empty newline for each %obj.x.
If instead of using %obj i use %this it prints out the numbers i would expect. I thought that %this represented the datablock being used, while %obj represented the instance of the object using my datablock? Surely I should be using %obj for this, because I have several different buildings. I'm sure I'm doing something simple wrong.
Any help would be greatly appreciated.
#2
It seems like something is seriously wrong-
I added: echo(%obj); which returned 1439. So at the console I typed-
1439.dump();
Which returned lots of stuff, but no 'isInfected' or any of my other added variables.
It did have:
dataBlock = "City1"
but no Tagged Fields.
Also I tried:
City1.dump()
Which does correctly list all my variables as 'Tagged Fields'.
Am I referencing the wrong thing somehow? I think it must be confusion between datablocks and objects. I want one datablock with several instances which i thought were then objects referenced as %obj? Am I wrong?
03/19/2003 (3:44 pm)
Thanks for the reply.It seems like something is seriously wrong-
I added: echo(%obj); which returned 1439. So at the console I typed-
1439.dump();
Which returned lots of stuff, but no 'isInfected' or any of my other added variables.
It did have:
dataBlock = "City1"
but no Tagged Fields.
Also I tried:
City1.dump()
Which does correctly list all my variables as 'Tagged Fields'.
Am I referencing the wrong thing somehow? I think it must be confusion between datablocks and objects. I want one datablock with several instances which i thought were then objects referenced as %obj? Am I wrong?
#3
City1.dump();
which does output my tagged fields (isInfected etc) with the correct default values i set in the datablock (unsurprisingly). I think the problem is my misunderstanding of how to use/reference datablocks and objects.
Cheers for any help
03/20/2003 (2:22 am)
I also tried:City1.dump();
which does output my tagged fields (isInfected etc) with the correct default values i set in the datablock (unsurprisingly). I think the problem is my misunderstanding of how to use/reference datablocks and objects.
Cheers for any help
#4
Was also wondering - Is it ok to place the building in the editor or do i need to do something explicit in script to initialise it? At the moment I've just created it in the editor, which puts the following entry in my .mis file:
new StaticShape() {
position = "-36.0277 9.72425 303.018";
rotation = "1 0 0 0";
scale = "5 5 5";
dataBlock = "City1";
};
To restate my problem:
My datablock is this-
datablock StaticShapeData(City1)
{
category = "Cities";
shapeFile = "~/data/shapes/buildings/city1/city.dts";
isInfected = true;
hitsRequired = 3;
hitsSoFar = 0;
};
and I want to access those fields from my function:
function City1::damage(%this, %obj, %sourceObject, %position, %damage, %damageType){}
but they seem to be empty in this function.
ie. %obj.isInfected, %obj.hitsSoFar etc all echo a blank line.
I'm sure this is really simple and I'm just not understanding how the script language works. Would be great if someone could put me right! :)
03/23/2003 (6:57 am)
Don't suppose anyone can tell me what I'm doing wrong with my above code?...Was also wondering - Is it ok to place the building in the editor or do i need to do something explicit in script to initialise it? At the moment I've just created it in the editor, which puts the following entry in my .mis file:
new StaticShape() {
position = "-36.0277 9.72425 303.018";
rotation = "1 0 0 0";
scale = "5 5 5";
dataBlock = "City1";
};
To restate my problem:
My datablock is this-
datablock StaticShapeData(City1)
{
category = "Cities";
shapeFile = "~/data/shapes/buildings/city1/city.dts";
isInfected = true;
hitsRequired = 3;
hitsSoFar = 0;
};
and I want to access those fields from my function:
function City1::damage(%this, %obj, %sourceObject, %position, %damage, %damageType){}
but they seem to be empty in this function.
ie. %obj.isInfected, %obj.hitsSoFar etc all echo a blank line.
I'm sure this is really simple and I'm just not understanding how the script language works. Would be great if someone could put me right! :)
#5
for example:
03/23/2003 (7:21 am)
Did you add these variables to StaticShapeData::initPersistFields() in the c++ code?for example:
void StaticShapeData::initPersistFields()
{
Parent::initPersistFields();
addField("isInfected", TypeBool, Offset(isInfected, StaticShapeData));
addField("hitsRequired", TypeF32, Offset(hitsRequired, StaticShapeData));
addField("hitsSoFar", TypeF32, Offset(hitsSoFar, StaticShapeData));
// ect....
#6
function City1::onAdd(%this,%obj)
{
%obj.isInfected = true;
%obj.hitsRequired = 5;
%obj.hitsSoFar = 0;
}
This now seems to be completely ignoring the datablock, because it also works with my datablock variables commented out:
datablock StaticShapeData(City1)
{
category = "Cities";
shapeFile = "~/data/shapes/buildings/city1/city.dts";
//isInfected = true;
//hitsRequired = 5;
//hitsSoFar = 0;
};
I don't know what's going on, but it'll do for now :)
03/23/2003 (10:20 am)
I've just got it to work by adding:function City1::onAdd(%this,%obj)
{
%obj.isInfected = true;
%obj.hitsRequired = 5;
%obj.hitsSoFar = 0;
}
This now seems to be completely ignoring the datablock, because it also works with my datablock variables commented out:
datablock StaticShapeData(City1)
{
category = "Cities";
shapeFile = "~/data/shapes/buildings/city1/city.dts";
//isInfected = true;
//hitsRequired = 5;
//hitsSoFar = 0;
};
I don't know what's going on, but it'll do for now :)
Torque Owner Badguy
comes empty the rest of your code wont run.
be sure to have this set to true for an infected building
you can
Edit:
in the console for a variable dump
and it will dump your variables added as tags.
you need a true there. (1)