Game Development Community

dev|Pro Game Development Curriculum

Change damage flash color in script

by Louis Dufresne · 06/16/2008 (6:22 am) · 2 comments

* Inside GameConnection.h *
find F32 mDamageFlash;
add under it:
F32 mDamageFlashRed;
F32 mDamageFlashGreen;
F32 mDamageFlashBlue;

find F32 getDamageFlash() { return mDamageFlash; }
add under it:
F32 getDamageFlashRed() { return mDamageFlashRed; }
F32 getDamageFlashGreen() { return mDamageFlashGreen; }
F32 getDamageFlashBlue() { return mDamageFlashBlue; }
void setDamageFlashColor(F32 red, F32 green, F32 blue);

* Inside GameConnection.cc *
find mDamageFlash = mWhiteOut = 0;
add under it:
mDamageFlashRed = mDamageFlashGreen = mDamageFlashBlue = 0;

find mDamageFlash = 0; inside the function void GameConnection::readPacket(BitStream *bstream)
add under it:
mDamageFlashRed = mDamageFlashGreen = mDamageFlashBlue = 0;

find if(bstream->readFlag()) right under that inside the same function
add inside the if statement:
if(bstream->readFlag())
    mDamageFlashRed = bstream->readFloat(7);
if(bstream->readFlag())
    mDamageFlashGreen = bstream->readFloat(7);
if(bstream->readFlag())
    mDamageFlashBlue = bstream->readFloat(7);

find F32 flash = mControlObject->getDamageFlash(); inside the function
void GameConnection::writePacket(BitStream *bstream, PacketNotify *note)
add under it:
F32 flashRed = mControlObject->getDamageFlashRed();
F32 flashGreen = mControlObject->getDamageFlashGreen();
F32 flashBlue = mControlObject->getDamageFlashBlue();

scroll down and find function F32 GameConnection::getBlackOut()
add this function underneath it:
void GameConnection::setDamageFlashColor(F32 red, F32 green, F32 blue)
{
	mDamageFlashRed = red;
	mDamageFlashGreen = green;
	mDamageFlashBlue = blue;
}

* Inside ShapeBase.h *
find F32 mDamageFlash;
add under it:
F32 mDamageFlashRed;
F32 mDamageFlashGreen;
F32 mDamageFlashBlue;

find virtual F32 getDamageFlash() const;
add under it:
virtual F32  getDamageFlashRed() const;
virtual F32  getDamageFlashGreen() const;
virtual F32  getDamageFlashBlue() const;

right under that find virtual void setDamageFlash(const F32 amt);
add under it:
virtual void setDamageFlashColor( const F32 red, const F32 green, const F32 blue);

* Inside ShapeBase.cc *
find mDamageFlash = 0.0; inside function ShapeBase::ShapeBase()
add under it:
mDamageFlashRed = mDamageFlashGreen = mDamageFlashBlue = 0.0;

find function F32 ShapeBase::getDamageFlash() const
add these functions under it:
F32 ShapeBase::getDamageFlashRed() const
{
   return mDamageFlashRed;
}
F32 ShapeBase::getDamageFlashGreen() const
{
   return mDamageFlashGreen;
}
F32 ShapeBase::getDamageFlashBlue() const
{
   return mDamageFlashBlue;
}

find function void ShapeBase::setDamageFlash(const F32 flash) right under it
add function underneath it:
void ShapeBase::setDamageFlashColor(const F32 red, const F32 green, const F32 blue)
{
	mDamageFlashRed = red;
	if (mDamageFlashRed < 0.0)
      mDamageFlashRed = 0;
   else if (mDamageFlashRed > 1.0)
      mDamageFlashRed = 1.0;

	mDamageFlashGreen = green;
	if (mDamageFlashGreen < 0.0)
      mDamageFlashGreen = 0;
   else if (mDamageFlashGreen > 1.0)
      mDamageFlashGreen = 1.0;

	mDamageFlashBlue = blue;
	if (mDamageFlashBlue < 0.0)
      mDamageFlashBlue = 0;
   else if (mDamageFlashBlue > 1.0)
      mDamageFlashBlue = 1.0;
}

now scroll all the way down and find function
ConsoleMethod( ShapeBase, setDamageFlash, void, 3, 3, "(float lvl)")
add function under it:
ConsoleMethod( ShapeBase, setDamageFlashColor, void, 5, 5, "(float red, float green, float blue)")
{
   F32 flashRed = dAtof(argv[2]);
   F32 flashGreen = dAtof(argv[3]);
   F32 flashBlue = dAtof(argv[4]);
   if (object->isServerObject())
      object->setDamageFlashColor(flashRed, flashGreen, flashBlue);
}

find function ConsoleMethod( ShapeBase, getDamageFlash, F32, 2, 2, "") right under there
add the functions:
ConsoleMethod( ShapeBase, getDamageFlashRed, F32, 2, 2, "")
{
   return object->getDamageFlashRed();
}
ConsoleMethod( ShapeBase, getDamageFlashGreen, F32, 2, 2, "")
{
   return object->getDamageFlashGreen();
}
ConsoleMethod( ShapeBase, getDamageFlashBlue, F32, 2, 2, "")
{
   return object->getDamageFlashBlue();
}

* Inside Game.cc *
find F32 damageFlash = 0; inside function void GameRenderFilters(const CameraQuery& camq)
add under it:
F32 damageFlashRed = 0;
F32 damageFlashGreen = 0;
F32 damageFlashBlue = 0;

right under that find if(connection)
add inside the if statement:
damageFlashRed = connection->getDamageFlashRed();
damageFlashGreen = connection->getDamageFlashGreen();
damageFlashBlue = connection->getDamageFlashBlue();

inside the same function scroll down and find glColor4f(0.3, 0.1, 0, damageFlash);
I commented this out and added:
glColor4f(damageFlashRed, damageFlashGreen, damageFlashBlue, damageFlash);

Recompile the engine here and make sure everything compiled correctly. I advise you to maybe compile each file individually after you make an edit to it. If you get any linker errors like I did, then you may have skipped a part. This happened when I added the virtual functions inside ShapeBase.h but forgot to add the actual functions inside ShapeBase.cc

Now for script.

* Inside Player.cs *
find the function Armor::onDamage and right above %obj.setDamageFlash(%flash);
add:
%obj.setDamageFlashColor(1.0, 0.0, 0.0);
This makes the damageflash red, but change the colors around and see how it now changes the damage flash from script! Now you don't have to recompile the engine each time you want to change the damage flash color.

Lastly delete the .dso file for player.cs and have fun!

#1
06/16/2008 (1:30 pm)
Nice, very similar to mine. I would recommend using a single ColorF instead of three separate floats, but it works either way.
Also, as you're using floats, I would recommend using mClampF to clamp the values. Does the same job, but less fiddly code and a good habit to get in to.

mDamageFlashRed = mClampF(red, 0.f, 1.f);
	mDamageFlashGreen = mClampF(green, 0.f, 1.f);
	mDamageFlashBlue = mClampF(blue, 0.f, 1.f);
#2
06/18/2008 (10:58 am)
Thanks a bunch Louis, I've taken what you have provided on step further in this resource. Maybe someone else may find it useful: Click Here