<<

SBC – Proceedings of SBGames 2015 | ISSN: 2179-2259 Computing Track – Short Papers

Developing games with : A case study using the Unity3D platform

Marcelo B. Barbosa Andreza B. Rêgo Igor de Medeiros

Federal Institute of Education, Science and Technology of Rio Grande do Norte, Brazil

Abstract projected classes in order to group them on the hierarchy base levels. Consequently, the super-classes Unity3D is a game development framework that placed on the top levels possess more general gathers various tools with the purpose to ease the game properties. This tactic supports the generation of development process. Unity3D uses the concept of subclasses with more specific behaviors. object composition applied to a -based architecture to customize the core elements of the An alternative for reusing functionality is object game: the Game Objects. Yet object inheritance composition. This technique is focused on assembling remains as one of the most common techniques for smaller objects to create a more complex functionality code reuse, object composition offer its own [Gamma et al. 1995]. Despite sharing the same advantages. This article presents a comparison between purpose, inheritance and object composition differs object composition and inheritance on the game substantially. Each one of the mechanisms has its own development context. To observe the practical aspect advantages and disadvantages. The maintenance and of object composition, we developed a game called modification of class’ implementation is easier than orBITa using the Unity3D game engine. We will reusing functionality through inheritance, for instance. present the benefits and disadvantages of using this For our project goal, object composition appears as a object oriented mechanism and demonstrate how it more suitable choice for its inherent capabilities. affects the structure of the scripts components developed. We focused on working with the Unity3D platform for the development of our game. Unity3D is currently Keywords: game development, object-oriented design, one of the most used engines available in the industry. object composition, unity3d For our purposes, the free version of the framework is sufficient. The necessary features for producing a Authors’ contact: simple 2D game are included in the free . [email protected] Unity3D utilizes up-to-date tools and holds an active [email protected] community, ready to answer common questions related [email protected] to the framework.

1. Introduction Objects in Unity3D are defined by a Game Object class that inherits from a base class called The object-oriented paradigm is often used on game MonoBehaviour. Every script automatically derives programming. Game object elements may appear as from MonoBehaviour. This super-class assures that a instances of a bigger Game class, to represent the Game Object can handle events, has a state machine entities composing a game scene, like characters, for its actions and applies physics to the object. enemies, environment objects and projectiles. Although, every other behavior defined for a Game However, an object-oriented approach must experience Object is attributed to a compositional routine difficulties if implemented with inheritance [Passos et implemented by programming scripts called al. 2009]. Objects that inherit from two different Components. Developing games using components can hierarchies may have similar functionality, leading to minimize development costs and reduce the code redundancies. development time [Folmer 2007]. Thus, a component- based architecture is perfectly satisfactory to construct Inheritance is a powerful feature of object oriented our game efficiently. programming which allows the definition of relationships between classes to ease code reuse and In this paper, we expose the contrast and achieve a better overall design [Weisfeld 2008]. A new correlation between object composition and class class can be created by inheriting methods and inheritance in a functional way, through the attributes from a class that has a similar behavior. But development of the 2D game orBITa. The object this often leads to an organizational issue, though it is composition in Unity3D is displayed as a result of not always plausible to have a single super-class that association between Game Objects and programming owns all the resources inherited by a of subclasses. scripts called Components. The main focus is the An object oriented project can become more organized analysis of those elements which are responsible for using hierarchic inheritance. As noted by Horstmann applying specific behaviors to the Game Objects. [2013], while structuring a class hierarchy we can verify which resources are commonly inherited by the 2. Related Work

XIV SBGames – Teresina – PI – Brazil, November 11th - 13th, 2015 240 SBC – Proceedings of SBGames 2015 | ISSN: 2179-2259 Computing Track – Short Papers

