Simple code request

Grab your favourite IDE and tinker with the innards of game engines

Simple code request

Postby TechieUK on Wed Dec 25, 2013 10:45 pm

Hey. :smt039
I'm currently learning animation and modelling I don't have the time to learn C++ properly, however there is some very simple (I hope) code I would like to try and implement.

Basically, I'm working with npc_citizen. My model has 6 different skins, listed below:
0 - Normal, 'clean' skin
1 - Damaged skin, some scratches here and there
2 - Wounded skin, with some more prominent blood
3 - Badly wounded skin, basically 2 with more blood
4 - Near-death skin, much bloodier
5 - Dead skin with dried blood, more dead-looking face

What I would like to do is implement a very simple feature. I've modified the citizen to have 100 health.

I would like to have the following code:
If health is 100, switch to skin 0
If health is between 99 and 75, switch to skin 1
If health is between 74 and 50, switch to skin 2
If health is between 49 and 25, switch to skin 3
If health is between 24 and 1, switch to skin 4
If health is zero/npc is dead, switch to skin 5

The reason for that implementation is if I heal the npc, the skin will go backwards. If I give enough health to an npc on skin 4, he switches back to skin 0, etc.

Would anyone here be kind enough to write out and explain this very simple code?

You don't need to write it all out if you do; the only code I want to know is:
If health is between a and b, switch to skin c
So I can change values a, b and c to get the results I want. I assume the code would just be one line, but I'm probably wrong- I don't know C++.

If you can do this for me, or at least point me in the right direction, it would be excellent and very much appreciated.
Remember I have 0 C++ experience, so you will probably need to explain everything you say related to the code!
I will probably ask a lot of questions too. :smt023

-TechieUK (WILLAM)
Also know as WILLAM; TechieUK is my old name. If you see WILLAM on Steam or ModDB, it's me.
User avatar
TechieUK
Regular
Regular
 
Joined: Sat Apr 13, 2013 4:29 pm

Re: Simple code request

Postby stoopdapoop on Thu Dec 26, 2013 8:27 am

which codebase?
I'm Brown
Image
User avatar
stoopdapoop
Veteran
Veteran
 
Joined: Sun Aug 21, 2005 2:14 am
Location: Ann Arbor, MI

Re: Simple code request

Postby LordDz on Thu Dec 26, 2013 2:19 pm

I can currently not test this as the mod I have it for currently doesn't compile.
However, it should be in the Server Episodic/Source Files/Hl2 DLL/npc_citizen17.cpp file.
Image

Go to the function "int CNPC_Citizen::OnTakeDamage_Alive( const CTakeDamageInfo &info )"
Then just add this over the line "if( (info.GetDamageType() & DMG_BURN) && (info.GetDamageType() & DMG_DIRECT) )"
Code: Select all
if (GetHealth() >= 100)
   {
      m_nSkin = 0;
   }
   else if (GetHealth() <= 99 && GetHealth() >= 75)
   {
      m_nSkin = 1;
   }
   else if (GetHealth() <= 74 && GetHealth() >= 50)
   {
      m_nSkin = 2;
   }
   else if (GetHealth() <= 49 && GetHealth() >= 25)
   {
      m_nSkin = 3;
   }
   else if (GetHealth() <= 24 && GetHealth() >= 1)
   {
      m_nSkin = 4;
   }
   else
   {
      m_nSkin = 5;
   }


Then above the function itself, add
Code: Select all
int CNPC_Citizen::OnTakeDamage_Dying(const CTakeDamageInfo &info)
{
   m_nSkin = 5;
}

Image
Don't worry about the red errors, just go to npc_citizen17.h, scroll down to "// Damage handling" (CRTL + F to search) and add
Code: Select all
int            OnTakeDamage_Dying(const CTakeDamageInfo &info);

Image
You should probably change it to be using percentages instead of numbers, hopefully this will work. As said I have no way of currently testing it :)
User avatar
LordDz
May Contain Skills
May Contain Skills
 
Joined: Mon Sep 01, 2008 12:28 pm
Location: Hammer Crash Logs

Re: Simple code request

Postby TechieUK on Thu Dec 26, 2013 4:39 pm

I'll have to try it in a bit when I've redownloaded everything in my steam library. :cry:
I didn't expect to get a reply so fast so thanks for this - if it doesn't work for whatever reason I'll try and fix it myself. Maybe I'll figure out something about programming in C++ since right now I can only code python. I'll try and learn as soon as I wrap my head around uv mapping. I'm one of those people who likes to self-teach things like modelling but programming isn't really something you can just figure in my mind.
Also know as WILLAM; TechieUK is my old name. If you see WILLAM on Steam or ModDB, it's me.
User avatar
TechieUK
Regular
Regular
 
Joined: Sat Apr 13, 2013 4:29 pm

Re: Simple code request

Postby SM Sith Lord on Fri Dec 27, 2013 4:14 pm

Kudos to you LordDz for coding that up. I hope TechieUK knows how to compile. :D

Good luck to you on your quest of learning C++, TechieUK. If you already know any other language, it shouldn't be all that hard to pick up.
SM Sith Lord
Been Here A While
Been Here A While
 
Joined: Sat Nov 25, 2006 4:25 pm
Location: Los Angles, CA

Re: Simple code request

Postby TechieUK on Fri Dec 27, 2013 5:54 pm

Oh ya, I know how to compile and all that. I've been playing with some of the code on the VDC for a while but never really got round to figuring out how to write my own code. I can clone weapons and change their name though and once after a lot of searching and editing I was able to take the crowbar out completely and replace it with weapon_melee_sword. I have an HL2 mod where I dump all of the stuff I make but never get round to using like these (click for full size)

Image

Image

I also figured out how to change the default setting for console commands... At least, it works. It's probably not the best way to do it. I search the entire solution for the command and then change it from 0 to 1 or 1 to 0. :wink:

Edit:
src\game\server\hl2\npc_citizen17.cpp(2259) : error C4716: 'CNPC_Citizen::OnTakeDamage_Dying' : must return a value

I assume you would edit
Code: Select all
int CNPC_Citizen::OnTakeDamage_Dying(const CTakeDamageInfo &info)
{
   m_nSkin = 5;
}

to something along the lines of
Code: Select all
int CNPC_Citizen::OnTakeDamage_Dying(const CTakeDamageInfo &info)
{
   m_nSkin = 5;
   return BaseClass::OnTakeDamage_Dying;
}

I'm not sure though, I copied that last line from OnTakeDamage_Alive and edited it.
Also know as WILLAM; TechieUK is my old name. If you see WILLAM on Steam or ModDB, it's me.
User avatar
TechieUK
Regular
Regular
 
Joined: Sat Apr 13, 2013 4:29 pm

Re: Simple code request

Postby LordDz on Fri Dec 27, 2013 9:41 pm

Oh yeah, just copy the return value that onAlive does and change it and it should probably work.
User avatar
LordDz
May Contain Skills
May Contain Skills
 
Joined: Mon Sep 01, 2008 12:28 pm
Location: Hammer Crash Logs

Return to Programming

Who is online

Users browsing this forum: No registered users