• No results found

CS354R DR SARAH ABRAHAM COMPONENT-BASED SOFTWARE DESIGN

N/A
N/A
Protected

Academic year: 2021

Share "CS354R DR SARAH ABRAHAM COMPONENT-BASED SOFTWARE DESIGN"

Copied!
27
0
0

Loading.... (view fulltext now)

Full text

(1)

COMPONENT-BASED SOFTWARE DESIGN

CS354R

DR SARAH ABRAHAM

(2)

CS354R

GAME ENGINE ARCHITECTURE

‣ Good practices lay a good foundation

(3)

CS354R

INHERITANCE-BASED ARCHITECTURE

‣ Deadly diamond

‣ Hard to maintain

‣ Messy structure

‣ Potential memory penalties

GameObject MovableObject DrawableObject CollisionObject AnimatedObject PhysicalObject

(4)

CS354R

PROBLEM

‣ Entities inherently occupy multiple domains

‣ Domains should remain as isolated as possible

(5)

CS354R

COMPONENT-BASED ARCHITECTURE

‣ Break domains into component classes

‣ Entity acts as a container of components

(6)

CS354R

POTENTIAL DOMAINS

Domains can be a single component or broken into multiple components:

Input

Graphics (Rendering + Animations)

Physics (Collision + Forces)

Sound

GUI

AI (Sensing + Thinking + Acting)

Game Logic

(7)

CS354R

ABSTRACT BASE CLASSES

‣ Components as abstract base classes implemented via interfaces

‣ System knows when/where to call methods

‣ User implements specific functionality needed by parent object

(8)

CS354R

COMPONENTS IN UNREAL

Static mesh component Point light component

(9)

CS354R

UACTORCOMPONENTS

Only way to render meshes, implement collision, play audio, etc

Scene components have location but no geometric representation

Primitive components have location and geometric representation

To interact with larger systems (e.g. rendering, physics, etc), components must be given state for that system

Provides component with properties system requires

Allows for more efficient updating when state is “dirty”

(10)

CS354R

COMPONENT PROPERTIES

‣ Character Movement Component controls all movement associated with Character objects

‣ Walking, Jumping/Falling, Swimming, Flying etc

‣ Physics calculations and

networking replication handled within the Character Movement Component

(11)

CS354R

CONNECTING VIA BLUEPRINT

Directly access the component

Access component through parent

(12)

CS354R

COMPONENTS IN UNITY

Transform component Camera component

Main Camera Game Object associated with each scene

(13)

CS354R

GAMEOBJECTS WITHOUT INHERITANCE

‣ No need for GameObject inheritance

‣ Instantiate GameObjects based on selected components:

GameObject * createPlayer1() { return new GameObject(new Player1InputComponent(), new Player1PhysicsComponent(), new Player1GraphicsComponent()); }

(14)

CS354R

PURE COMPONENT-BASED DESIGN

‣ If we take this model of container classes with components to its extreme, we get Entity-Component-Systems

(15)

CS354R

ENTITY COMPONENT SYSTEM

‣ A specific form of component-based architecture

‣ Entity is an id

‣ Entity data stored as components

‣ Systems modify

related components

