
Originally Posted by
why06
By making ammo private, you prevent people from screwing things up like this, also I can still find out the ammo like so.
Code:
int ammocount = pistol.GetAmmoCount();
but I don't fuck anything up in the process. This becomes even more important where you have very sensitive variables that could crash the entire program if left unchecked. And that's why we have accessors, so people can look, but not touch, if that makes sense.
Just to contribute: the 'private', 'public' and 'protected' keywords are just for the IDE, they do nothing at all when compiled. Like why said they're just a means to prevent people from writing dirty code.
In game SDK's you can just comment out or replace them all with public and have full access to all the vars, which is sometimes needed to achieve what we basterds want.
If the text below is currently above your league do not continue on reading as it'll only make you more confused.
*why don't we have spoilers*
For example(from the top of my head, dont have SDK installed atm) Alien Swarm uses 'Commander' entities, which you have control over, however instead of using that entity they each have a Marine entity which is the actual entity that is being moved when you press wasd/move cursor etc.
Getting the commander entity is just engine->GetLocalPlayer() which returns the index, then entitylist->GetEntityByIndex(yourindex) to get access to IClientEntity.
Since the commander entity inherits from IClientEntity we can just cast that to the commander entity type(CASW_Player I think it was).
Now the problem is the commander entity doesn't move. so if we want to get our marine's position we'll need to have access to the marine entity, but there's 1 problem!
CASW_Player's GetMarine function is not virtual! that means we can't compile our code calling GetMarine without having a pointer to the original function, however the original function IS present in the SDK, and it does as much as EntityList->GetEntityFromNetworkHandle, and apparantly our CASW_Player class has a private member named m_hMarine!
Now in order to still get access to the marine we just replace the private: into public: and use EntityList->GetEntityFromNetworkHandle(m_hMarine.Value) and we'll have our marine entity! ;D Needless to say that we can get positions etc of that now.
edit: @mooka: So if you called a function that was in a private class would it be a compiler error ? So that would prevent you from accidentally changing whats inside the function?
Yes it would tell you that the variable/function you tried to access/call is protected and can not be used.