between components or Game Objects A method proposed by Bitman [1997] aggregate instances [Passos et al. 2009]. The proposed solution is various studies and shows how software reuse is the utilization of the pattern in affected by the structure of the code. The work the system architecture. This design pattern intends to proposes a model that estimates that applications built provide a flexible way to manage indirect association from independent composed components shall possess between dependent components. It removes the higher cohesion and usability than what is commonly responsibility for the programmer to deal with creating experienced when using inheritance. The model can be instances for composed objects. used as a basis to ensure the applicability of the object composition technique is desirable for improving 3. Composition over Inheritance quality for the proposed scripting process. Nevertheless, the platform has to allow the usage of a An object oriented design requires careful management compositional structure. The Unity3D manual explains to remain efficient. and other that the preferred configuration for working with Game techniques shall be used to achieve the necessary goals Objects relates to the component-based architecture and solve problems that may show up during most of [UnityTechnologies 2015]. Object composition is not the development phases. The main idea is referenced in the documentation, but it stands as the straightforward: favor object composition over class conventional way to work with those types of elements inheritance. This affirmative relies basically on the fact in a lower level programming perspective. that compositional design will produce more objects and the system’s behavior will rely upon the A Game Entity represents a generalization of the relationships between objects instead of behaviors Unity3D Game Object concept, as different game from a super-class [Gamma et al. 1995]. platforms and frameworks may embrace different environments to represent objects existing within the The Decorator pattern also applies as a reasonable game world. Until lately, class hierarchy game entities solution for object oriented design issues. It basically where represented by class hierarchy, and promotes the attachment of similar responsibilities to consequently, used the object inheritance technique to an object dynamically [Freeman and Freeman 2013], reuse functionality. West [2007] described a full similarly to what is proposed for the compositional refactoring process made inside a company from the method. A remarkable fact is that this design pattern game industry. The entities were refactored from a still encourages the use of inheritance. The main hierarchic inheritance architecture to a compositional difference in that case is that object inheritance does one. A new framework was established to allow the not have the intention to extend behaviors from the usage of this kind of structural design with aggregation super-classes: It uses the inheritance in order to of components. After the implementation of the achieve type matching. That is only because the framework, the conversion from object inheritance to decorator class needs to have the same as the object composition went slowly, mainly because of the components it wraps. development team struggle to familiarize with the new context. Hence, this process produced positive results In practical terms, compositional reuse techniques over time: codes became cleaner and easier to maintain share, among others, the following advantages: primarily for the encapsulation of functionality in Independent, composed components are created; independent components. Components are encapsulated; Potentially lower maintenance costs. On the other hand, it shows some Other frameworks and game engines have their disadvantages: Object composition requires greater own style of working with game entities. The Unreal expertise to use; It increases the development time Engine, for example, acts similarly to Unity3D. On its significantly. Inheritance does not contemplate these manual, it is stated that Actors, which are subclasses of advantages; but it has a couple of its own, including: a wider Object class, are objects composed by Eliminates successfully duplication of code, shortens specialized parts called Components [EpicgamesInc development time and demands less proficiency to 2015]. Therefore, it is noted that Unreal Engine 4 uses work with. This leads to the comprehension that an a hybrid approach for its OO design, mixing Object efficient architecture may balance the use of both Composition and Inheritance to enhance the overall techniques to capitalize their advantages [Bitman structure. On the other hand, the Cocos2D-X Game 1997]. It happens mainly because Object Composition Engine works differently. Game Objects are divided tends to be problematic if there are many component into Scene, Node, Sprite, Menu, and Action Objects. dependencies within its structure, which is a common These objects do not have their customization as a situation in the game development context, and also result of smaller objects aggregation. Their properties because inheritance by itself can become troublesome are previously defined on the major classes from which if it occurs within a hierarchy that is too deep. the object behaviors are inherited [Cocos2D-X 2015]. Therefore, derived classes work more properly if the functionalities are tight coupled, while more generic A further study about the composition of behaviors behaviors can be efficiently obtained if implemented in a game system architecture indicates that it could with the compositional model. bring some issues, generally related to the high

XIV SBGames – Teresina – PI – Brazil, November 11th - 13th, 2015 241 SBC – Proceedings of SBGames 2015 | ISSN: 2179-2259 Computing Track – Short Papers

The chosen may influence directly on the outcomes of an Object Composition Figure 3 displays the components that belong to the usage in an OO based structure. Aspect-oriented player Game Object, which represent the astronaut in languages and extensions offer native support to Object the scene, and the main playable character in the game. Composition, for example. By using these resources, we are able to reduce significant delays on the compositional development, and mitigate code complexity issues.

4. orBITa

orBITa is a simple 2D exploration game with an easy- to-learn gameplay system. orBITa is an exploration game that consists on controlling an astronaut that flies around planets in space (Figure 1).

Figure 3: The player Game Object components

Each component modifies the Game Object in a specific way. The last two components (Attracted Body and Player Movement) are manually built scripts.

Figure 1: Player standing on an explored planet. Attracted Body is a component to enable the attraction between an Attractor Body and an Attracted Body: the Players explore a generated galaxy by clicking on player and planet Game Object, respectively. While planets in the scene. On Unity3D, a scene is composed Player Movement is the main controller behavior, of Game Objects and is the element responsible for allowing the user to be able to actually play the game displaying them in the screen [UnityTechnologies and control its character around screen. 2015]. The player and planet Game Objects have scripts to become mutually attracted in order to 4.1 Object Composition applied in the game simulate gravity and the orbit behavior (Figure 2). scripts

Scripts are the instruments giving motion to every working piece in the framework. Every component has necessarily a built-in script to be active during the software execution. Components also operate over scripts available only in the framework source code. Our goal is to evaluate custom scripts created for orBITa due to the possibility of staging properly the code structure, so that we can demonstrate the intended premise.