(http://www.alecmce.com/)

How do these systems communicate?

(e.g. how can an entity update its sprite during a collision?)

(16)

CS354R

COMPONENT COMMUNICATION

Direct Reference

Components have references to relevant components

Example:

GraphicsComponent contains a reference to Physics Component

Updates sprite upon collision

Message Passing

Component sends message to container class

Container class broadcasts message to its components

Same idea as event-driven programming

(17)

CS354R

IMPLEMENTING ECS

Components grouped by an ID form a “game object”

Need fast component lookup by ID

Factory classes create components for each game object type

Alternatively, “data-driven” model can read in a file defining object types

Inter-object communication requires sending a message to an “object”

to get required response

Know a priori which component gets a given message

Multicast to all of the components of an object

(18)

CS354R

RESTRUCTURING THE ENGINE LOOP

‣ GameObjects contained in vector at game manager level

‣ GameObject components stored in vector within component managers:

class PhysicsManager : public Manager { std::vector<PhysicsComponent>;

}

class Engine {

std::vector<GameObject>;

void update();

}

(19)

CS354R

STORING DATA

‣ Game Objects are a unique id:

‣ Components contain relevant data:

struct GameObject { unsigned int id;

};

struct PhysicsComponent { vector3 position;

quaterion orientation;

vector3 velocity;

};

(20)

CS354R

UPDATING THE SYSTEM

‣ System accesses entities with relevant components during update loop

class PhysicsSystem : public System { std::vector<GameObject> entities;

void update() {

for (entity in entities) {

physics = getPhysicsComponent(entity);

physics.position += physics.velocity;

} ...

Systems add and remove entities as their components change

(21)

CS354R

CREATION AND DESTRUCTION

‣ What do we need to consider when creating or destroying objects in an entity-component system?

(22)

CS354R

MANAGING MEMORY

‣ Essential for entity-component systems

‣ Smart layout of data will avoid cache misses

‣ Cache hits lead to massive performance gains

‣ Arrays are flat with fast access

‣ Vectors allow for flexibility in array size

(23)

CS354R

ECS PROS

Can be more memory-efficient

Only store properties in use, no unused data members in objects

Easier to construct in a data-driven way

Define new attributes with scripts, less recoding of class definitions

Can be more cache-friendly

Data tables loaded into contiguous locations in cache

Struct of arrays (rather than array of structs) principle

(24)

CS354R

ECS CONS

Hard to enforce relationships among properties

Harder to implement large-scale behaviors if they’re

composed of scattered pieces of fine-grained behavior

Harder to debug

Can’t just put a game object into a debugger watch window and see what happens to it

(25)

CS354R

HYBRID SOLUTIONS POSSIBLE

Pure component-based systems might be over-engineering

Always design for the problem

Usual software principles:

Take time to plan before writing code

If a system is difficult to conceptualize, the current approach might be wrong

Leave time to rework existing code

(26)

CS354R

LAST THOUGHTS ON DATA STRUCTURES

There is no one correct solution

Individual preference is a good place to start…

But be flexible and adapt to the problem

Don’t over-engineer or prematurely optimize…

But keep data storage and caching in mind

Try different approaches

Take multiple passes to refactor

(27)

CS354R

REFERENCES

Scott Bilas. A Data-Driven Game Object System<http://

scottbilas.com/files/2002/gdc_san_jose/game_objects_slides.pdf>

Bob Nystrom. Game Programming Patterns <http://

gameprogrammingpatterns.com/component.html>

Randy Gaul. Component Based Engine Design <http://

www.randygaul.net/2013/05/20/component-based-engine- design/>

Nomad Game Engine <https://medium.com/@savas/nomad- game-engine-part-2-ecs-9132829188e5>

References

Related documents

This electrolyzer model allows for the determination of the average Nernst potential, cell operating voltage, gas outlet temperatures, and electrolyzer efficiency for any

If a member or member’s representative is not satisfied upon the exhaustion of the internal expedited complaint review process, the Member, or the Member’s representative, with

Policy alternatives to improve farmers capacity to adapt and anticipate the climate change should consider: (a) development of food crops practices and field school of climate,

Because of shared junc- tion nucleotides in satDNA monomers and because of high sequence similarity in flanking sequences, most of the fragments studied in this set likely emerged

• Develop 1 past successes question that you could use with a client to engage them in the FGDM process?. • Identify what you might learn from the client when they answer

The AHA would support other measures that restrict food advertising and marketing to children including, but not limited to allowing only healthy foods to be marketed and

(1872–2016) of the floodplain vegetation of a segment of the heavily regulated upper Rhine River to different static approaches for the es- timation of its PNV a) a statistical

Both inside and outside distributions are shifted towards larger B-V with respect the high galactic latitude field, meaning that we are observing some extinction also outside the