SlideShare a Scribd company logo
1 of 44
Download to read offline
Introduction to Engine
Development with Component
Based Design
Randy Gaul
Overview – Intro to Engine Development
• What is an engine
• Systems and game objects
• Components
• Engine
• Systems
• Messaging
• Serialization
What is an Engine?
• Collection of systems
class Engine
{
public:
void RunGame( );
void AddSystem( System *system );
void ShutDown( );
private:
std::vector<System *> m_systems;
bool m_gameRunning;
}
Systems
• Perform operations
• Often on game objects
• Example systems:
• Graphics
• Game Logic
• Physics
• Input
• Object manager
class System
{
public:
virtual void Update( float dt ) = 0;
virtual void Init( void ) = 0;
virtual void SendMessage( Message *msg ) = 0;
virtual ~System( ) {}
};
Game Objects
• Collection of Components
• What is a component?
• Contains functions
• Contains data
• More on this later
• Examples:
• Sprite
• AI/Behavior
• Player controller
• Physics collider
Game Object Example
class GameObject
{
public:
void SendMessage( Message *msg );
bool HasComponent( ComponentID id ) const;
void AddComponent( ComponentID id, Component *c );
void Update( float dt );
Component *GetComponent( void );
GameObjectHandle GetHandle( void ) const;
bool Active( void ) const;
private:
GameObjectHandle m_handle;
std::vector<Component *> m_components;
bool m_active;
// Only to be used by the factory!
GameObject( );
~GameObject( );
};
Game Object Details
• SendMessage
• Forwards message to all components
• Component can ignore or respond to each message type
• Active
• Game objects and components contain active bool
• False means will be deleted
• ObjectFactory update should delete everything with m_active as false
• Handle
• Unique identifier used to lookup a game object
• Can be integer, use std::map to start with
Game Object Details
• Private constructor/destructor
• Only want ObjectManager system to create/destroy game objects
Components
• Components define game objects
• Determine behavior, appearance, functionality
• Contains data
• Contains functions
Game Object Diagram
Small Enemy
Sprite
AABB
Collider
Patrol
Behavior
Base Component Example
class Component
{
public:
virtual ~Component( );
virtual void SendMessage( Message *message ) = 0;
virtual void Update( float dt ) = 0;
virtual void Init( void ) = 0;
virtual void ShutDown( void ) = 0;
bool Active( void ) const;
private:
ComponentID m_id; // type of component
bool m_active;
GameObjectHandle m_owner;
};
Derived Component Example
• Physics Component Example
class Collider : public Component
{
public:
// Implement base functions here
void ApplyForce( const Vec2& force );
void SetVelocity( const Vec2& vel );
private:
Shape *shape;
Vec2 m_forces;
Vec2 m_velocity;
Vec2 m_position;
};
Questions?
Systems You Will Need
• Object Manager/Factory
• Graphics
• Physics
• Game Logic
• Input/Windows Manager
Base System Class
class System
{
public:
virtual void Update( float dt ) = 0;
virtual void Init( void ) = 0;
virtual void SendMessage( Message *msg ) = 0;
virtual ~System( ) {}
};
Systems You Might Want
• Audio Manager
• Resource Manager
• Memory Allocator
• Scripting
• UI Manager
• Anything else your heart desires
Messaging
• What is a message?
• Payload
• ID Payload
ID
Messaging
• Send information from one place to another
• Virtual function call is type of messaging
• Can send info from any one place to any other
Messaging
• Send to Engine:
• Usually just forward to all systems
• Send to system:
• Each system handles messages uniquely
• Send to GameObject
• Usually forwards to all components
• Send to Component:
• Each component handles messages uniquely
The Need for Messaging
• Imagine this:
• Systems, game objects and engine can receive messages
• Imagine an Enemy class
• Has some functions
• How do you call a function?
• #include Enemy.h
The Need for Messaging
• Imagine coding a battle sequence
• Player fights monsters
Player::Battle( Enemy *enemy )
{
enemy->SpitFireOnto( this );
}
The Need for Messaging
• Imagine coding a battle sequence
• Player fights monsters
• Any problems?
Player::Battle( Enemy *enemy )
{
enemy->SpitFireOnto( this );
}
The Need for Messaging
Player::Battle( Enemy *enemy )
{
enemy->SpitFireOnto( this );
}
Player::Battle( SmallEnemy *enemy )
{
enemy->SmallEnemyAttack( this );
}
Player::Battle( FatEnemy *enemy )
{
enemy->Eat( this );
}
The Need for Messaging
#include "Enemy.h"
#include "SmallEnemy.h"
#include "FatEnemy.h"
The Need for Messaging
#include "Enemy.h"
#include "SmallEnemy.h"
#include "FatEnemy.h"
#include "DireEnemy.h"
#include "HealthPowerup.h"
#include "Lots of STUFF"
The Need for Messaging
#include "Enemy.h"
#include "SmallEnemy.h"
#include "FatEnemy.h"
#include "DireEnemy.h"
#include "HealthPowerup.h"
#include "Lots of STUFF"
#include "Lots of STUFF"
#include "Lots of STUFF"
#include "Lots of STUFF"
#include "Lots of STUFF"
#include "Lots of STUFF"
#include "Lots of STUFF"
#include "Lots of STUFF"
Messaging Purpose
• Lower file inclusions
• Generic way to send data from one place to another
Messaging Implementation
• Create base message class
• Base just has ID
• Create many derived classes
• Derived holds the payload
• Send base across SendMessage functions
• Inside, use switch on ID to typecast
Processing a Message
// In some header
enum MessageID
{
TakeDamage,
FightBack
};
// In Enemy.cpp
Enemy::SendMessage( Message *message, int payload )
{
switch(message->id)
{
case TakeDamage:
int damage = payload;
hp -= damage;
break;
case FightBack:
HealthComponent *hp = (HealthComponent *)payload;
hp->ApplyDamage( m_damage );
this->PlayFightAnimation( );
break;
}
}
New Battle Sequence
Player::Battle( Enemy *enemy )
{
// Star the player attack animation
this->PlayAttackAnimation( );
// Damage the enemy
enemy->SendMessage( TakeDamage, m_damage );
// Let the enemy fight back agains the player
enemy->SendMessage( FightBack, this );
}
Payload
• Can use an integer
• Typecast inside SendMessage to appropriate type
• Can store 4 bytes
• Able to store pointers as well
• Probably best to:
• Store pointer to Message struct
• Typecast inside SendMessage to derived types
Message Structs
// Base message
struct Message
{
MessageID id;
};
// Random messages for game logic
struct DamageMessage : public Message
{
int damage;
};
struct BumpIntoMessage : public Message
{
GameObject *from;
float collisionSpeed;
float damage;
};
Serialization
• Write object to a file
• Read an object from a file
• Required for saving game state
• Quickly create new object types!!!
• Useful for level editors
Serialization
• Think in terms of components
• Write an object’s components to file
• Read an object’s components to file
Missile.txt
Position = { 12.5, 102.52 }
Health = 5
Sprite = Missile.png
GameLogic = MissileScript.txt
Serialization – Object Creation
• ObjectFactory – creates game objects
• Can also create objects from a file
// Create a new game object
GameObject *o = ObjectFactory->CreateObject( "Missile" );
Serialization – Object Creation
GameObject *ObjectFactory::CreateObject( string fileName )
{
// Open the corresponding file
File file( fileName );
// Construct the object from string
GameObject *gameObject = m_creators[fileName]->Create( );
// Deserialize the gameObject from the file data
gameObject->Deserialize( file );
return gameObject;
}
Serialization – Object Creation
• Constructing objects from string
• Use dependency inversion
• Use std::map to start with
• http://www.randygaul.net/2012/08/23/game-object-factory-distributed-
factory/
Serialization – Object Creation
• Deserialize the GameObject from string
• Create components from file
• Position, Health, Sprite, GameLogic are all components
• Assign values to each created component from file data
Missile.txt
Position = { 12.5, 102.52 }
Health = 5
Sprite = Missile.png
GameLogic = MissileScript.txt
Serialization – Object Creation
void GameObject::Deserialize( File file )
{
while(file.NotEmpty( ))
{
string componentName = file.GetNextWord( );
Component *component =
ObjectFactory->CreateComponent( file, componentName );
this->AddComponent( component );
}
}
Serialization – Object Creation
void GameObject::Deserialize( File file )
{
while(file.NotEmpty( ))
{
string componentName = file.GetNextWord( );
Component *component =
ObjectFactory->CreateComponent( file, componentName );
this->AddComponent( component );
}
}
Serialization – Object Creation
Component *ObjectFactory::CreateComponent( File file, string name )
{
Component *component = m_creators[name]->Create( );
// Deserialize is a virtual function!
component->Deserialize( file );
return component;
}
Component Deserialize Example
void PhysicsComponent::Deserialize( file )
{
x = file.ReadFloat( );
y = file.ReadFloat( );
}
• Keep it really simple
• Hard-code the data members to read in
Component Serialize Example
void PhysicsComponent::Serialize( file )
{
file.WriteFloat( x );
file.WriteFloat( y );
}
• To-file is just as simple
Parting Advice
• Don’t worry about efficiency
• Ask upper classmen for help
• Read Chris Peter’s demo engine
• Keep it simple
• Email me: r.gaul@digipen
• http://www.randygaul.net/2012/08/23/game-object-factory-distributed-factory/

