Subtracting Bullets

Weiand
26 Oct 2011, 01:55
Here is where I am at.

Want to shoot gun, count down the bullets, be empty, relaod it, then start it all over

Have object Gun. Gun is object, not a container.
When taking Gun

<ontake type="script">
SetObjectFlagOn (Gun, "6")
</ontake>

So that puts Gun in player inventory & sets obj Gun flag to 6, giving Gun 6 bullets

There is an object Ammo (a box of ammo for gun)
Ammo has a "Load" button in inventory when taken

<Load type="script">
if (GetBoolean(Gun, "0")) {
SetObjectFlagOn (Gun, "6")
msg ("You loaded the gun")
}
else {
if (not GetBoolean(Gun, "0")) {
msg ("Gun does not need to be reloaded")
}
}
</Load>

So if obj Gun flag is not 0, says does not need to be reloaded, which works fine
if obj Gun flag is 0, should "load" set obj Gun flag to 6

Now to the part that doesn't work


<command name="Shoot">
<pattern>shoot #object#</pattern>
<script>
if (GetBoolean(Gun, "6")) {
SetObjectFlagOn (Gun, "5")
}
else {
if (GetBoolean(Gun, "5")) {
SetObjectFlagOn (Gun, "4")
}
else {
if (GetBoolean(Gun, "4")) {
SetObjectFlagOn (Gun, "3")
}
else {
if (GetBoolean(Gun, "3")) {
SetObjectFlagOn (Gun, "2")
}
else {
if (GetBoolean(Gun, "2")) {
SetObjectFlagOn (Gun, "1")
}
else {
if (GetBoolean(Gun, "1")) {
SetObjectFlagOn (Gun, "0")
}
else {
if (GetBoolean(Gun, "0")) {
msg ("You are out of bullets")
}
}
}
}
}
}
}
switch (object) {
case (player) {
msg ("You don't really want to shoot yourself")
}
default {
msg ("You shoot \"" + object.name + "\"and cause heavy damage")
}
}
</script>
</command>


When typing in shoot wall I get the msg You shoot wall & cause heavy damage, but it doesn't lower the bullet count.
At the same time, there must be a better way to do the count other than the long line of if else if's that I put in.
But this method doesn't do anything all.
So in short, just what am I missing here? I know I did it wrong.

Alex
26 Oct 2011, 08:41
I wouldn't use flags at all here. Flags are for things that are "on" or "off", but the number of bullets in a gun is... a number.

So I would use an attribute instead. Go to the Attributes tab for the gun, and add a "bullets" attribute (integer). You can then change the number of bullets in the gun by modifying "gun.bullets" in an expression, so for example when you fire the gun, set:


gun.bullets = gun.bullets - 1

Weiand
26 Oct 2011, 23:51
Much thanks Alex. That did the trick to make the bullets countdown.

I kept poking around to get the rest of the desired effects. Not sure if I took the long way around & if there is a faster cleaner way to do so, but I did get all the results I wanted.

Basic concept is:
Pick up gun, has 6 bullets. Pick up Ammo that reloads 6 bullets. If gun is not empty, says doesn't need to be reloaded, if empty, does so.
player types shoot #object# gun fires, prnt msg saying did so & caused damage.
when gun is empty, does not show damage msg & instead of just the msg shoot gun, it gives msg to reload
if player typed in shoot self, gives a warning, then if done again, shoots player, msg of being dead, uses a bullet, pause game, then msg as alive
I had to set gun attrib to 7 to get the gun to actually fire 6 times, then give msg of empty

I made two cmds for shoot #object# & shoot self
when I type in shoot self, that works, but shoot me or shoot myself, treats it as a #object#
do I need to make serperate cmd's for me myself as well as self, or could that be done with a switch?

As said, I have gotten the desired result for objects & for self, if needed I can just make two more cmd's for me & my self

Here is how it is all coded:

<object name="Gun">
<inherit name="editor_object" />
<look>A very large .44 Magnum</look>
<take />
<takemsg>Now you feel like Dirty Harry</takemsg>
<dropmsg>You drop the gun to the floor with a loud thud</dropmsg>
<inventoryverbs>Look at; Drop</inventoryverbs>
<bullets type="int">7</bullets>
</object>

<object name="Box of Ammo Sm">
<inherit name="editor_object" />
<alias>Ammo</alias>
<look>A big box of ammo for a .44</look>
<take />
<takemsg>Thinking you will want to reload, you take the box of ammo</takemsg>
<dropmsg>You decide you don't need any more ammo and drop the box on the ground</dropmsg>
<inventoryverbs>Look at; Load; Drop</inventoryverbs>
<Load type="script">
if (GetBoolean(Gun, "empty")) {
msg ("You loaded the gun")
SetObjectFlagOff (Gun, "empty")
Gun.bullets = 7
}
else {
if (not GetBoolean(Gun, "empty")) {
msg ("Gun does not need to be reloaded")
}
}
</Load>

<command name="Shoot">
<pattern>shoot #object#</pattern>
<script>
Gun.bullets = Gun.bullets - 1
if (Gun.bullets = 0) {
SetObjectFlagOn (Gun, "empty")
msg ("You are out of bullets")
}
else {
if (GetBoolean(Gun, "empty")) {
msg ("You pull the trigger getting only a click. You need to reload the gun")
}
}
if (not GetBoolean(Gun, "empty")) {
msg ("You shoot \"" + object.name + "\"and cause heavy damage")
}
</script>
</command>

