Game Development Community

Another question about behaviors

by JD Scogin · in Torque Game Builder · 07/20/2007 (1:24 pm) · 17 replies

So I created a shooting game. I put 16 squares (1 tile) each) on the sceen. I tied behavior "Takes Damage" to them. I have a shooter that shoots balls. Deals Damage is tied to them.
So as the balls bounce around, they hit the squares. The squares have a healt of 100. The balls do a damage of 10. The squares get redder as they take hits. What I assumed (ach!) would happen is that each square would have it's own health and would take damage each time it was hit. But (there it is!) what happens it that other squares get darker (takes damage) as the balls strike squares. This does not make sense to me. I have moved the squares so they don't touch. Deleted them all and put the back. It seems that they take damage in order and not by which one it hit. Am I missing something (maybe) or did not code it correctly (likely) but there is not much script. Only to make sounds.
Interestingly, I have the balls take damage also, and they seem to work correctly.
Thanks for any suggestions.
Jd

#1
07/20/2007 (4:25 pm)
Are the squares all in one tilelayer, or in separate ones?
#2
07/20/2007 (7:53 pm)
Yep, all on layer 0
Here is some interesting details.
I put an echo in my call back for when the square get's hit.
The object is 1451. I put an echo in the behavior where it subtracks 10 from the health.
It shows the object as 1452. And in fact, it seems to subtrack the 10 from the next square, not
the one that is getting hit.
But then things get really crazy and each square that gets hit, kills another square.
I must have somthing really screwed up.
If anyone wants to look at it, you can down load the whole project here.
It is a little over 4 meg so it may take awhile.
Click Here
Let me know if that URL does not work. It should.

Press "S" to move left, "F" to move right, space to fire.
Thanks
#3
07/21/2007 (12:36 am)
Seems like it's because the walls are all named wall, I removed their names and it appears to work correctly... why this happens, I'm not too sure, very new to behaviors.

Also, you may want to add the Template behavior to the projectile object that is in the level, although it may be fine how it is (might only matter if it is in the play area where stuff can interact with it)
#4
07/21/2007 (5:33 am)
Amazing!
I don't think I would have ever figured that out. I guess they do not need to be named.
I did not use the name anyway, just the class.
Wow. Well, that is sure a learning.
Thanks a bunch!

Also, thanks for telling me about the Template behavior. Did not even know what it was for.
You may be new to behaviors, but you are way ahead of me!
#5
07/21/2007 (10:22 am)
Nothing to do with behaviors, but important point: The ability to "name" an object implies that any named objects should have unique names, but this is not enforced on you. However, if you have more than one object with the same name, the engine is only going to ever access the first one added to the simulation with that name, until it is removed, and then will move on to the next one, etc.
#6
07/21/2007 (12:03 pm)
Thanks, that make sense and a good lesson learned.
Jd
#7
07/21/2007 (12:54 pm)
Follow up quesiton.
I used a "Shoots" bahavior.
To tell it what to shoot, I have to give the projectile a name. Class name does not show up in the drop down list.
Now I want to find out what the healt of the projectils is. (There can be up to 10 clones at the same time)
hmmmm, can I do this? or is the out of the scope of the behavior.
Since if I name it, the only one that it can act on is the first one until it is killed.
Is this correct?
#8
07/21/2007 (1:18 pm)
I'm not an expert on the behavior system, but if you open up the file /BehaviorsPlayground/game/behaviors/game/shoots.cs, you can find the following line:

%projectile = %this.projectile.cloneWithBehaviors();

This creates a copy of the projectile you selected in the dropdown list, along with any behaviors it may have. At this point in the execution flow, you could certainly look at any fields on %projectile, but it's important to note that once you leave this scope, %projectile is it's own object in the simulation, and you would need to know the objectID of that particular projectile to inspect it.

I do think that you are probably using object names much too often in your exploration--it's more important to work with class/superclass, or other namespace identifiers instead of simply "name". As mentioned above, when you want to refer to an object by name, you can only get 1 particular instance of that object, which will cause many of the problems you seem to be having.
#9
07/21/2007 (1:21 pm)
I am not sure I understand. How do I select a projectile in the down down list without naming it. It seems that the object only shows up in the dropdown list if it is named.
Am I missing something again?
Thanks
#10
07/21/2007 (1:26 pm)
Ok, half step back:

Behaviors like "shoots" spawn off new objects as a result of the behavior being executed. The way this mechanism works is that you make an 'example' object, and name it, and allow the behavior to refer to that 'example' or "template" object.

Now, when your game runs, that particular template object is never actually part of your game play--it's simply there so that when the "shoots" behavior is executed, it knows what type of projectile to spawn.

