I have suddenly remembered this here series of combat scripting posts that I was doing the other Geological Epoch!
So I will do another one. A pretty simple, but important one; our first Combat Meter.
Now as I didn’t explain in the first post but probably should have, when it comes to people (AVs) taking “damage” in Second Life, there are two distinctly different ways that can work. There is a notion of “health” and “damage” that are built directly into the world (but as far as I can tell very seldom used, because they’re inflexible and kind of silly), and then there’s “health” and “damage” as enforced by scripts in things that you wear.
(In a way this is like the difference between vendors that let you buy using “buy”, and those that let you buy using “pay”, as explained in my extremely thorough post on the subject. “Buy” is built into the world whereas “Pay” enables scripting, just like Linden Damage is build into the world whereas Scripted Damage is scripted. Not that there is anything all that significant about the analogy. :) )
Many, even most, Residents probably don’t even know that every AV is always at some state of Health, and that if that Health goes to zero, we “die”, in the sense that we are suddenly teleported Home, just as though we’d hit control-shift-h or whatever.
The reason that people don’t know this is that the vast majority of SL land has the “damage enabled” flag turned off, so you’re always at 100% health, and the viewer doesn’t normally even bother displaying the health meter in that case.
But if you stop by my Park in the center of Hughes Rise, or various other places, and then look carefully, you’ll be able to find a little heart on the screen, probably with a “100%” next to it. It you fly or teleport a few dozen meters into the sky and let yourself fall, or fly at high speed into a tree, or other amusing things, the number may go down from 100% (and then rather quickly go back up again). If you lose enough health fast enough, you may even hear your AV make a little supposedly gender-appropriate pained-grunt sort of noise.
And if you get the little number next to the heart down to 0%, you will suddenly be teleported home.
Now that sounds like a useful basis for a combat system, but it really isn’t, because it’s relatively easy to script up an object that instantly kills anyone it’s pointed at, and being teleported home isn’t a very flexible way to implement defeat (for instance it’s tough to script a scoreboard of “kills” or anything, just for one example), and healing is always exactly the same over time (no way to make “medikits” or “health potions” or anything), and it doesn’t apply to non-AVs (targets, monsters, robots, etc) and so on and so on.
So basically no one uses the built-in health and damage system for actual combat. What they use instead is Scripted Damage, which works by having the combatants wear some sort of attachment or other, that somehow finds out when they are “damaged” by “weapons”, or “healed” by whatever, and does whatever the system designer thought appropriate when they “die”.
The things that you wear that keep track of your damage generally have some sort of indicator of how healthy you are right now, in Scripted Damage terms, and are therefore generically called Damage Meters.
In today’s posting here, we will make a Damage Meter. Or, actually, we will discover that we’ve Already Got One!
Already got one with a few modifications, that is; because it turns out that our self-healing target, from the self-healing target post, makes a very nice basis for an AV damage meter.
This is because of this Very Important Fact: when something hits your AV, all of your attachments see the collision event.
(I understand that this may not be true in OpenSim, or in some OpenSims, in which case damage meters there will have to work in some entirely other way of which I amn’t aware; I may explore that at some point too.)
So for instance if you were to take our self-healing target into inventory, and attach it to the center of your HUD say, and stand in front of the auto-popgun, you would see the health value above it go gradually down; and then if you step aside out of the stream of pellets, it will go slowly up again.
The only problem with it is that when it gets down to zero nothing useful happens. The script does an “llDie()” at that point, which you might think would cause the attachment to Cease To Be, or to detach or something, but in fact (another Interesting Fact) llDie() does nothing at all in attachments, so nothing actually happens.
This is easy to change, though. For now let’s just have the meter say something amusing when you “die”, to demonstrate that it is working. We will just change the process_collision
routine so that it does something slightly different when health is zero, as in:
process_collision(integer index) { if (llVecMag(llDetectedVel(index))>15) { health = health - 1; show_health(health,MAX_HEALTH); if (health<=0) { llSay(0,"Arg! "+llKey2Name(llGetOwner())+ " has been defeated!"); } llSetTimerEvent(HEAL_SECONDS); } }
(We also took out the “pow” and “clunk”, because we’re confident enough that things are generally working that we don’t need them anymore.)
So modify the target script as above, put it into a prim, wear that prim somewhere appropriate on your HUD, and stand in front of the auto-popgun or have a friend shoot at you, and you will find yourself being “damaged” and eventually being “defeated”.
Combat! Shazam!
Some other stuff one might want to do:
- Support “medikits” or “health potions” or whatever, that heal you when, say, you walk (run) across them (I have code for that!),
- Cause you to, say, fall down and stop running around when you “die” (I have code for that too!),
- Remove your ability to use your own weapons while you are “dead” (I don’t have code for that yet, but I might talk about various approaches).
(The other thing I’m currently playing with, which I haven’t quite scripted up yet, is to have the targets, the “monsters”, be “lootable”, so that once you’ve defeated one you can touch it or something and get I dunno some kind of reward. If anyone knows of a sim or a system of scripts that does this sort of WoW-like lootable-monsters thing, let me know; I’d like to take a look!)
So that’s all for this time. Pew pew! :)
Filed under: scripting, Second Life | Tagged: combat, scripting, Second Life |
Thank you, so much, for this series of tutorials! I don’t suppose you ever plan to make more? I would love to be able to learn how to make moving targets, especially ones that won’t stray too far from the spot it was originally placed. Also, I’ve been playing around with the basic melee weapons script. How would that work with what has already been covered?
I should so get back to this! :) I am far too easily distracted.
Moving targets that stay in an area would be relatively easy.
I’m not sure which one is the basic melee weapons script; there are a few of them. Depending exactly how it works, it would probably not be hard to incorporate…
The one I got ahold of and have been playing with is called “Swordfighting.lsl” I can copy and paste the link, here, if it’s okay.
Sure, if WordPress will let you and all…
Thank you! Here is the link: http://www.outworldz.com/cgi/freescripts.plx?ID=864
Interesting, thanks! Apart from the animations and sounds, the only interaction with the target that I see in that script is the llPushObject() call. I don’t know if that actually does Linden damage to the target, or just gives them a bit of a shove. I will have to experiment! My guess is that it just shoves them, so this script actually doesn’t do any kind of damage at all, but does let you fight with animations and sounds and being knocked here and there. :)
Thanks so much for this tutorial but where might I find the code for heals and to lay down “dead”? Would love to incorporate those elements. Thanks again