Keeping track of how many objects are in a box?
by Georgina Benedetti · in Torque Game Builder · 02/13/2011 (5:02 pm) · 7 replies
I am trying to create a piece of code where whenever an object falls into a box, a counter goes up by one and when the object is taken out the counter decreases by one. I have been having trouble with trying to get the counter to increase. I have tried two different codes;
<code>
function itemClass::onCollision(%srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
%AnItemPositionX = %srcObject.getPositionX();
%AnItemPositionY = %srcObject.getPositionY();
if(%AnItemPositionX < 62 && %AnItemPositionX)
{
if(%AnItemPositionY < 148 && %AnItemPositionY > 92)
{
echo("in the crate");
$counter++;
crateText.text = "rate =" SPC $counter;
%srcObject.stopCounter();
}
}
}
<code>
this isn't ideal, because it continues to increase my counter by one over and over again, instead of just once. The other code is;
<code>
function itemClass::check(%this)
{
%this.isChecking = true;
%this.schedule(10 * %this.checkIndex, checkCrate);
%this.checkIndex++;
}
function itemClass::checkCrate(%this)
{
%AnItemPositionX = %this.getPositionX();
%AnItemPositionY = %this.getPositionY();
if(%AnItemPositionX < 62 && %AnItemPositionX > -62 && %AnItemPositionY < 148 && %AnItemPositionY > 92)
{
echo("in the crate");
$counter++;
crateText.text = "rate =" SPC $counter;
}
if(%this.isChecking)
{
%this.check();
}
}
<code>
but this doe not want to work either...
<code>
function itemClass::onCollision(%srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
%AnItemPositionX = %srcObject.getPositionX();
%AnItemPositionY = %srcObject.getPositionY();
if(%AnItemPositionX < 62 && %AnItemPositionX)
{
if(%AnItemPositionY < 148 && %AnItemPositionY > 92)
{
echo("in the crate");
$counter++;
crateText.text = "rate =" SPC $counter;
%srcObject.stopCounter();
}
}
}
<code>
this isn't ideal, because it continues to increase my counter by one over and over again, instead of just once. The other code is;
<code>
function itemClass::check(%this)
{
%this.isChecking = true;
%this.schedule(10 * %this.checkIndex, checkCrate);
%this.checkIndex++;
}
function itemClass::checkCrate(%this)
{
%AnItemPositionX = %this.getPositionX();
%AnItemPositionY = %this.getPositionY();
if(%AnItemPositionX < 62 && %AnItemPositionX > -62 && %AnItemPositionY < 148 && %AnItemPositionY > 92)
{
echo("in the crate");
$counter++;
crateText.text = "rate =" SPC $counter;
}
if(%this.isChecking)
{
%this.check();
}
}
<code>
but this doe not want to work either...
#2
Does %this.owner.getAnimation(); mean that it is asking for the animation of the object that the behavior is on?
At the start of the code, can I create a variable for the crate number? Would it look like $crate = 0; or is it different in behaviors? Then later on would I be able to use the code cratetext.text = "crate =" SPC $score; ?
I'm very confused.
02/26/2011 (6:11 pm)
I did that tutorial a while ago but I'm having trouble understanding all the code and what goes where when it comes to behaviors. I'm also finding it difficult to adjust the code to what I want to happen in my scene. Does %this.owner.getAnimation(); mean that it is asking for the animation of the object that the behavior is on?
At the start of the code, can I create a variable for the crate number? Would it look like $crate = 0; or is it different in behaviors? Then later on would I be able to use the code cratetext.text = "crate =" SPC $score; ?
I'm very confused.
#3
You can use behaviours to make crates easily configurable. Let's say you have a behaviour named Container. You want the unique identifier to be configurable in a text field where you just enter the number in the editor. Parts of the behaviour code could look like this:
OK, so that stub is slightly useless. You could for example use the Name field of the scene object (under Scripting), or a dynamic field. But a behaviour gathers settings in one place, and you can also make up some sane defaults. For instance, you could keep track of the highest crate ID added in a global variable and automatically assign the IDs, if the Identifier field is left at 0.
If we assume you have an easy case of crates which don't move, it's easy to build a crate from multiple objects. Rough list of things to do (assuming side/front view game here):
Now you just need the onEnter() and onLeave() methods in the Container behaviour to check what sort of object is entering, and update any local and global variables as necessary.
For the more advanced: Make the behaviour create the necessary trigger object and collision objects, sizing and positioning them according to the crate image. This is not actually hard, and good practice :)
(Mounting may be an option for crates which move.)
02/26/2011 (7:53 pm)
%this.Owner refers to the object a behaviour is attached to, yes.You can use behaviours to make crates easily configurable. Let's say you have a behaviour named Container. You want the unique identifier to be configurable in a text field where you just enter the number in the editor. Parts of the behaviour code could look like this:
if (!isObject(Container))
{
%template = new BehaviorTemplate(Container);
%template.friendlyName = "Container";
%template.behaviorType = "Custom object";
%template.description = "Attach to an object to make it a container which can hold objects.";
%template.addBehaviorField(Identifier, "Unique identifier for reference.", STRING, "0");
}
function Container::onAddToScene(%this, %scenegraph)
{
// Handy to do since this behaviour might expand later
%o = %this.Owner;
// This lets you get the identifier from the object itself
%o.CrateNumber(%this.Identifier);
}OK, so that stub is slightly useless. You could for example use the Name field of the scene object (under Scripting), or a dynamic field. But a behaviour gathers settings in one place, and you can also make up some sane defaults. For instance, you could keep track of the highest crate ID added in a global variable and automatically assign the IDs, if the Identifier field is left at 0.
If we assume you have an easy case of crates which don't move, it's easy to build a crate from multiple objects. Rough list of things to do (assuming side/front view game here):
- A sprite for the crate, ordered on the appropriate layer. If objects dropped in should disappear, this would be in a layer in front of them[/li]
- Scene objects with collision to make up the walls and bottom of the crate.[/li]
- A trigger object with the Container behaviour, filling up the contents of this crate.[/li]
- Objects to drop into the crate, possibly with their own unique dynamic fields or behaviours to let the crate adjust score.[/li]
Now you just need the onEnter() and onLeave() methods in the Container behaviour to check what sort of object is entering, and update any local and global variables as necessary.
For the more advanced: Make the behaviour create the necessary trigger object and collision objects, sizing and positioning them according to the crate image. This is not actually hard, and good practice :)
(Mounting may be an option for crates which move.)
#4
Create a trigger object in the editor which surrounds your crate. Make sure it has send collision and the objects entering the crate have receive collision. Also make sure to turn on the enter and leave callbacks in the Trigger object options. Give your trigger a class in the editor, such as crateTriggerClass.
Now you can use the onEnter and onLeave functions something like this:
02/27/2011 (2:53 am)
I'd recommend avoid behaviors until you're more familiar with Torquescript.Create a trigger object in the editor which surrounds your crate. Make sure it has send collision and the objects entering the crate have receive collision. Also make sure to turn on the enter and leave callbacks in the Trigger object options. Give your trigger a class in the editor, such as crateTriggerClass.
Now you can use the onEnter and onLeave functions something like this:
function crateTriggerClass::onEnter(%this, %object)
{
if (%object.class $= "ballClass") // or whatever class your objects should have
$counter++;
}
#5
Also is %object just a variable you made up or is it in the built into torque editor?
Thanks for the help! I really didn't want to get to know behaviors just yet...
03/02/2011 (3:02 pm)
Okay that makes sense but I just want to ask what does the $ do in front of the = ?Also is %object just a variable you made up or is it in the built into torque editor?
Thanks for the help! I really didn't want to get to know behaviors just yet...
#6
03/02/2011 (3:32 pm)
:)
#7
The %object is a parameter passed in by the system, and is the reference to the object being collided with.
03/02/2011 (7:53 pm)
$= means string comparison. If you expect numbers, use ==.The %object is a parameter passed in by the system, and is the reference to the object being collided with.
Torque Owner Shawn Simas
Bigfoot Studios
The Rainy Day Tutorial has examples.