How to delete objects nicely under 1 function 1 by 1
by Vlad I · in Torque Game Builder · 04/28/2012 (1:02 pm) · 14 replies
Sorry for newbie question, I just want to make it look nice :)
I have made this simple code which works ok for me but was thinking I could make it all work under one function with out having to repeat it 10 times.
I want to delete objects with clicks 1 by1.
I have made this simple code which works ok for me but was thinking I could make it all work under one function with out having to repeat it 10 times.
I want to delete objects with clicks 1 by1.
function Dummy01::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Scissors01))
{
Scissors01.safedelete();
}
}
function Dummy02::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Hammer))
{
Hammer.safedelete();
{
}I tried this, but it deletes straight 2 objects:function Dummy01::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Scissors01))
{
Scissors01.safedelete();
}
if(isObject(Hammer))
{
Hammer.safedelete();
{
}About the author
#2
Are you saying that you don't get why this (edit) deletes 2 objects?:
04/28/2012 (3:11 pm)
I don't understand what you are doing i'm afraid.Are you saying that you don't get why this (edit) deletes 2 objects?:
function Dummy01::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Scissors01))
{
Scissors01.safedelete();
}
if(isObject(Hammer))
{
Hammer.safedelete();
{
}But, isObject tests if the object exists, so if both of the objects exists they both get deleted of course.
#3
It is the easiest way to make it all work without bugs for me.
Ok, Scissors01 has class Dumm01, Hammer has class Dummy02.
With this code it all works great, no probs. I click on Scissors01 it gets deleted, then I click on hammer it gets deleted.(mind you I dont want 2 objects to be deleted at once with one click on either object, just one by one, click after click)
I just thought I could make it all under one function, so I wouldn't need to repeat this code 10 more times for other objects. If I can't, it's fine, copy/paste works just as well :D
04/29/2012 (12:01 am)
:) I'm addicted to multiple classes.It is the easiest way to make it all work without bugs for me.
Ok, Scissors01 has class Dumm01, Hammer has class Dummy02.
With this code it all works great, no probs. I click on Scissors01 it gets deleted, then I click on hammer it gets deleted.(mind you I dont want 2 objects to be deleted at once with one click on either object, just one by one, click after click)
I just thought I could make it all under one function, so I wouldn't need to repeat this code 10 more times for other objects. If I can't, it's fine, copy/paste works just as well :D
function Dummy01::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Scissors01))
{
Scissors01.safedelete();
}
}
function Dummy02::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Hammer))
{
Hammer.safedelete();
{
}
#4
Edit:
Using script object you can do this:
04/29/2012 (4:33 am)
Maybe you could say:%this $= Hammeror
%this.class $= Hammer
%this.superclass $= HammerSomething like that, not sure of what the %this contains unfortunately.
Edit:
Using script object you can do this:
new ScriptObject( scrObj ){
superClass = scrClass;
type = "someScript";
};
function scrClass::doSomething(%this)
{
echo(%this.type); // Outputs: "someScript"
}So i believe %this would refer to the object.
#5
04/29/2012 (7:29 am)
Very funny Lukas
#6
I was unsure on wheter %this would refer to the class or the object calling. I am not entirely sure how your code is set up or what the Hammer object is.
But a solution for you is to add a dynamicfield to each of the DummyObjects so if you pressed a dummy then it would delete the object which is associated with said dummy, you could add the name of the object as a dynamic field to the Dummy object.
Then when the DummyObject is pressed it would call %this.obj.safedelete();
Example:
04/29/2012 (7:41 am)
I didn't intend to be fun. I am sorry if I offended you in any way!I was unsure on wheter %this would refer to the class or the object calling. I am not entirely sure how your code is set up or what the Hammer object is.
But a solution for you is to add a dynamicfield to each of the DummyObjects so if you pressed a dummy then it would delete the object which is associated with said dummy, you could add the name of the object as a dynamic field to the Dummy object.
Then when the DummyObject is pressed it would call %this.obj.safedelete();
Example:
new ScriptObject( scrObj1 ){
superClass = scrClass;
obj = Hammer;
};
new ScriptObject( scrObj2 ){
superClass = scrClass;
obj = Scissors01;
};
function scrClass::onMouseDown(%this)
{
%this.obj.safedelete();
}
//If scrObj1 is pressed Hammer is deleted
//If scrObj2 is pressed Scissors01 is deletedI know onMouseDown doesn't apply to scripobjects but it is just an example of how it would work.
#7
04/29/2012 (12:12 pm)
%this should reference the object, so yes, something like %this.delete if you want to make a class of item that deletes itself on being clicked.
#8
04/29/2012 (12:39 pm)
Thanks guys. I ended up the old way. It is less messy, and easier for me to understand it.
#9
What is Scissors01? What does it DO? An object named Scissors01 should probably never EVER be of the class "Dummy01"... let alone "Dumm02". It's scissors! It should be of class... "tool", perhaps.
Now these scissors and hammer objects -- is deleting themselves when you click them ALL they are supposed to do in your game? If that's the case, I could see maybe making a class "deleteWhenClicked", and then you could make both your scissors and hammer of that class type.
Assuming they're to do something more than that (and not ALWAYS just delete themselves when clicked), you'll need a bit more to your "tool" class or whatever.
At any rate all will be made clear if you just describe your game a bit and what the heck is going on! :)
04/29/2012 (12:41 pm)
PLEASE describe what you're trying to do overall -- it'll help us help you!What is Scissors01? What does it DO? An object named Scissors01 should probably never EVER be of the class "Dummy01"... let alone "Dumm02". It's scissors! It should be of class... "tool", perhaps.
Now these scissors and hammer objects -- is deleting themselves when you click them ALL they are supposed to do in your game? If that's the case, I could see maybe making a class "deleteWhenClicked", and then you could make both your scissors and hammer of that class type.
Assuming they're to do something more than that (and not ALWAYS just delete themselves when clicked), you'll need a bit more to your "tool" class or whatever.
At any rate all will be made clear if you just describe your game a bit and what the heck is going on! :)
#10
I'm working on a hidden object game.
On the screen in the room you have a number of hidden objects you need to find so when you click on one of them it gets deleted and the label describing it should be deleted too along with it. After you find all of them a trigger should be set visible when all objects are found.
LevelTrHo03 is a trigger to go to another room.
Shovel01Part2Ho03 is a name of the object using class Hos.
Shovel01Part2Dummy is a name of the label object for Shovel01Part2Ho03, it should be deleted when Shovel01Part2Ho03 is clicked.
And so on for other objects you need to find Scissors01Part2Ho03, Sword01Part2Ho03 etc.
Object01 is just a function for the Shovel01Part2Ho03.
Object02 is for Scissors01Part2Ho03 etc.
It all works great except that I can't make LevelTrHo03 level trigger set visible after all objects are found.
04/29/2012 (1:18 pm)
Ok, since then I modified it heavily, so bare with me I'll try to explain it.I'm working on a hidden object game.
On the screen in the room you have a number of hidden objects you need to find so when you click on one of them it gets deleted and the label describing it should be deleted too along with it. After you find all of them a trigger should be set visible when all objects are found.
LevelTrHo03 is a trigger to go to another room.
Shovel01Part2Ho03 is a name of the object using class Hos.
Shovel01Part2Dummy is a name of the label object for Shovel01Part2Ho03, it should be deleted when Shovel01Part2Ho03 is clicked.
And so on for other objects you need to find Scissors01Part2Ho03, Sword01Part2Ho03 etc.
Object01 is just a function for the Shovel01Part2Ho03.
Object02 is for Scissors01Part2Ho03 etc.
It all works great except that I can't make LevelTrHo03 level trigger set visible after all objects are found.
$TotalNumber= 0;
function Hos::onAdd(%this)
{
$TotalNumber++;
}
//function AllowBack(%this)
//{
// LevelTrHo03.setUseMouseEvents( true );
//}
function Hos::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
%this.safeDelete();
$TotalNumber--;
if( $TotalNumber== 0 )
LevelTrHo03.setUseMouseEvents( true );
}
function Object01::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Shovel01Part2Ho03))
{
//$TotalNumber--;
playEffect(%this);
Shovel01Part2Ho03.safedelete();
Shovel01Part2Dummy.safedelete();
updateSceneSave(%this);
// AllowBack(%this);
}
}
function Object02::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Scissors01Part2Ho03))
{
//$TotalNumber--;
playEffect(%this);
Scissors01Part2Ho03.safedelete();
Scissors01Part2Dummy.safedelete();
updateSceneSave(%this);
// AllowBack(%this);
}
}
function Object03::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Sword01Part2Ho03))
{
playEffect(%this);
Sword01Part2Ho03.safedelete();
Sword01Part2Dummy.safedelete();
updateSceneSave(%this);
}
}and it shows no errors.
#11
Too many functions with onMouseDown, but I don't mind it as long as it does the job.
04/29/2012 (1:31 pm)
Now you see Tim why I love multiple classes :DToo many functions with onMouseDown, but I don't mind it as long as it does the job.
#12
Why not just do one class method like this?
Then just name your items in the scene Scissors_obj, Hammer_obj, etc... and the label for them Scissors_word, Hammer_word, etc..
At the start of each level you'd have to set $sceneItemsLeft to the number of items in that scene, but that's not so bad.
(I realize you don't have a fadoutandkill() function, but it just fades out the object and kills it.) :)
No crazy nested classes all over the place!
No need to keep adding code just to make more scenes!
Also -- have you done the behaviors tutorials? This seems like a job for behaviors. :D
04/29/2012 (3:04 pm)
OK I gotcha.Why not just do one class method like this?
function HiddenObject::onMouseDown( %this, %modifier, %worldPosition, %click)
{
if(%this.found)
return;
%str = strreplace(%this.getname(), "obj","word");
fadeoutandkill(%str, 500); // deletes the label
fadeoutandkill(%this, 500); // deletes the object in the scene
%this.found = 1;
// do sound
alxplay(sound_found);
// do particles
// do saveGame
updateSceneSave();
$sceneItemsLeft--;
if($sceneItemsLeft == 0)
{
nextLevelTrigger.setvisible(1);
}
}Then just name your items in the scene Scissors_obj, Hammer_obj, etc... and the label for them Scissors_word, Hammer_word, etc..
At the start of each level you'd have to set $sceneItemsLeft to the number of items in that scene, but that's not so bad.
(I realize you don't have a fadoutandkill() function, but it just fades out the object and kills it.) :)
No crazy nested classes all over the place!
No need to keep adding code just to make more scenes!
Also -- have you done the behaviors tutorials? This seems like a job for behaviors. :D
#13
04/29/2012 (11:44 pm)
How about using a global onMouseDown and then checking which objects it hits.function sceneWindow2D::onMouseDown( %this, %modifier, %worldPosition, %click)
{
//check if we hit something
%hitObjects = sceneWindow2D.getSceneGraph().pickPoint(%worldPosition);
%nrOfHits = getWordCount(%hitObjects);
for(%i=1;%i<=%nrOfHits;%i++)
{
%object = getWord(%hitObjects, %i-1);
if(%object.class $= "className1" || %object.class $= "clasName2" || %object.class $= "className3")
{
%object.safeDelete();
}
}
#14
Although I still have a problem with LevelTrHo03 trigger. When I come back to that level again the level trigger is off since no objects left to be deleted. The script wrote them all in previouslly into the save file and now read them all out onLevelLoaded. So, no objects in the scene hence LevelTrHo03 level trigger is off and waits for the clicks, which won't happen.
How do I go about this?
I'm trying this with no errors but LevelTrHo03 is still off when I come back to the level:
I did do all the tutorials :D I wouldn't have gone that far if I havn't done it.
04/30/2012 (1:59 am)
Thanks guys!Although I still have a problem with LevelTrHo03 trigger. When I come back to that level again the level trigger is off since no objects left to be deleted. The script wrote them all in previouslly into the save file and now read them all out onLevelLoaded. So, no objects in the scene hence LevelTrHo03 level trigger is off and waits for the clicks, which won't happen.
How do I go about this?
I'm trying this with no errors but LevelTrHo03 is still off when I come back to the level:
function counterHo03 :: onLevelLoaded (%this, %scenegraph)
{
LevelTrHo03.setUseMouseEvents( false );
$sceneItemsLeft = 0;
}
function HiddenObject1::onAdd(%this)
{
%this.setUseMouseEvents( true ); // enabling mouse event
$sceneItemsLeft++;
}
function HiddenObject1::onMouseDown( %this, %modifier, %worldPosition, %click)
{
if(%this.found)
return;
%str = strreplace(%this.getname(), "obj","word");
// fadeoutandkill(%str, 500); // deletes the label
// fadeoutandkill(%this, 500); // deletes the object in the scene
%str.safedelete();
%this.safedelete();
%this.found = 1;
playEffect(%this);
updateSceneSave(%this);
$sceneItemsLeft--;
if($sceneItemsLeft == 0)
{
LevelTrHo03.setUseMouseEvents( true );
}
}Or should I read out the objects from the save file and tell onLevelLoaded that nothing is left so the level trigger from now on should be using mouse events?I did do all the tutorials :D I wouldn't have gone that far if I havn't done it.
Torque 3D Owner Tim Scheiman
Ghost Ship Studios
What is a "Dummy"? And why do you have two separate functions for onMouseDown for that class?
Can you describe what you're doing on a larger scale so we have some idea what is going on?