Game Development Community

Energy as ammo?

by Toks · in Torque Game Engine · 08/09/2007 (3:12 am) · 12 replies

Hey,

is there a way already built in to use the players energy as ammo? for now i have a nasty looking construction to check if we have enough energy when we want to fire. is there a way i could use the players energy in quite the same way as an ammo object?

#1
08/09/2007 (3:37 am)
Yep, all this is built into the default player/weapon controllers.

From the top of my head:
  • You'll need to define these values in your weapon image datablock:
  • usesEnergy          = true;
       minEnergy           = 3;
       cutOffEnergy        = 3.1;
  • Then in your onFire state:
  • stateEnergyDrain[4]              = 9;
That should just about do it, too easy.
#2
08/09/2007 (7:18 am)
When i use this, stateTransitionOnAmmo does not transition when the players energy level increases to have enough energy again. should i use another syntax for transitionOn(No)Ammo or is this simply not supported for weapons that use energy?
#3
08/09/2007 (5:36 pm)
Yeah the weapon isn't actually using ammo or the inventory system so these callbacks aren't supported.

Another approach you could take which would offer you more flexibility would be to implement this resource: Generic Bar Hud Control

With some simple scripting you can hook your weapons up to the generic bar, it's what I do in my game. You could still use the ammo system, so you have all the ammo related functions/callbacks at your disposal however display it in the form of a progress bar - same thing as an energy bar really.
#4
08/10/2007 (12:48 am)
What i did now, is in the stateTransitionOnTriggerDown in ready i didnt go to "Fire" but to a new "CheckEnergy". this then calls a script which checks if the players energy level is higher than the energy value defined in the image. if so, it sets the ammostate to true, if not it sets to false. then i have a new stateTransitionOnAmmo to fire, and a stateTransitionOnNoAmmo to ready (so we can try to fire again and well just check if the energy is high enough since i cant use a stateTransitionOnAmmo from a "NoAmmo" state (which i deleted)).

it works, but its a bit of an ugly way of dealing with this. maybe ill have to hardcode a neater way.

edit: Nice resource though :) however, i dont quite understand how would you hook your weapons into the bar? like the same way i did with my energybar really, using getEnergyLevel and setEnergyLevel?
#5
08/10/2007 (1:05 am)
You basically do something like this:
  • Client:
  • //-----------------------------------------------------------------------------
    // Ammo Bar Hud (Client)
    //-----------------------------------------------------------------------------
    function clientCmdSetAmmoBarHud(%amount, %maxInv)
    {
       if (%amount == -1)
          AmmoBar.setValue(0);
       else
       {
          AmmoBar.setMinValue(0);
          AmmoBar.setMaxValue(%maxInv);
          AmmoBar.setValue(%amount);
          %threshold = 25; // desired pulse threshold as a percentage
          %pulseThresh = %maxInv / 100 * %threshold;
          AmmoBar.setPulseThreshold(%pulseThresh);
       }
    }
    
    function clientCmdResetAmmoBarHud(%maxInv)
    {
       AmmoBar.setValue(%maxInv);
    }
    
    function clientCmdDecreaseAmmoBar(%value)
    {
       ammoBar.drain(%value);
    }
    //-----------------------------------------------------------------------------
  • Server:
  • //-----------------------------------------------------------------------------
    // Ammo Bar Hud (Server)
    //-----------------------------------------------------------------------------
    function GameConnection::setAmmoBarHud(%client, %amount, %maxInv)
    {
       commandToClient(%client, 'setAmmoBarHud', %amount, %maxInv);
    }
    //-----------------------------------------------------------------------------
  • Within your onFire method:
  • // Decrease the ammo bar
       CommandToClient(%obj.client, 'DecreaseAmmoBar', 1);
  • Within your reload method:
  • %obj.client.setAmmoBarHud(%obj.getInventory(%this.ammo), %this.ammo.maxInventory);
  • Within your onMount method:
  • // Initial ammo state
       if (%obj.getInventory(%this.ammo))
       {
          %obj.setImageAmmo(%slot, true);
          %obj.client.setAmmoBarHud(%obj.getInventory(%this.ammo), %this.ammo.maxInventory);
       }
       else
       {
          %obj.client.setAmmoBarHud(-1);
       }
    function WeaponImage::onUnmount(%this,%obj,%slot)
    {
       %obj.client.setAmmoBarHud(-1);
       %obj.setArmThread(looknw);
    
    }
  • Within your onPickup method:
  • commandToClient(%shape.client, 'resetAmmoBarHud', %this.image.ammo.maxInventory);

    And anywhere else you need to update ammo.
    #6
    08/10/2007 (1:49 am)
    Here's a video that shows the ammo bar in action, I think it's quite effective especially considering how easy it was to hook up.

    AmmoBar.exe (Bink Video 13.3 MB)

    The video is low quality to keep the file size down however you should be able to see what's going on, the ammo bar is the yellow one at the bottom left hand corner of the screen. It updates under all circumstances (draining ammo, switching weapons, changing weapon modes, throwing weapons, picking up weapons, picking up ammo etc).
    #7
    08/10/2007 (12:45 pm)
    Video quality is good, and i apreciate your help very much, still think some core changes in the statemachine will be better for my purposes. what im looking for is really an onEnergy and onNoEnergy state in the machine, which shouldnt be too hard to write. if i do, ill post it here, or resource it and put up a link here. might take some time untill I find it in my heart to look into it :)
    #8
    08/10/2007 (1:15 pm)
    Actually, i started looking into it a bit already (im too tired to do something useful though)
    i found something interesting:

    void ShapeBase::updateImageState(U32 imageSlot,F32 dt)
    {
    if (!mMountedImageList[imageSlot].dataBlock)
    return;
    MountedImage& image = mMountedImageList[imageSlot];
    ShapeBaseImageData& imageData = *image.dataBlock;
    ShapeBaseImageData::StateData& stateData = *image.state;
    image.delayTime -= dt;

    // Energy management
    if (imageData.usesEnergy) {
    F32 newEnergy = getEnergyLevel() - stateData.energyDrain * dt;
    if (newEnergy < 0)
    newEnergy = 0;
    setEnergyLevel(newEnergy);

    if (!isGhost()) {
    bool ammo = newEnergy > imageData.minEnergy;
    if (ammo != image.ammo) {
    setMaskBits(ImageMaskN << imageSlot);
    image.ammo = ammo;
    }
    }
    }
    }

    this is the start of the updateImageState function in shapeimage.cc, it looks like it (in the part that I pasted here) drains the energy according to the state and then it sets the ammo boolean, which if im correct is the ammoState really, to true or false regarding the energy we have and the minEnergy defined in the image.

    so im left in the dark why I cant use the stateTransitionOn(No)Ammo, any educated help here please?

    EDIT: also wondering, by the way, why we dont update the ammoState in the same place for an image that has usesEnergy set to true and false.
    #9
    08/11/2007 (9:26 pm)
    Pretty cool clip Tim. I enjoyed watching it.


    (looks like the nade launcher still has an issue.. where the nade is fired from)


    cool though!
    #10
    08/11/2007 (9:39 pm)
    Thanks!

    Yep, the projectile fires from the player's eye node as opposed to the muzzle point node, like it should, go figure.

    It was part of a weapon pack that I was working on a long time ago, unfortunately it has ended up as a dead project. There's another video of it here and although it is outdated it does show more of the weapons and features that I was working on.

    [li]Download Video Here (93 MB)
    #11
    08/12/2007 (3:20 am)
    Very neat :) all not energy based though :P
    #12
    08/12/2007 (10:21 am)
    Very nice. You do very good work Tim.