bayonet

Topics regarding Scripting with Reality Factory
Post Reply
whatwhat89
Posts: 17
Joined: Sun Feb 18, 2007 1:29 am

bayonet

Post by whatwhat89 »

How can a make a scripted weapon that when you hit the right mouse button the bayonet thrusts?
Pain is only temporary.
User avatar
zany_001
Posts: 1047
Joined: Fri Mar 02, 2007 8:36 am
Location: Aotearoa

Post by zany_001 »

i cant remember exactly,but u could use onRightClick i think,and change the wepon to melee for just that click,then revert it,but i cant think of it right now.soz
Once I was sad, and I stopped being sad and was awesome instead.
True story.
User avatar
darksmaster923
Posts: 1857
Joined: Wed Jan 03, 2007 10:32 pm
Location: Huntington Beach, California, USA

Post by darksmaster923 »

i assume you know how to make scripted weapons in the first place.
you have to have if(self.rbutton_pressed = true)
then add Animate(STAB);
then just do a fire projectile with a life time of like 0.001.
Herp derp.
whatwhat89
Posts: 17
Joined: Sun Feb 18, 2007 1:29 am

Post by whatwhat89 »

I'm not quite sure how to make a scripted weapon.
Pain is only temporary.
User avatar
Juutis
Posts: 1511
Joined: Thu Jan 12, 2006 12:46 pm
Location: Finland

Post by Juutis »

How can a make a scripted weapon that when you hit the right mouse button the bayonet thrusts?
I'm afraid it's not quite that simple. You can't add custom actions to the built-in weapon system with scripting or anything. You would have to script your own weapon system and only then you would be able to do this kind of special actions.

I don't recommend trying to script your own weapons unless you are very familiar with the scripting language. But if you really want to do this, I could try to write a little explanation about scripted weapons.
Pain is only psychological.
whatwhat89
Posts: 17
Joined: Sun Feb 18, 2007 1:29 am

Post by whatwhat89 »

If you would that would be great.
Pain is only temporary.
User avatar
Juutis
Posts: 1511
Joined: Thu Jan 12, 2006 12:46 pm
Location: Finland

Post by Juutis »

Sorry for being so slow... Anyway, here goes!

I assume you want a first person weapon. I've never attempted to script a 3rd person weapon, but I think it would be a lot more complicated. You would probably have to have a scripted player too.

First of all, some things you need to know:
"Scripted weapons" are nothing else than pawns. The engine treats them just like it treats your NPCs and other pawns, you can't tell it "this is a scripted weapon, make it shoot". The actor of the pawn acts as the visible weapon and the script controls its behaviour.

So, you will need a weapon model. Preferably one with some detail and the hands that are usually visible in first person weapons. It should have at least two basic animations, idle and shoot. Later on, you can make it nicer with reload, arming, and other animations like that. Then import the model into RF and define a new pawn using the actor you created. Now we can get to the tricky part, writing the script. The script will be running in low level all the time.

Basically, what make scripted weapons possible, are the cool scripting commands that RF offers:
Animate() lets you play an animation with a pawn. A very basic command but soooooo useful!
PositionToPlayer() makes the pawn move with the built-in player. Another basic command which we will use.
FireProjectile() allows the pawn to shoot projectiles. Without this projectile weapons would be hard to script.
self.camera_pitch and self.player_yaw contain the information about the camera rotation. With these it's possible to make the pawn rotate with the camera so that it looks like it's attached to the camera.

Here's a very very very basic script:

Code: Select all

{

IDLE      [gun_idle]
SHOOT  [gun_shoot]

OFFSET_Y     [75]

FIRERATE      [0.5]
PROJECTILE  [bullet]
FIREBONE     [firebone]
DAMAGEATTRIBUTE  [health]


firetimer [0]

Spawn[ ()
{
Gravity(false); // we don't want the gravity to affect this pawn
SetNoCollision(); // Don't collide with anything

LowLevel("setup_lowlevel");   // go to low level
} ]

setup_lowlevel[ ()
{
self.ThinkTime = 0;

self.yaw_speed = 10000;  // make sure the pawn rotates fast enough
self.pitch_speed = 10000;

AnimateHold(IDLE);  // play the idle animation
self.think = "idle";  // start executing "idle" order
} ]


idle[ ()
{
self.ThinkTime = 0;  // execute this order every frame

if(self.animate_at_end)  // if animation has finished, play it again
{
     AnimateHold(IDLE);
}

PositionToPlayer(0,OFFSET_Y,0,false,false);  // move with the player

self.ideal_yaw = self.player_yaw;  // get the ideal rotation
self.ideal_pitch = self.camera_pitch;

ChangeYaw();    // rotate to match the rotation defined above
ChangePitch();

if(self.lbutton_pressed and (firetimer < self.time))  // if left mouse button is pressed and enough time has passed since the last shot
{
     firetimer = self.time + FIRERATE;  // set the delay to next shot
     FireProjectileBlind(PROJECTILE,FIREBONE,0,0,0,DAMAGEATTRIBUTE);  // shoot the projectile
     AnimateHold(SHOOT);  // play the shooting animation
}
} ]

}
That's a script that should make the pawn move with the player and rotate with the camera, and shoot with the left mouse button. I decided to define the idle animation and the Y-offset from the player origin in the start of the script along with some other variables. This way they're much easier to change later.
IDLE is the name of the idle animation. The pawn plays this when not shooting.
SHOOT is the name of the shooting animation.

OFFSET_Y is the offset from the player origin. In other words, how high the camera is measured from the floor. You will need to position the pawn exactly where the camera is. You can check how high the camera is by opening install\camera.ini. There should be a line "height = ..." under "FirstPerson". You should give OFFSET_Y in the script the same value.

FIRERATE is the delay in seconds between shots. 0.5 means that the pawn will shoot 2 times a second if the left button is held down.
PROJECTILE is the name of the projectile the pawn will shoot. Projectiles are defined in weapon.ini.
FIREBONE is the bone at which the projectile will be launched.
DAMAGEATTRIBUTE is the attribute that the projectile will damage.


Now add a pawn to your level with the pawntype you defined earlier, give it this script and see how it goes.

You will probably have to play around with different rotations in pawn.ini A LOT to align the pawn correctly. You can check if the pawn is even near where you want it to be in 3rd person view if it can't be seen in 1st person view. One way I found out to work was to give the pawn a rotation 0, 0, 0 in pawn.ini and then turn the actual model 180 degrees in MilkShape.


I hope you understand what's going on in the script and that this gets you started. As usual, feel free to ask anything about anything. :)
Pain is only psychological.
Post Reply