The line I showed above is how that works--in the editor, when you drop down the list, you are populating the field "%this.projectile" with the name of your template object.

When the code runs, it creates a new object in your game, using the template object as an example to populate the newly created object.

You do need to provide a name for the "template" objects, so they can be accessed by the behaviors, but it's very important to understand that the behavior is only using that object for reference--it doesn't modify the template object directly.

Does that help to clear it up a bit?
#11
07/21/2007 (1:45 pm)
I downloaded your project, opened up the level, and removed the name field from all of your "wall" objects, leaving the wallClass intact.

Once I did this, everything worked as expected (as far as I can tell).

So moral of the story: it's fine (and you need to) name any singleton (only going to have one of) objects that are used within behavior dropdown lists.

However, you should not give the same name to multiple "instances" of objects when creating a level.
#12
07/21/2007 (3:42 pm)
I think I am getting close to understanding.
I create an object in the game and name it.
Then give that name to the behavior.
Then I can reference the behavor, but not the object.
Because the object I put in the game will never be used.
It is used only to creat the behavioral object which can be refreneced using the behavior.
Am I getting it or talking in circles.
Anyway, I think I am seeing what you are saying.
Now to try it.
I should not name then object projectile, that would confuse things.
Off to try.
#13
07/21/2007 (3:58 pm)
OK one more.
I make a projectile object and name it bullet, use that in Shoots.
If I name the object class bulletClass, will the object shoots creats be part of that class?

I am trying to see how to trap the collisions. (OnCollision)
If I add "TakesDamage) behavior to bullet,
Will
bulletClass::OnCollision(%srcObj, .....)
return the behavior object name(number)?
%behavior = %srcObj.getBehavior("TakesDamageBehavior");
if(%behavior.health < 10)
{
$balls_in_play--;
balls_text.text = $balls_inPlay;
text3.text = %behavior.healt;
}
Uploading up to date code (mess I am writting)
Jd
#14
07/22/2007 (6:22 pm)
Quote:If I name the object class bulletClass, will the object shoots creats be part of that class?

Definitely.

Also, you can use the {code}code here{/code} tags to display code correctly in posts (replace the curly braces with square ones though).
#15
07/24/2007 (12:35 pm)
So now I have it working. I know when the last ball is destoried so I can end the game. But there is one thing I don't understand.
Setup:
I have a bullet that is assigned the name bullet, class bulletClass. I assigned it a verrible of "123".
Behaviors assigned to the bullet is "Deals Damage" and "Takes Damage".
I have a player with Bevaior "Shoots" and the projections is bullet.
Good so far. Now I have some red balls that are assigned Behavior "Deals Damage".
So when the bullet hits the red ball, it is delt damage. When the bullet takes enough damage, it dies.
I have limited the number of bullets to 20. There can be up to 20 bullets flying around at the same time.
I want to end the game when the last bullet dies.
How do I do this?
I echo'ed the srcObj when a bullet hits a ball. redballClass::oncollison(%srcObj....) is say 1591.
I chech the varriable and it is "123" as expected.
So I would assume that there is a bullet with a handle of 1591.
Now I go into the "Takes Damage" and put an echo there, and it shows 1593.
Which it 2 above what I think the bullet handle should be.
I check the varriable and there is no "123".
If I subtract 2 from it and check the varriable, it is "123".
There is a part of the codes that says:
if (%this.health <= 0)
and subtrack 2 from the "%this" object then check to see if it has the "123" variable then subtrack 1 from the number of balls in play, it works.
My question is why is there a + 2 difference.
I can't figure out how to do this without modifing the code in the bahavior.
If I did not explain this very well, or you want to look at the code.
Click Here

Click Here
#16
07/24/2007 (1:04 pm)
Quote:I echo'ed the srcObj when a bullet hits a ball. redballClass::oncollison(%srcObj....) is say 1591.

The bullet would be the dstObj in that case, wouldn't it? The redball will be the srcObj since it's its class. Why the 123 field is working in that case is odd. I'll check out the scripts.

Hmm, I don't see anything blatantly wrong (though I don't see the code line you have above either. Maybe I'm looking at a slightly different version?

Anyway, the figure this out, I'd try making all bullets and redBalls print out their ID when they're added. And maybe the zig field too. That way you can see what numbers should be giving what results.

Maybe that'll help - sorry I can't offer a solution.
#17
07/24/2007 (3:10 pm)
Yea, I think you are right. It may have been dstObj. But it was the bullet, because it had the "123". No other objects had that tied to them.
Thanks for the correction.
Then line of code above is in the "Takes Damage" behavior.
Jd