Figure 2: Player flying towards a planet with applied gravity. First, let’s evaluate the MakeTravel script (Figure Game Objects appear as the fundamental elements 4). It's responsible for the behavior that allows the of the game. Unity3D architecture is shaped upon the player to fly from one planet to another. customization of Game Objects and the interaction between them. But an empty game object does not contemplate a functional scope, even though it inherits behavior of a MonoBehaviour major Game class. Hence, a Game Object acts mainly as a container to smaller game elements, called Components. This approach carry out the component-based system principles previously discussed in Sections 2 and 3. Figure 4: Definition of the MakeTravel class. Components are, in essence, customizable parts. A As shown in Figure 4, the class for our custom script operates as a transformer component to add script inherits from MonoBehaviour, which is the base specific behaviors and cooperation between different class every script derives from [UnityTechnologies Game Objects and Components [UnityTechnologies 2015]. At the first lines of the code, the necessary 2015]. attributes for our class are declared. Here, two different

XIV SBGames – Teresina – PI – Brazil, November 11th - 13th, 2015 242 SBC – Proceedings of SBGames 2015 | ISSN: 2179-2259 Computing Track – Short Papers

Game Objects are set: player and planet. The last one succeeded in providing us with a practical form of the is the Game Object that the script is attached to. Player compositional system. Unity3D matched perfectly the stands for the playable character. A Planet refers to needs of the project, both for its component-based one of the planets disposed on the scene. architectural approach and for the adaptability to object composition while in the scripting process.

References

BITMAN, W.R., 1997. Balancing software composition and inheritance to improve reusability, cost, and error rate. Johns Hopkins APL Technical Digest (Applied Physics Laboratory), 18(4), 485-500.

COCOS2D-X, 2015. Cocos2d-x Developers Manual. Figure 5: Definition of the onMouseDown method. Available from: http://www.cocos2d-x.org/wiki/.

Colliders hold a list with all the colliders attached to the Game Object (Figure 5). It is necessary in order EPICGAMESINC., 2015. Unity Engine 4 Documentation. Available from: https://docs.unrealengine.com/. to get the planet orbit component, which happens to be a collider even though the component is implemented FOLMER, E., 2007. Component Based Game Development – without the collision abilities. A Solution to Escalating Costs and Expanding

Deadlines? In H. Schmidt et al., eds. Component-Based If inheritance was used in this example, some Software Engineering SE - 5. Lecture Notes in Computer troubles would be experienced along the way. Science. Springer Berlin Heidelberg, 66-73. Supposing all components CircleCollider2D, PlayerMovement and Animator functionality are FREEMAN, E. & FREEMAN, E., 2013. Head First Design implemented on MakeTravel’s super-class: the scripts Patterns, Available from: http://www.ncbi.nlm.nih.gov/ would have to be inherited from MonoBehavior to be pubmed/23386138. recognized by the framework. It would work, but a

Player Game Object is essentially different from a GAMMA, E. ET AL., 1995. Design patterns: elements of Planet Game Object. That is a substantial issue for the reusable object-oriented software, Available from: http:// system. We would also have a lot of unnecessary code www.cs.up.ac.za/cs/aboake/sws780/references/patternsto because different kind of objects usually needs architecture/Gamma-DesignPatternsIntro.pdf. different behaviors. Also, we would deal with lots of code redundancies to properly alter those behaviors. PASSOS, E.B. ET AL., 2009. Smart composition of game The code structure would be compromised, along with objects using dependency injection. Computers in the readability and efficiency of the architecture. Entertainment, 7(4), 1.

5. Conclusion UNITYTECHNOLOGIES, 2015. Unity Documentation. Available from: http://docs.unity3d.com/. Object inheritance has its own benefits. It is still necessary to analyze the problem before choosing a WEISFELD, M., 2008. The Object-Oriented Thought Process specific reuse technique. Object inheritance is more 3rd ed., Addison-Wesley Professional. suitable to be applied on development contexts with a limited range of behavior mutability or smaller projects WEST, M., 2007. Evolve Your Hierarchy: Refactoring Game in general, for example. But Object Composition Entities with Components. stands as a solid solution for reuse in OO architecture, and fits perfectly on the context of game development.

Every object oriented project requires special needs and countermeasures to work properly. This paper presents some design patterns intended to fix specific reuse problems: the Decorator pattern and the Dependency injection. Even though it is not trivial to integrate them or other patterns within an architecture model, they are powerful tools to be considered. This paper shows some of the profits of favoring composition over inheritance, and confirmed through a simplified but practical view how object composition eases the programming of behavior functions. The scripts used in orBITa exposes some advantages on code reuse and readability. The development of orBITa

XIV SBGames – Teresina – PI – Brazil, November 11th - 13th, 2015 243