<command name="Shoot Self">
<pattern>shoot self</pattern>
<script>
if (not GetBoolean(player, "warn")) {
SetObjectFlagOn (player, "warn")
msg ("You don't really want to shoot yourself do you?")
}
else {
if (GetBoolean(player, "warn")) {
msg ("You shot yourself and are dead")
Pause (10)
msg ("Lucky you! You came back to life")
}
}
Gun.bullets = Gun.bullets - 1
</script>
</command>

Or did I take the long road to get it to do what I wanted?

EDIT: Not sure why I didn't remember... On the Shoot Self cmd I added shoot self; shoot me; shoot myself & that took care of that part
guess I just overlooked that at the time

Pertex
27 Oct 2011, 07:25
You are on a good way, but you have one Problem. You can shoot yourself without bullets in your gun :D

You could combine the two shoot-commands something like


if (object="me" or object="self" or object="myself") {
... code of shot self command
} else {
... code of shoot comand
}




if (not GetBoolean(player, "warn")) {
SetObjectFlagOn (player, "warn")
msg ("You don't really want to shoot yourself do you?")
}
else {
if (GetBoolean(player, "warn")) { <------you dont need this if
msg ("You shot yourself and are dead")
Pause (10)
msg ("Lucky you! You came back to life")
}
}
Gun.bullets = Gun.bullets - 1


You are often using something like


else {
if


if within an else. Why not using "else if" ? With it you dont need so much layers.

Weiand
27 Oct 2011, 15:57
Pertex wrote:You are on a good way, but you have one Problem. You can shoot yourself without bullets in your gun :D

You could combine the two shoot-commands something like


if (object="me" or object="self" or object="myself") {
... code of shot self command
} else {
... code of shoot comand
}



Good point. I didn't notice that. I'll have to figure out how to implement that.

[quote]
if within an else. Why not using "else if" ? With it you dont need so much layers.

[/quote]

Mainly, I use Else because it seems when selecting Else if from the UI the program tends to lock up more often
than if selecting the Else then adding the if. So to save the frustration of it locking up on me & having to start that section all over
I have been using the Else

Weiand
31 Oct 2011, 02:09
I now have 3 different "shoot" cmd's

when first picking up gun, the shoot #obj# works perfect
The shoot self cmd works (still will work if out of bullets)
but after using the 3rd shoot bum cmd, the bullet countdown no longer works


<command name="Shoot">
<pattern>shoot #object#</pattern>
<script>
Gun.bullets = Gun.bullets - 1
if (Gun.bullets = 0) {
SetObjectFlagOn (Gun, "empty")
msg ("You are out of bullets")
}
else {
if (GetBoolean(Gun, "empty")) {
msg ("You pull the trigger getting only a click. You need to reload the gun")
}
}
if (not GetBoolean(Gun, "empty")) {
msg ("You shoot \"" + object.name + "\"and cause heavy damage")
}
</script>
</command>


<command name="Shoot Self">
<pattern>shoot self; shoot me; shoot myself</pattern>
<script>
if (not GetBoolean(player, "warn")) {
SetObjectFlagOn (player, "warn")
msg ("You don't really want to shoot yourself do you?")
}
else {
if (GetBoolean(player, "warn")) {
msg ("You shot yourself and are dead")
Pause (10)
msg ("Lucky you! You came back to life")
}
}
Gun.bullets = Gun.bullets - 1
</script>
</command>


<command name="Shoot Bum">
<pattern>shoot Bum; shoot bum</pattern>
<script>
if (GetBoolean(Bum, "dead")) {
msg ("The bum is already dead, no need to waste bullets")
}
if (GetBoolean(Gun, "empty")) {
msg ("You are out of bullets!!!!")
}
else {
if (not GetBoolean(Gun, "empty")) {
SetObjectFlagOn (Bum, "dead")
msg ("You shoot the bum with your mighty .44 blowing a large hole in him. He drops to the floor in a heap")
Gun.bullets = - 1
}
}
</script>
</command>

With the 3rd, when Bum has flag dead, shooting again gives the damage & the already dead msg.

I'm not sure why the shoot #obj# cmd stops counting down bullets after shooting bum.
After that, gun will shoot infinate times & not empty

Alex
01 Nov 2011, 09:35
Your "shoot bum" is a separate command to "shoot #object#", and you're only decreasing the bullets in "shoot #object#". When the player types "shoot bum", none of that "shoot #object#" script is triggered, because the "shoot bum" script is handling it instead.

You should put all three of these in the same command, "shoot #object#" should be the parent for everything. Then you can use something like this:


Gun.bullets = Gun.bullets - 1
if (Gun.bullets = 0) {
SetObjectFlagOn (Gun, "empty")
msg ("You are out of bullets")
}
else {
if (object = player) {
// shoot self, shoot me etc. will all be handled here automatically, as they're all valid names for the player object
}
else if (object = bum) {
// shoot bum will be handled here
}
else {
// shoot any other object will be handled here
}
}