(SOLVED) Scripting a firearm

For the past few days, I've been trying to implement a firearm into my game. I've looked on the forums several times, but can't find any replies or tutorials that solve my problem. Here's what I'm trying to do:

The gun begins the game loaded with 6 bullets (a full mag of 5 and one in the chamber). I want the player to be able to fire the gun randomly into the air with the "fire" command, thus losing a bullet, or shoot a zombie/animal and also lose a bullet. There's only one gun, so no need for equip commands. The player will automatically re-load the gun if he has a mag with him. The guns do not do damage, they just kill in a shot.

I want to be able to script the gun, not mess around with the code.

Here's what the attempted script for when the player types the command "fire gun" currently looks like:

If: Player is carrying object - object - gun
then:
If object attribute equals - object - gun - attribute: clip = gun.clip > 0
then:
set variable: gun.clip = expression gun.clip - 1
Print message: You fire the gun

else:
Print message: You fire the gun

If it helps at all, this is the message I receive in game when I attempt to fire the gun:
Error running script: Error compiling expression 'gun.clip = gun.clip > 0': CompareElement: Operation 'GreaterThan' is not defined for types 'Boolean' and 'Int32'

Any help you can give me will be very much appreciated :)


The problem with script, as opposed to code, is it is hard to show in text. The problem is this line:

If object attribute equals - object - gun - attribute: clip = gun.clip > 0

This is for testing if one thing is equal to another. You want to know if it is greater. Try instead:

if [expression] gun.clip > 0

Also, that final else should presumably say that you cannot fire the gun?


Ah yes, sorry. The final else is supposed to say "the gun clicks uselessly in your hand." My bad. I was trying to re-create. Yes, as you point out, scripts are difficult to re-produce by text :( I'd include a screenshot, but not sure how.

I made the change you suggested. It's still printing the same message.


what type of Attribute is your 'gun' Object's 'clip' Attribute? (boolean, string, int/integer, double, object, etc ???):

click on your 'gun' Object on the left side's "tree of stuff", then click on the 'Attributes' Tab on the right side, then look at the bottom 'Attributes' box, finding your 'clip' Attribute, clicking on it, so it is highlighted/choosen, then look at what type the drop down box is for it, if it's not an 'int/integer', you need to change it to being an 'int/integer'.


coding/scripting (whether done through the GUI/Editor or directly in-code), requires that your names must match up (you must be a grammer/spelling/typo nazi, coding/scripting requires you to be 100% error free) and for your Attribute/Data types to match up.

the reason is that it's not possible (for both you/us/humans and computers alike) to do this:

(addition):
"red" + 7 = ?HUH? ERROR!!!!
(string: "red" + int: 7)

(addition):
"dead" + 7 = ?HUH? ERROR!!!!!
(boolean: dead: false/true + int: 7)


// not the most efficient code, but it should be easier to understand/follow (I hope, lol)
// (just an example, for you to take a look at)

<object name="gun">
  <attr name="clip" type="int">6</attr>
  <attr name="reserve_clip" type="int">50</attr>
  <attr name="displayverbs" type="listextend">fire</attr>
  <attr name="inventoryverbs" type="listextend">fire</attr>
  <attr name="fire" type="script"><![CDATA[
    if (gun.clip > 0) {
      msg ("you fire a shot")
      gun.clip = gun.clip - 1
    } else {
      if (gun.reserve_clip > 0) {
        msg ("you reload your gun")
        if (gun.reserve_clip > 5) {
          gun.clip = 6
          gun.reserve_clip = gun.reserve_clip - 6
        } else {
          gun.clip = gun.reserve_clip
          gun.reserve_clip = 0
        }
      } else {
        msg ("You need more clips, as both your gun's clips and your reserve clips, are depleted")
      }
    }
  ]]></attr>
</object>

<verb>
  <property>fire</property>
  <pattern>fire</pattern>
  <defaultexpression>You can't fire that!</defaultexpression>
</verb>

Here's a tutorial I made https://www.youtube.com/watch?v=yiQRfCjl1Lg&t=706s with files attached in the description on the video. All the video is for is to show you how to use the weapons, ammo and combat, also it shows you how to equip weapon so you can only attack with weapon equipped.

Hope it helps you
Mike


Hi onimike. I actually downloaded your gun file from your reply to a similar post :) It's an interesting system, and I might use it in a later game, but it's not quite what I'm looking for. Thanks for helping :)

hegemonkhan, I copied and pasted your script. That's almost exactly what I was looking for :D

Thank you to everyone who helped. I'm grateful for all of your efforts, and for your patience (especially as this topic has been posted elsewhere). Enjoy making games :D


Thank you guys so much! I just copied hegemonkhan's code into my game, fiddled around with the script a bit and the system I wanted to implement is now exactly as I had intended. I'm so pleased :) Now, how do I set this post as solved?


I think you edit your first post, and there's a text box for changing your post's title, which you then manually add-type-in: [SOLVED], in front of your post's title:

post title text box: [SOLVED] Scripting a firearm


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums