Game Development Community

Need a little coder help

by Tom Timothy · in Torque 3D Professional · 05/12/2012 (12:39 pm) · 11 replies

I am trying to access the energy bar and make it so while in a car the energy decreases. And add items that player picks up for more energy haven't seen any resources on something like this. Pretty please guys sure one of you can type something like this up in short order.

#1
05/12/2012 (2:09 pm)
In the stock code the health/energy bar found in engine/source/T3D/fps/guiHealthBarHud.cpp is coded to require a connection and a Player as a control object:
void GuiHealthBarHud::onRender(Point2I offset, const RectI &updateRect)
{
   // Must have a connection and player control object
   GameConnection* conn = GameConnection::getConnectionToServer();
   if (!conn)
      return;
   ShapeBase* control = dynamic_cast<ShapeBase*>(conn->getControlObject());
   if (!control || !(control->getTypeMask() & PlayerObjectType))
      return;

// rest of function...
}
This is easily changed to allow vehicles, or any Shapebase object you can control for that matter.
if (!control || !(control->getTypeMask() & PlayerObjectType) || !(control->getTypeMask() & VehicleObjectType))
Recompile and you should be good to go.

#2
05/14/2012 (11:29 am)
If you want simultaneous display energy status eg pet and player , car and player or party list then you should create new HUBs with a name you like and a little script code for easier way (in update health or energy hubs, scripts/server/player.cs)

eg server-> player.cs

function Player::updateEnergy(%player)
{
//echo("c4Player::updateEnergy() -> Player energy changed, updating HUD!");

// Calcualte player energy
%maxEnergy = %player.getDatablock().maxEnergy;
%energyLevel = %player.getEnergyLevel();
%curEnergy = %maxEnergy - %energyLevel;
%curEnergy = mceil(%curEnergy);

// will Update the numericalEnergy HUD.
commandToClient(%player.client, 'setNumericalEnergyHUD', %curEnergy, %maxEnergy);
}

client -> client.cs

function clientCmdSetNumericalEnergyHUD(%curEnergy, %maxEnergy)
{
// Skip if the hud is missing.
if (!isObject(numericalEnergyHUD))
return;

// The server has sent us our current energy, display it on the HUD
%energyOSD = %curEnergy @ "/" @ %maxEnergy;
numericalEnergyHUD.setValue(%energyOSD);
numericalEnergyHUD1.setValue(%energyOSD);

// Ensure the HUD is set to visible while we have energy / are alive
if (%curEnergy)
HealthHUD.setVisible(true);
else
HealthHUD.setVisible(false);
}
#3
05/14/2012 (12:03 pm)
Dimitris' suggestion would certain work for the scripted HealthHud -- I almost set it up that way originally...

I actually submitted a Resource for a numerical healthBarHud control (in C++), that performs better than the scripted version, but it would still require the one line change from my previous post to allow for usage with vehicles.

I also recall a Resource that allowed the healthbar control to used in conjunction with any number of objects at once -- you'd have to search for it though.
#4
05/15/2012 (1:42 am)
Michael, those stated it is important, because the MMO needs multiple imaging Hubs present simultaneously. Temporarity I did script but will be much better to go in engine side.
I tried it on a table but was not the result I needed. Course probably somewhere to bore the engine with my code I wrote.

Michael well not to open a new post, a question if you know how to approach this. I did the mounts actively with health, skills etc.
Do not use the code for car or another. The only problem is the plateau in the level or the slope of the ground during the movement of the object. eg the horse is running on a hillside.
Know if there is something for the player's side?
#5
05/16/2012 (6:20 pm)
@Dimitris: Here is a healthBarHud Resource that adds the ability to set a healthbar to a specific object, allowing you to have multiple health/energy status Huds on the screen at once. It would need to be ported for use in Torque 3D, but that looks like it would only need a few simple changes, mostly converting openGl rendering to GFX. We would need to know about how your horse object/class is implemented before saying how to resolve that particular issue. If derived as a vehicle, it should align to the average slope of the ground it is standing on.
#6
02/11/2013 (11:42 pm)
@Michael: I added your change in post #1 above to both guiHealthTextHud.cpp and GuiHealthBarHud to enable their use with vehicles. For some reason, it works fine without the change but with it neither works - any ideas?
#7
02/12/2013 (8:17 am)
Sorry to continue with this, but it makes no sense... I installed a plain-vanilla 1.2, added a healthbarhud and ran the game, the bar displays health just fine. Did this mod, and it won't render the bar...