More Related Content

Similar to IntroToEngineDevelopment.pdf

Scene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesScene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesBryan Duggan
 
Sephy engine development document
Sephy engine development documentSephy engine development document
Sephy engine development documentJaejun Kim
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScriptDenis Voituron
 
Gdc09 Minigames
Gdc09 MinigamesGdc09 Minigames
Gdc09 MinigamesSusan Gold
 
#NewMeetup Performance
#NewMeetup Performance#NewMeetup Performance
#NewMeetup PerformanceJustin Cataldo
 
Clean Code Development
Clean Code DevelopmentClean Code Development
Clean Code DevelopmentPeter Gfader
 
Game Models - A Different Approach
Game Models - A Different ApproachGame Models - A Different Approach
Game Models - A Different ApproachNick Pruehs
 
SwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup GroupSwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup GroupErnest Jumbe
 
Look Mommy, No GC! (TechDays NL 2017)
Look Mommy, No GC! (TechDays NL 2017)Look Mommy, No GC! (TechDays NL 2017)
Look Mommy, No GC! (TechDays NL 2017)Dina Goldshtein
 
Secret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutSecret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutDror Helper
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Remy Sharp
 
Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02PL dream
 
Cnam azure 2014 mobile services
Cnam azure 2014   mobile servicesCnam azure 2014   mobile services
Cnam azure 2014 mobile servicesAymeric Weinbach
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsNick Pruehs
 
Refactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartGabriele Lana
 
AIWolf programming guide
AIWolf programming guideAIWolf programming guide
AIWolf programming guideHirotaka Osawa
 

Similar to IntroToEngineDevelopment.pdf (20)

Scene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesScene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game Engines
 
Sephy engine development document
Sephy engine development documentSephy engine development document
Sephy engine development document
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScript
 
Gdc09 Minigames
Gdc09 MinigamesGdc09 Minigames
Gdc09 Minigames
 
#NewMeetup Performance
#NewMeetup Performance#NewMeetup Performance
#NewMeetup Performance
 
Clean Code Development
Clean Code DevelopmentClean Code Development
Clean Code Development
 
Game Models - A Different Approach
Game Models - A Different ApproachGame Models - A Different Approach
Game Models - A Different Approach
 
Dart Workshop
Dart WorkshopDart Workshop
Dart Workshop
 
SwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup GroupSwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup Group
 
Look Mommy, No GC! (TechDays NL 2017)
Look Mommy, No GC! (TechDays NL 2017)Look Mommy, No GC! (TechDays NL 2017)
Look Mommy, No GC! (TechDays NL 2017)
 
C# 6.0 Preview
C# 6.0 PreviewC# 6.0 Preview
C# 6.0 Preview
 
Game dev 101 part 2
Game dev 101   part 2Game dev 101   part 2
Game dev 101 part 2
 
Secret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutSecret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you about
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
 
Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02
 
Cnam azure 2014 mobile services
Cnam azure 2014   mobile servicesCnam azure 2014   mobile services
Cnam azure 2014 mobile services
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine Basics
 
Refactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing Part
 
Android ndk
Android ndkAndroid ndk
Android ndk
 
AIWolf programming guide
AIWolf programming guideAIWolf programming guide
AIWolf programming guide
 

Recently uploaded

chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 

Recently uploaded (20)

chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 

