The psyche behind a pathological mind

You may wonder why such philosophical title appeared in the blog having a playful physics theme. The idea here is to elicit a bug (or a feature in my sister’s thinking) in being which everyone knows yet so few admit.

The bug is the inability to appreciate. From personal experience, when I was a high schooler there used to be a buzz about being IITian. We were told that an IITian bags more respect than Bill Gates, perhaps, in the context of software engineering. That used to supply a push (both mechanical and mental), to excel in current study. The practice that went to prepare IIT’s entrance examination coupled with the IIT alumnus’s achievement would stand like a testament to the reputation that we realized second (so on) hand.

Then I cleared the entrance and got selected to pursue my undergraduate studies at IIT Roorkee. And to my surprise, I met bit of people who didn’t take any, not some, any, pride being IITian. This anti-fascination-ism reached to the point that someone (a non IITian) asked me “What have you done being an IITian” with possible intention of comparing with non-IITians. This is something I would not have said even if I had not cleared the IIT-JEE.

Similar is the case with Earth’s pollution. If you really want to understand the context, try computing the ratio of number of planets discovered by Mankind to number of planets with conscious beings, something even a ten year old can imagine and visualize. Given such extreme figure, how can one even think of polluting the environment deliberately, for instance by maintaining the space race. The problem is same, inability to appreciate.

Lastly, since this is playful physics blog, I’d like to mention multiplatforming. Those who have taken into account the definition of Turing Machine, may understand how satisfying it is to play a game (run a same program) on desktop, laptop, mobile, and so called next-gen platforms. Fun is in the ability to play on internet with players from different platforms, called cross-play. Achieving is certainly not hard, Namco has so aptly demonstrated, and also not pretty obvious, 2K team has so aptly demonstrated (mild sarcasm). Yet you may find “them” (players?) complaining and not appreciating the feat Namco has achieved. What is like achieving a miracle for 2K (sic) is seemingly not worth consideration for Namco’s player(?) base.

Finite State Machine For Weapons

Making an Unreal Engine game solo is quite introspecting experience in a way. Not only you can take independent decisions but also you have to take the responsibility of those decisions and ability to convince others (while presenting the work). Thus there is an element of democracy in the process and if done right, could be a secularist process, in the sense that you constantly get to challenge the dogma. Then this process becomes necessary for proper evolution of everything involved in making of games.

For instance Epic’s global illumination way of making photorealistic graphics (Lumen) can be reasonably questioned and some, if not many, studios have chosen to do without getting into the complexity which is not measured by checking a particular field in the details panel.

For my game with the theme of shooting and killing zombies, I decided to use FSM for different states of weapon for instance firing and just holding. A very apt article on FSM for games is in game programming patterns. A well known game Unreal Tournament also uses FSM for weapons. So I took some code from there and used that in my game.

"These (FSMs) came out of a branch of computer science called automata theory whose family of data structures also includes the famous Turing machine"

A minor glimpse of the design can be seen here. The FSM states that I consider are

  1. USTWeaponStateFiring – When the weapon is firing
  2. USTWeaponStateActive – When weapon is not firing but being held by player
  3. USTWeaponStateInActive – When weapon is not held by player (dropped weapon etc)
  4. Zooming – When player is using zoom feature

They are all derived from a single base class USTWeaponState defined like so

UCLASS(DefaultToInstanced, EditInlineNew, CustomConstructor, Within=STWeapon)
class SUNOVATECHZOMBIEKILL_API USTWeaponState : public UObject
{
    ...
}

The variable for caching the current state is declared like so


/**
 * @brief The present state of the weapon. Basically cache for weapon's  
 * finite state machine
 * 
 * @note UT uses UUTWeaponState
 */
UPROPERTY(BlueprintReadOnly, Category = "Weapon")
USTWeaponState* CurrentState;

Finally, the code for transitioning of weapon’s state is like so

/**
 * @brief FSM's routine for state transition
 * 
 * @param NewState The weapon state to transition to
 */
virtual void GotoState(class USTWeaponState* NewState);

All the possible FSM states are instantiated and initialized in the constructor like so

ActiveState = ObjectInitializer.CreateDefaultSubobject<USTWeaponStateActive>(this,
TEXT("StateActive"));
for (int32 i = 0; i < 2; i++)
{ 
USTWeaponStateFiring* NewState = ObjectInitializer.CreateDefaultSubobject<USTWeaponStateFiring, USTWeaponStateFiring>(this, FName(*FString::Printf(TEXT("FiringState%i"), i)), false);
}

Unreal Engine Classes Within

The title has been deliberately written to match that of Final Fantasy: The Spirits Within. With that taken care of, let me focus on the Unreal Engine part. Within is a class specifier for Unreal Engine C++ UCLASS() macro. The official documentation is like so

Within=OuterClassNameObjects of this class cannot exist outside of an instance of an OuterClassName Object. This means that creating an Object of this class requires that an instance of OuterClassName is provided as its Outer Object.

A use case is

UCLASS(Within=STWeapon)
class SUNOVATECHZOMBIEKILL_API USTWeaponState : public UObject
{
 GENERATED_UCLASS_BODY()
 ...
}

where class ASTWeapon is defined like so

UCLASS()
class SUNOVATECHZOMBIEKILL_API ASTWeapon : public UObject
{
  GENERATED_UCLASS_BODY()

public:
 /**
   * @brief Getter for the Owner of this inventory
   */
 ASunovatechZombieKillPawn* GetSTOwner() const
 {
  return STOwner;
 }
  ...
}

Now the GetSTOwner() can be accessed via following code in USTWeaponState

GetOuterASTWeapon()->GetSTOwner();

Iterators : The proper way

यदा यदा हि धर्मस्य ग्लानिर्भवति भारत

धर्मसंस्थापनार्थाय सम्भवामि युगे युगे ॥7-8, Chapter 4॥

A shalok or verse, from Geeta, that is known to many Hindus, or the gist of which gets reflected in our actions (I am a Hindu) anyway, is the representation of iterative theme, of course, coupled with a right action mentioned above. The meaning implies that Shri Krishna takes birth in an epoch, time again, whenever there is rise of evil, to establish law and order.

Unreal Engine has a way of iterating over the C++ objects which has been used in hundreds of AAA games covering a stretch of couple of decades.

The iteration is done like so

for (TActorIterator<AActor> ActorItr(testWorld); ActorItr; ++ActorItr)
{
	// print name
	KR_INFO("Iterating over actor: {0}", (*ActorItr)->GetName());
}

The TActorIterator template is defined here. The pseudo code for iteration roughly is

  1. Initialize ActorList with the appropriate UObjects (of a world or editor relevant objects for instance) of cached objects.
  2. Appropriately define (overload) the increment (++) operator with desired class filters and appropriate checks

A fruitful thing is to think what remains constant and what doesn’t in this way of iteration, which in turn defines the concept of iteration.