rodrigoueda / GTScriptableVariable

Scriptable variable base on Ryan Hipple Talk about Game Architecture with Scriptable Objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GTScriptableVariable

Table of Contents

About

This repository is based on Ryan Hipple Talk about Game Architecture with Scriptable Objects and containe collection of wrappers that can help you manage dependancies between systems/components. Make sure to be familiar with concept of scriptable objects, because they are the core of this package. Clone this Repo to see the use case of this package.

Variables

Variables are wrappers for any type of data (preferable value type), the purpose of Variables is to decouple data from system, so other systems can access this data without need to know the source of the data. All Variables derived from scriptable object.

Event Variables are like normal variable with addition option for raising game event whenever variable is changed through SetValue method.

use Reference Varaible as a field whenever you don't know whether the class should use variable or constant.

use ReadOnly Variable as a field whenever you need access the value of Variable, but you don't want to change it.

By default there are 4 different variables for int, float, bool and string. That include Variable, Event Variable, Reference Variable, and Readonly Variable for those types.

You can create more variable types with Class Creator or from code.

Creating variables and event variables

All Variables and Event Variables can be create from project context menu

Variables path : Create > ScriptableVars > Vars

Event variables path : Create > ScriptableVars > EventVars

Game Events

Game events are scriptable objects that can be raise from code or by UnityEvent in editor. Every game event contain list of listeners which will be call whenever event is raise. The purpose of game event is to decouple event caller from event receiver.

By default there are 5 different Game Events for each Game Event there is corresponding Listener. The zero argument Listener can subscribe to any type of Game Event.

  • GameEvent
  • IntGameEvent
  • FloatGameEvent
  • BoolGameEvent
  • StringGameEvent

You can create more game event types with Class Creator or from code.

Creating game events

All Game Events can be create from project context menu

Variables path : Create > ScriptableVars > Events

Game Event Triggers

Game Event Triggers are components that can help you trigger your Game Events on one of the unity event(Awake, OnEnable etc.) without need to write additional code. All default game events are supported.

Use Event Trigger to trigger Game Event on one of this unity event:

  • Awake
  • OnEnable
  • Start
  • OnDisable
  • OnDestroy

Use Collision Event Trigger to trigger Game Event on one of this unity event:

  • OnTriggerEnter
  • OnTriggerEnter2D
  • OnTriggerExit
  • OnTriggerExit2D
  • OnCollisionEnter2D
  • OnCollisionEnter
  • OnCollisionExit2D
  • OnCollisionExit

Collision Event Triggers support dynamic parameter for parameters which type derived from Component, like ParticleSystem or Rigidbody. If dynamic parameter is tick then Game Event will be call with component attached to the gameobject which trigger on of the unity physic events. If the component is not present then one of the following think happen, depends on chosen behavior:

  • StopOnNull - won't trigger event if component is null
  • TriggerWithDefualt - will trigger event with component set in editor
  • TriggerWithNull - will trigger event passing null as the argument

Dynamic Collections

Dynamic Collections are wrappers for List of Components or Game Objects the purpose of Dynamic Collections is to decouple the way of collecting Components or Game Objects from the systems that require those Components/Game Objects.

By defualt there is only GameObjectCollection, which can contain Game Objects

Currently you can add custom dynamic collection only from code(Class Creator will support creation of dynamic collection in the near future). Use this code snippets as the guidelines for creating custom dynamic collection. [NameOfType] - type name of the component for example Rigidbody

  namespace GTVariable
  {
      public class [NameOfType]Collection : DynamicCollection<[NameOfType]>
      {

      }
  }
  //this file should be in "Editor" folder
  namespace GTVariable.Editor
  {
      public class [NameOfType]CollectionEditor : DynamicCollectionEditor<[NameOfType]Collection, [NameOfType]>
      {

      }
  }

Text Widgets

Text Widgets are components that can help you display value of Variable on the screen. There is TextWidget for each default Variable. You need TextMeshPro to use this components.

To update widget you need to call UpdateValue method. You can do that by adding a Listener component on same Game Object and calling UpdateValue method in response.

Class Creator

Use class creator whenever you need to create custom type of Variable, Game Event or Event Trigger

To open class creator window navigated to Tools > Class Creator

After opening the class creator window:

  1. Specify the path in which files should be create, by clicking on Select Button in top right.

The path should lead to the Assets folder and shouldn't lead to the this package folder

  1. Specify the class name and type name for example Class Name: Float | Type Name: float
  2. You can add namespace in namespace field, if needed. For example using MyNameSpace;
  3. Select either Game Event Creator to create custom Game Event or Variable Creator to create custom Variable
  4. Select classes you want to create.
  5. You can preview file by clicking Preview Button. You need to update preview everytime you make chanages.
  6. Click Create Button to create clasess file or Delete Button to delete classes file.

Folder structure for custom types

.
├── Editor                             # Folder for editor class
|   ├── Collections
|   ├── Events
|   |   ├── EventTriggers
|   |   |   ├── CollisionTriggers
|   |   |   └── Triggers
|   |   ├── GameEvents
|   |   ├── Listeners
|   |   └── UnityEvents
|   └── Variables
|   |   ├── References
|   |   └── Vars
|   |   |   └── ReadOnly
└── Runtime                            # Folder for runtime class
|   ├── Collections
|   ├── Events
|   |   ├── EventTriggers
|   |   |   ├── CollisionTriggers
|   |   |   └── Triggers
|   |   ├── GameEvents
|   |   ├── Listeners
|   |   └── UnityEvents
|   └── Variables
|   |   ├── EventVars
|   |   ├── References
|   |   └── Vars
|   |   |   └── ReadOnly

About

Scriptable variable base on Ryan Hipple Talk about Game Architecture with Scriptable Objects

License:MIT License


Languages

Language:C# 100.0%