IntroToEngineDevelopment.pdf

  • 1. Introduction to Engine Development with Component Based Design Randy Gaul
  • 2. Overview – Intro to Engine Development • What is an engine • Systems and game objects • Components • Engine • Systems • Messaging • Serialization
  • 3. What is an Engine? • Collection of systems class Engine { public: void RunGame( ); void AddSystem( System *system ); void ShutDown( ); private: std::vector<System *> m_systems; bool m_gameRunning; }
  • 4. Systems • Perform operations • Often on game objects • Example systems: • Graphics • Game Logic • Physics • Input • Object manager class System { public: virtual void Update( float dt ) = 0; virtual void Init( void ) = 0; virtual void SendMessage( Message *msg ) = 0; virtual ~System( ) {} };
  • 5. Game Objects • Collection of Components • What is a component? • Contains functions • Contains data • More on this later • Examples: • Sprite • AI/Behavior • Player controller • Physics collider
  • 6. Game Object Example class GameObject { public: void SendMessage( Message *msg ); bool HasComponent( ComponentID id ) const; void AddComponent( ComponentID id, Component *c ); void Update( float dt ); Component *GetComponent( void ); GameObjectHandle GetHandle( void ) const; bool Active( void ) const; private: GameObjectHandle m_handle; std::vector<Component *> m_components; bool m_active; // Only to be used by the factory! GameObject( ); ~GameObject( ); };
  • 7. Game Object Details • SendMessage • Forwards message to all components • Component can ignore or respond to each message type • Active • Game objects and components contain active bool • False means will be deleted • ObjectFactory update should delete everything with m_active as false • Handle • Unique identifier used to lookup a game object • Can be integer, use std::map to start with
  • 8. Game Object Details • Private constructor/destructor • Only want ObjectManager system to create/destroy game objects
  • 9. Components • Components define game objects • Determine behavior, appearance, functionality • Contains data • Contains functions
  • 10. Game Object Diagram Small Enemy Sprite AABB Collider Patrol Behavior
  • 11. Base Component Example class Component { public: virtual ~Component( ); virtual void SendMessage( Message *message ) = 0; virtual void Update( float dt ) = 0; virtual void Init( void ) = 0; virtual void ShutDown( void ) = 0; bool Active( void ) const; private: ComponentID m_id; // type of component bool m_active; GameObjectHandle m_owner; };
  • 12. Derived Component Example • Physics Component Example class Collider : public Component { public: // Implement base functions here void ApplyForce( const Vec2& force ); void SetVelocity( const Vec2& vel ); private: Shape *shape; Vec2 m_forces; Vec2 m_velocity; Vec2 m_position; };
  • 14. Systems You Will Need • Object Manager/Factory • Graphics • Physics • Game Logic • Input/Windows Manager
  • 15. Base System Class class System { public: virtual void Update( float dt ) = 0; virtual void Init( void ) = 0; virtual void SendMessage( Message *msg ) = 0; virtual ~System( ) {} };
  • 16. Systems You Might Want • Audio Manager • Resource Manager • Memory Allocator • Scripting • UI Manager • Anything else your heart desires
  • 17. Messaging • What is a message? • Payload • ID Payload ID
  • 18. Messaging • Send information from one place to another • Virtual function call is type of messaging • Can send info from any one place to any other
  • 19. Messaging • Send to Engine: • Usually just forward to all systems • Send to system: • Each system handles messages uniquely • Send to GameObject • Usually forwards to all components • Send to Component: • Each component handles messages uniquely
  • 20. The Need for Messaging • Imagine this: • Systems, game objects and engine can receive messages • Imagine an Enemy class • Has some functions • How do you call a function? • #include Enemy.h
  • 21. The Need for Messaging • Imagine coding a battle sequence • Player fights monsters Player::Battle( Enemy *enemy ) { enemy->SpitFireOnto( this ); }
  • 22. The Need for Messaging • Imagine coding a battle sequence • Player fights monsters • Any problems? Player::Battle( Enemy *enemy ) { enemy->SpitFireOnto( this ); }
  • 23. The Need for Messaging Player::Battle( Enemy *enemy ) { enemy->SpitFireOnto( this ); } Player::Battle( SmallEnemy *enemy ) { enemy->SmallEnemyAttack( this ); } Player::Battle( FatEnemy *enemy ) { enemy->Eat( this ); }
  • 24. The Need for Messaging #include "Enemy.h" #include "SmallEnemy.h" #include "FatEnemy.h"
  • 25. The Need for Messaging #include "Enemy.h" #include "SmallEnemy.h" #include "FatEnemy.h" #include "DireEnemy.h" #include "HealthPowerup.h" #include "Lots of STUFF"
  • 26. The Need for Messaging #include "Enemy.h" #include "SmallEnemy.h" #include "FatEnemy.h" #include "DireEnemy.h" #include "HealthPowerup.h" #include "Lots of STUFF" #include "Lots of STUFF" #include "Lots of STUFF" #include "Lots of STUFF" #include "Lots of STUFF" #include "Lots of STUFF" #include "Lots of STUFF" #include "Lots of STUFF"
  • 27. Messaging Purpose • Lower file inclusions • Generic way to send data from one place to another
  • 28. Messaging Implementation • Create base message class • Base just has ID • Create many derived classes • Derived holds the payload • Send base across SendMessage functions • Inside, use switch on ID to typecast
  • 29. Processing a Message // In some header enum MessageID { TakeDamage, FightBack }; // In Enemy.cpp Enemy::SendMessage( Message *message, int payload ) { switch(message->id) { case TakeDamage: int damage = payload; hp -= damage; break; case FightBack: HealthComponent *hp = (HealthComponent *)payload; hp->ApplyDamage( m_damage ); this->PlayFightAnimation( ); break; } }
  • 30. New Battle Sequence Player::Battle( Enemy *enemy ) { // Star the player attack animation this->PlayAttackAnimation( ); // Damage the enemy enemy->SendMessage( TakeDamage, m_damage ); // Let the enemy fight back agains the player enemy->SendMessage( FightBack, this ); }
  • 31. Payload • Can use an integer • Typecast inside SendMessage to appropriate type • Can store 4 bytes • Able to store pointers as well • Probably best to: • Store pointer to Message struct • Typecast inside SendMessage to derived types
  • 32. Message Structs // Base message struct Message { MessageID id; }; // Random messages for game logic struct DamageMessage : public Message { int damage; }; struct BumpIntoMessage : public Message { GameObject *from; float collisionSpeed; float damage; };
  • 33. Serialization • Write object to a file • Read an object from a file • Required for saving game state • Quickly create new object types!!! • Useful for level editors
  • 34. Serialization • Think in terms of components • Write an object’s components to file • Read an object’s components to file Missile.txt Position = { 12.5, 102.52 } Health = 5 Sprite = Missile.png GameLogic = MissileScript.txt
  • 35. Serialization – Object Creation • ObjectFactory – creates game objects • Can also create objects from a file // Create a new game object GameObject *o = ObjectFactory->CreateObject( "Missile" );
  • 36. Serialization – Object Creation GameObject *ObjectFactory::CreateObject( string fileName ) { // Open the corresponding file File file( fileName ); // Construct the object from string GameObject *gameObject = m_creators[fileName]->Create( ); // Deserialize the gameObject from the file data gameObject->Deserialize( file ); return gameObject; }
  • 37. Serialization – Object Creation • Constructing objects from string • Use dependency inversion • Use std::map to start with • http://www.randygaul.net/2012/08/23/game-object-factory-distributed- factory/
  • 38. Serialization – Object Creation • Deserialize the GameObject from string • Create components from file • Position, Health, Sprite, GameLogic are all components • Assign values to each created component from file data Missile.txt Position = { 12.5, 102.52 } Health = 5 Sprite = Missile.png GameLogic = MissileScript.txt
  • 39. Serialization – Object Creation void GameObject::Deserialize( File file ) { while(file.NotEmpty( )) { string componentName = file.GetNextWord( ); Component *component = ObjectFactory->CreateComponent( file, componentName ); this->AddComponent( component ); } }
  • 40. Serialization – Object Creation void GameObject::Deserialize( File file ) { while(file.NotEmpty( )) { string componentName = file.GetNextWord( ); Component *component = ObjectFactory->CreateComponent( file, componentName ); this->AddComponent( component ); } }
  • 41. Serialization – Object Creation Component *ObjectFactory::CreateComponent( File file, string name ) { Component *component = m_creators[name]->Create( ); // Deserialize is a virtual function! component->Deserialize( file ); return component; }
  • 42. Component Deserialize Example void PhysicsComponent::Deserialize( file ) { x = file.ReadFloat( ); y = file.ReadFloat( ); } • Keep it really simple • Hard-code the data members to read in
  • 43. Component Serialize Example void PhysicsComponent::Serialize( file ) { file.WriteFloat( x ); file.WriteFloat( y ); } • To-file is just as simple
  • 44. Parting Advice • Don’t worry about efficiency • Ask upper classmen for help • Read Chris Peter’s demo engine • Keep it simple • Email me: r.gaul@digipen • http://www.randygaul.net/2012/08/23/game-object-factory-distributed-factory/