FWIW here's the code:

void GuiHealthBarHud::onRender(Point2I offset, const RectI &updateRect)
{
   // Must have a connection and player control object
   GameConnection* conn = GameConnection::getConnectionToServer();
   if (!conn)
      return;
   ShapeBase* control = dynamic_cast<ShapeBase*>(conn->getControlObject());
   if (!control || !(control->getTypeMask() & PlayerObjectType) || !(control->getTypeMask() & VehicleObjectType))
      return;

   if(mDisplayEnergy)
   {
      mValue = control->getEnergyValue();
   }
   else
   {
      // We'll just grab the damage right off the control object.
      // Damage value 0 = no damage.
      mValue = 1 - control->getDamageValue();
   }


   // Background first
   if (mShowFill)
      GFX->getDrawUtil()->drawRectFill(updateRect, mFillColor);

   // Pulse the damage fill if it's below the threshold
   if (mPulseRate != 0)
      if (mValue < mPulseThreshold) 
      {
         U32 time = Platform::getVirtualMilliseconds();
         F32 alpha = 2.0f * F32(time % mPulseRate) / F32(mPulseRate);
         mDamageFillColor.alpha = (alpha > 1.0f)? 2.0f - alpha: alpha;
      }
      else
         mDamageFillColor.alpha = 1;

   // Render damage fill %
   RectI rect(updateRect);
   if(getWidth() > getHeight())
      rect.extent.x = (S32)(rect.extent.x * mValue);
   else
   {
      S32 bottomY = rect.point.y + rect.extent.y;
      rect.extent.y = (S32)(rect.extent.y * mValue);
      rect.point.y = bottomY - rect.extent.y;
   }
   GFX->getDrawUtil()->drawRectFill(rect, mDamageFillColor);

   // Border last
   if (mShowFrame)
      GFX->getDrawUtil()->drawRect(updateRect, mFrameColor);
}
#8
02/12/2013 (11:11 am)
Hey Gibby, change the line where it checks for the player control object to:
if (!control || !(control->getTypeMask() & PlayerObjectType | VehicleObjectType))
#9
02/13/2013 (8:34 am)
@Michael: Thank You! Works perfectly for the player. I want to confirm, does this gui always read from the control object, i.e. when I mount a vehicle should it then read the mounted object? Or do I still need to hook the gui to the vehicle in script???
#10
02/13/2013 (11:16 am)
It always checks for a control object and whether that control object is a PlayerObjectType. The modified logic there just includes a VehicleObjectType into that condition. So which ever object is the current control object should be the one that is indicated by the GuiControl.

Maybe not suitable for all game types... so a purpose built control that could be hooked to any object through script would possibly be more flexible.
#11
02/15/2013 (7:35 am)
@Michael:

What's odd is, when I tried to setup this gui for vehicles only, like this:

if (!control || !(control->getTypeMask() & VehicleObjectType))

...it still wouldn't read health/energy from the vehicle. I've tried with MIT2.0, stock 1.2, as well as my own project. To verify, I added this to the Player::onMount:
%control = %obj.getControlObject();
   echo("player.cs->Armor::onMount new control object for " @ %obj @ " is  " @ %control@" a: "@%control.getClassName());

...and get this back in the console, so I'm sure the control object is being properly re-assigned on vehicle mounting:

player.cs->Armor::onMount new control object for 4902 is 4837 a: HoverVehicle

...note I get the same issue using guiHealthBarHud and guiNumericalHealthHud, so I wonder if there isn't some other issue there somewhere...