Skip to main content
GameDev.net gamedev.net
🔒 Locked

(C++) Source Mod Questions

Started by M Eversberg II Aug 19, 2007 at 11:24 PM 7 replies 1.5k views
Original Post
M Eversberg II
M Eversberg II
Since I'm not totally complete in my C++ classes (still need C++ II before I'm done with the core concepts) I'm coming up with some questions while going through the source code the Source SDK installs for my basic screwing around with mod. (I plan to be a coder for a serious one of my own design eventually) The first bit I saw in the grenade_frag.cpp file. It's an implementation file for the grenade_frag class.

	if( GetOwnerEntity() && GetOwnerEntity()->IsPlayer() )
	{
		m_flDamage		= sk_plr_dmg_fraggrenade.GetFloat();
		m_DmgRadius		= sk_fraggrenade_radius.GetFloat();
	}
	else
	{
		m_flDamage		= sk_npc_dmg_fraggrenade.GetFloat();
		m_DmgRadius		= sk_fraggrenade_radius.GetFloat();
	}
It's a decision structure I found in the Spawn method of the class. I am pretty sure that it's saying if the grenade is both owned AND owned by the player, it executes the first bit. What I do not understand is what the -> means. It's not any of the binary simbols I've seen, but it seems to be linking the two together. I'm sure it means that the grenade is owned by the player, but not sure what the symbol is or means. M.
<work in progress>
Verg
Verg
Semantically, all this is doing is calling a method, which then returns a pointer to an entity type.... which then calls a method:

GetOwnerEntity()->IsPlayer()


The "arrow operator" is how you call a function from a pointer to an object.
It's equivalent to:

(*pointer).IsPlayer()


The meaning is up to the implementation. What that means is... it is calling a method "IsPlayer()" on whatever object pointer is returned by the function "GetOwnerEntity()".

If you looked at the declaration of "GetOwnerEntity()", you'd probably find that it returns a pointer to an "Entity" object of some type (guessing by the name of the function).

THAT Entity would have a function called "IsPlayer()", which likely returns a boolean value.... and likely says whether the Entity in question is the main player in the game.

It seems pretty straightforward.
M Eversberg II
M Eversberg II
Ah. At this point I really havn't done much with pointers. Heard they are important though.

At this stage I'm mostly just playing around with things. Heard it's the best way to learn how to mod; also hoping my class this fall will help make things clearer.

M.
<work in progress>
Verg
Verg
Quote:
Original post by M Eversberg II
Ah. At this point I really havn't done much with pointers. Heard they are important though.


Well, they make things interesting in C++ [smile]

Quote:
At this stage I'm mostly just playing around with things. Heard it's the best way to learn how to mod; also hoping my class this fall will help make things clearer.


That's a good way to learn how things work in software. Load up a project in your IDE and step through it with a debugger, as well as change lines, and pieces of code.

Good luck in class. I'm sure you'll do well.


Chad
M Eversberg II
M Eversberg II
My issue with the way the code is written out is that it's a case scenario for each weapon. Each bit of source for each weapon has a totally different layout, in spite of common features, as well as things not beeing too easily figured out -- I can find where things increment rounds in magazine, but I can't find the initilization of the variable (and thus the value to change to increase/decrease it). Few comments, too. There also seem to be several .cpp files that have no header files to go with them, which is strange.

M.
<work in progress>
Daaark
Daaark
It's not strange to have cpp files without a header.

If weapons have different layouts and stuff, there must be a reason for it.
M Eversberg II
M Eversberg II
Well they're implementation files for a class. For instance there isn't an smg1.h but there is an smg1.cpp implementation file.

However, it has a load of includes

#include "cbase.h"
#include "basehlcombatweapon.h"
#include "NPCevent.h"
#include "basecombatcharacter.h"
#include "AI_BaseNPC.h"
#include "player.h"
#include "game.h"
#include "in_buttons.h"
#include "grenade_ar2.h"
#include "AI_Memory.h"
#include "soundent.h"
#include "rumble_shared.h"

So I'm assuming it's defining things in those.

M.
<work in progress>
Monder
Monder
The reason it's GetOwnerEntity() && GetOwnerEntity()->IsPlayer() is GetOwnerEntity() returns a pointer which may be null (i.e. it doesn't point to anything), dereferencing a null pointer will cause a crash. C++ employs something known as short circuit evaluation, this is where when evaluating an expression once it's certain what the value is it just results in that value without evaluating the rest of the expression. So in a && b if a evaluates to false a && b is certainly false so b is not evaluated. Null is equivalent to a logical false in C++ so if GetOwnerEntity() results in null then GetOwnerEntity() && GetOwnerEntity()->IsPlayer() will short circuit and GetOwnerEntity()->IsPlayer() is not evaluated, hence the null pointer will not be dereferenced.

As for the classes without matching header files, IIRC the weapons in HL2 generally have their class definitions in the CPP file along with the class implementation.

Also a word of warning. The Source SDK is a total mess, poorly documented and a total pain to work with. If you don't have much C++ experience you may want to try doing something else instead.
M Eversberg II
M Eversberg II
I agree, it IS a huge mess. There are, however, a number of tutorials and the like. Hopefully it will not be too difficult a project.

M.
<work in progress>

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.