Game Development Community

Timing a user event in scripts

by Name · in Torque Game Engine · 12/21/2004 (3:28 am) · 11 replies

Hey all,

Long time reader, first time poster... (heh), ok to the point, I have been working the torque engine for about a year now and have been very involved with editing and working within the engine code. This is all fine and dandy as I understand C++ quite well and this engine is so easy to work with =P. HOWEVER my current task involves implementing some scripts, which I officialy suck at. so here it is, a post for help.

I am creating a weapon (not really a weapon... does no damage) which pushes the object it "shoots" this is all easy enough... and works fine BUT this weapon is "powered up" by the user holding down the Left mouse button, the longer he/she holds it the more powerfull the push... and on release it fires... the way its set now it fires on push and always at a constant value of force that I set in the ammo properties. So here is my question(s):

1: How can I time the amount of time the mouse key is held down?

2: How can I have the weapon fire on release and not press?

Thanks for you help.

Chad.

#1
12/21/2004 (3:38 am)
Quick question. Do you need to do it by time? Or could you do it by holding the mouse down till it reaches a certain power.

I ask because it makes a difference on how to answer the question
#2
12/21/2004 (3:38 am)
2) You would want to have your TSCtrl.cc implement/expose the onMouseUp(...) event to the console. In this case, it's kind of a reverse exposure--instead of allowing the console to call engine methods, you want your engine code to call a console method. Examples of this (as well as how onMouseUp(...) is actually implemented--stock does it a lot in the editor code) are all over the code base, and are done via the Con::executef(...); call.

Basically, in your TSCTrl.cc/h you want to be able to process a mouseUp event passed from the platform, and further pass it to your scripting console by doing the Con::executef(..."onMouseUp",...); type of call.

EDIT: I had some code examples in this post, but this is a public forum. We can go into much more detail if you ask the question in a private (SDK Licensed) forum.
#3
12/21/2004 (3:47 am)
Oops... Didn't see this is Public
#4
12/21/2004 (3:55 am)
Yah this is public... sorry guys... and wow your fast... thanks for replys. If I continue to have any problems Ill repost in private.
#5
12/21/2004 (4:02 am)
Try use getsimtime() for how long time you pressed the button , and there is a stateTransitionOnTriggerUp use that for the state .

But you must change the state scripts ,because if you have the stateTransitionOnTriggerUp on the readystate, it will fire all the time until you press the mousedown.

Ops ! public area again :(
#6
12/21/2004 (4:05 am)
Public isn't the wrong place for this... script questions are fine.

What you can do is in script:
function doSomething(%val)
{
   if (%val)
      echo("Mouse button down so start your timer");
   else
      echo("Mouse button up so stop your timer");
}

GlobalMap.Bind(mouse, button1, doSomething);

That should do it.

- Brett
#7
12/21/2004 (4:36 am)
@Brett: Except that I was pulling out code references from player.cc, hehe. Too early in the morning, glad I caught it.

That's a good implementation by the way--I had completely forgotten that at the input/bind level you had awareness of the basic mouse state.

My technique is more aimed at a different use of mouse inputs in general, and I adapted to the original question--try out Brett's and it should do your trick with a lot less work.
#8
12/21/2004 (4:50 am)
I knew there was a simplified way to do it but I couldn't find the similar post.

This actually helps me with something i was working on also!
#9
12/21/2004 (10:20 am)
Simple charge-up..

datablock ShapeBaseImageData(ChargeWeaponImage)
{
   className = WeaponImage;
   shapeFile = "~/shapes/weapons/weapon.dts";
   computeCRC = true;
   emap = true;
   cloakable = true;
   mass = 5;

   mountPoint = 0;
   offset = "0 0 0";
   rotation = "1 0 0 0";
   eyeOffset = "0.1 0.2 -0.55";
   eyeRotation = "1 0 0 0";
   firstPerson = true;

   correctMuzzleVector = false;

   usesEnergy = true;
   //fireEnergy = 8; // This needs to not exist, conflicts with concept
   minEnergy = 3;
   maxCharge = 4; // Maxmimum damage modifier, this is multi by the proj damage value
   accuFire = false;

   lightType = "WeaponFireLight"; // NoLight, ConstantLight, PulsingLight, WeaponFireLight.
   lightColor = "0.800000 0.800000 0.800000 1.000000";
   lightTime = 1000; // Indicates the time when the light effect started.
   lightRadius = 8; // Extent of light.

   item = Weapon;
   projectile = SomeProj;
   projectileType = Projectile;

   stateName[0]                     = "Preactivate";
   stateTransitionOnLoaded[0]       = "Activate";
   stateTransitionOnNoAmmo[0]       = "NoAmmo";
   stateSound[0]                    = WeaponSwitchSound;

   stateName[1]                     = "Activate";
   stateTransitionOnTimeout[1]      = "Ready";
   stateTimeoutValue[1]             = 0.4;
   stateSequence[1]                 = "Activate";

   stateName[2]                     = "Ready";
   stateTransitionOnNoAmmo[2]       = "NoAmmo";
   stateTransitionOnTriggerDown[2]  = "Charge";

   stateName[3] = "Charge";
   stateScript[3] = "chargeStart";
   stateEnergyDrain[3] = 35;
   stateTransitionOnTriggerUp[3] = "Fire";

   stateName[4]                     = "Fire";
   stateTransitionOnTimeout[4]      = "Reload";
   stateTimeoutValue[4]             = 0.4;
   stateFire[4]                     = true;
   stateRecoil[4]                   = LightRecoil;
   stateAllowImageChange[4]         = false;
   stateSequence[4]                 = "Fire";
   stateScript[4]                   = "onFire";
   stateSound[4]                    = FireSound;

   stateName[5]                     = "Reload";
   stateTransitionOnNoAmmo[5]       = "NoAmmo";
   stateTransitionOnTimeout[5]      = "Ready";
   stateTimeoutValue[5]             = 1.0;
   stateAllowImageChange[5]         = false;
   stateSequence[5]                 = "Reload";

   stateName[6]                     = "NoAmmo";
   stateTransitionOnAmmo[6]         = "Reload";
   stateSequence[6]                 = "NoAmmo";
   stateTransitionOnTriggerDown[6]  = "DryFire";

   stateName[7]                     = "DryFire";
   stateTimeoutValue[7]             = 1.0;
   stateTransitionOnTimeout[7]      = "NoAmmo";
   stateSound[7]                    = DrySound;
};

//**************************************************************
// Functions
//**************************************************************

function ChargeWeaponImage::onFire(%data, %obj, %slot)
{
   %p = Parent::onFire(%data, %obj, %slot);
   %modifier = getSimTime() - %obj.startTime;
   %p.damageMod = %modifier / 1000;
   if(%p.damageMod > %data.maxCharge)
   	%p.damageMod = %data.maxCharge;
   
   if(%p.damageMod < 1)
   	%p.damageMod = 1;
}

function ChargeWeaponImage::chargeStart(%data, %obj, %slot)
{
   %obj.startTime = getSimTime();
}
#10
12/21/2004 (5:29 pm)
Wow again, thanks all, this was all very usefull and I appreciate it. Merry Christmas.
#11
05/01/2005 (10:40 pm)
This has been useful. I am making a jump mod, so the player can charge his jump visa vie Spiderman 2, so hopefully my current understanding of this kind of thing has been augmented. thanks.