• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

In-game object actions

Status
Not open for further replies.
Level 24
Joined
Aug 1, 2013
Messages
4,657
Hi all.

I was wondering how I could create a generic/dynamic way of handling in-game objects' actions.

For example, I want my unit to jump, walk, stand, etc, but I dont want it to be able to walk while it is busy jumping but I do want it to be able to stand (stop) while walking.

I dont really know how to describe it properly so Google doesnt help me much either.

I have created a game at school where the teacher used a method to create a new class for each action that your object could make and make such a class handle what other actions are allowed.
But this would mean that I need like 40 classes for each individual object-type in game.

I have designed a slightly shorter version where you only have an interface and declare the actions withing the class of the object.
This does help quite some but I still have to write the actions out every single time.

Is there a good generic way of doing stuff like this?
 
Level 25
Joined
May 11, 2007
Messages
4,651
Alternative 1:
Use an enum where you declare the states of the unit?
Stance
{
"Idle"
"Jumping"
"Walking"
"Humping"
"Giving Rep"
}

Give this enum to a unit class, then when your unit is doing an action, you set the enum to this.

So:

if (OurStance == Stance.Idle && jumpButtonPressed)
{
jump();
OurStance = Stance.Jumping;
}

if (OurStance == Stance.Jumping && StopButtonPressed())
{
OurStance = Stance.Idle;
Stop();
}

and so on, you're gonna have to tweak it a bit more but that's about how ya do it.

Alternative 2:
Check jumpHeight when pressing the stop button
if (StopButtonPressed && jumpHeight == 0)
{
stop();
}
else
{
//Fly fly fly away little frog
}
 
I only know C# and C++ and don't have a lot of experience. These languages allows you to dynamicly bind functions to a delegate (for C#) or function pointers (for C++). You can also add, remove and execute all functions in a delegate at once. When your object enters a certain state like jump, you can remove the walk function from the delegate and add the jump function to the delegate. The only problem is that the function signature must match the delegate signature.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
In modern games it is done by sending the entity a message to "jump", "skip" or "leap" etc. Message handlers then process the ones it can understand and discard the ones it cannot. This is how SC2 actors work although likely SC2 objects work in a similar way (but not customizable).

The actual solution is to data feed your engine. Walking, jumping or skipping etc are all common mechanics shared with all such entities. A database then is looked up to determine what a certain object type is permitted to do. Even if your "Stationary Defense Turret" supports jumping mechanically, the type has jumping disabled so you do not get jumping Stationary Defense Turrets in your game. A modder could modify the database to enable jumping Stationary Defense Turrets or even walking ones but the game you make has them disabled.
 
Status
Not open for further replies.
Top