UnityLearn – Beginner Programming – Observer Pattern – Pt. 03 – The Observer Pattern

Novemeber 7, 2019

Beginner Programming: Unity Game Dev Courses

Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

The Observer Pattern

The Observer Pattern

Observer Pattern: software design pattern in which an object, called the subject, maintains a list of dependents, called observers, and notifies them of any state change, usually by calling one of their methods

Observer Pattern Anatomy

Subject

  • collection of observers
  • method: AddObserver
  • method: RemoveObserver
  • method: NotifyObservers

Observer

  • method: Notify (what to do when notified)

An interface is a good way to ensure observers have the types of methods you need to exist when notified by the subject.

Implementing the Observer Pattern

This was an example of how to setup a basic observer pattern using the example project. First they created the interface for the observers, which was named IEndGameObserver. This simply held an empty method named Notify().

They then used the GameSceneController script as the subject for the observer pattern. To do so, they created a list of IEndGameObserver objects, created a method AddObserver which could add to that list, created a method RemoveObserver which could remove from that list, and finally a method called NotifyObservers that was a simple foreach loop that went through each IEndGameObserver in the list and called their Notify() method.

NotifyObservers then just needs to be called when the event or state is reached where the observers need to be informed that a change has occurred. Since this example was to inform objects of when the game ended, NotifyObservers was called within the GameSceneController script when the player ran out of lives.

Concrete Observers

Concrete Observers: just objects that implement the observer interface

This example showed that with this setup, either the observer or the subject can be used to add observers to the subject’s collection of observers. The HUDController already had a reference to the GameSceneController (subject) so it made sense to just have it add itself to the observer collection through that reference.

While the EnemyController and PowerupController do not have reference to the GameSceneController, the GameSceneController has references to them created on instantiating them. These could then be used with the GameSceneController to add them to the observer collection upon instantiation.

***Could possibly use this observer pattern in my thesis project to easily collect all of the various Create classes into the Scenario classes (and potentially again to collect the Scenario classes into the SystemManager).

Removing Observers

To avoid nullreferenceexceptions, you must ensure that observers that are destroyed are also removed from the subject’s collection of observers.

To apply this to the example, we added a method called RemoveAndDestroy to both the PowerupController and the EnemyController class which both removed it from the observer collection and destroyed it. This method was exactly the same for both, so this could indicate that it should be added to the interface itself to keep it consistent amongst all observers using this interface.

The Observer Pattern and C# Events

Example Problem with C# Events:
The ProjectileController has an event that occurs when it reaches the bounds of the screen. The PlayerController subscribes to this event by adding one of its methods to this event. There are some cases where the projectile exists while the player is destroyed, so when the projectile goes out of bounds (and calls its event), it tries to invoke a method from the destroyed PlayerController class, which gives an error.

Similar to the observer pattern where we want to remove observers from the collection when they are destroyed to prevent errors, we want to unsubscribe classes from events when they are destroyed. This ensures that they are revmoved from the events invocation list so that they do not try to call methods of destroyed objects which will create errors.

The Publisher Subscriber Pattern

Publisher Subscriber Pattern: similar to Observer Pattern, but adds another entity, the broker, which is in charge of keeping track of and notifying subscribers.

Observer Pattern has a single subject, but publisher subscriber pattern can have any number of publishers. Each of these publishers has a reference to the broker to notify it when a noteworthy happening occurs.

Example:
They created a class called EventBroker that just contained a public static event ProjectileOutOfBounds and a public static method named CallProjectileOutOfBounds (which just checked that the event wasn’t empty and then called that event). The PlayerController subscribes to this event by adding its EnableProjectile method to it on Start (and removes it OnDisable), both of which are very direct since everything is public static in EventBroker. Finally, the ProjectileController invokes the event CallProjectileOutOfBounds when the projectile leaves the screen, which now invokes the PlayerController’s EnableProjectile method.

This setup allows the ProjectileController to effectively communicate with the PlayerController without references to each other in anyway by going through the EventBroker.

SUMMARY

Using delegates, events, and actions can provide a clean and effective way to communicate important happenings throughout many objects and have them act accordingly. The Observer Pattern uses a single subject and multiple observers which receive information from the subject when to do something. The Publisher Subscriber system is similar, but adds a middle man entity of a broker which communicates the information between the publisher and subscribers without the publishers or subscribers needing any direct reference between each other.

HFFWS Thesis Project – Theory for General Generation System Architecture

November 6, 2019

HFFWS Puzzle Generation System

General System Architecture

General Notes

Pulley Varieties

I wanted to start with sketching out the general structure of a single puzzle type to help me get a better idea of what a useful general structure might look like for the overall puzzle generating system I want to make.

  • Hook on an end to latch onto other objects
  • Object to move can be used as a platform
  • Open heavy door
  • Focus on rotation of wheel
  • Build a pulley
  • (Rope puzzle) Move two rope conjoined objects somewhere to accomplish a goal

Breakdown of what I can currently alter/generate in rope/pulley tool

  • Objects (at ends of rope)
  • Objects (for wheels)
  • Length (of rope)
  • Position (of rope)
  • Position(s) (of wheel(s))

I then quickly sketched out what I could change with the current controllable parameters to match the various puzzle variations (I only did the first three for now just to work it into testing quicker).

Hook on end

  • Objects at ends
    • Hook
    • Grabbing Mass (i.e. small sphere)
  • Outside objects
    • Objects that need pulled by hook

Objects attached can be used as platforms

  • Objects at ends
    • Platform type objects
  • Outside objects
    • Mass objects to influence pulley movement

Open heavy door

  • Objects at ends
    • Heavy Door
  • Outside objects
    • Mass objects to influence pulley movement

Architecture Design (1st Pass)

The overall flow of information is:
GenerationManager -> ScenarioManager -> “Create” type classes

GenerationManager

Passes down highest level information to ScenarioManager. This will include puzzle type, variation type within that puzzle type, and any other high level information such as overall positioning or how many scenarios in total to generate.

ScenarioManager

Will take that information and use it to determine which “Create” type classes will need to be used (or have the option of being used), how many times to use each, and what other information needs to be passed on to them (the “Create” type classes) to properly build out the desired scenario.

“Create” type Class

Use the information passed down to them from the ScenarioManager to create objects that satisfy the needs of the desired scenario. Within these bounds, in then adds variation to what it creates to vary its creations while still meeting the requirements given by the ScenarioManager

All the “Create” type classes will need an interface or inheritance of some kind to make them all similar types of objects to make grouping them and using them a bit easier.

UnityLearn – Beginner Programming – Observer Pattern – Pt. 02 – Working with Multiple Subscriptions

Novemeber 5, 2019

Beginner Programming: Unity Game Dev Courses

Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Working with Multiple Subscriptions

Creating a Parameterized Event

Parameterized Event: an event that takes in at least one parameter

Points of Reference

A subscriber must have a reference to the publisher (class raising the event) in order to be able to subscribe to that event.

The general pattern for making delegates and events from how they have done it so far is that you create the overall delegate type that you will want the events to use outside of a class definition and public (so that all classes that want to make an event of this delegate type can use this single delegate type as a reference). The events themselves are then within the class definition, and use that newly created delegate type.

Example Breakdown

I broke down the tutorial example in order to better understand it.

The parameterized event is created in the EnemyController script. This is done by creating a delegate called EnemyDestroyedHandler outside of the class definition in the EnemyController script (this delegate is of type void and takes an int parameter, so any event using this must do follow the same signature). They then created a public event of type EnemyDestroyedHandler named EnemyDestroyed within the class definition of EnemyController.

Since subscribers need references to the publishers in order to subscribe to their events, they go to the GameSceneController class to subscribe to the EnemyDestroyed event since it already has references to the EnemyController class in its spawning methods. Here they add a method (Enemy_EnemyDestroyed, the default name) from within the GameSceneController class to the EnemyDestroyed event. It matches the signature (return type void, with a single paramter of type int). Now, when the EnemyDestroyed event is called in the EnemyController script, the parameter input given there is the same parameter input used for all methods subscribed to that event. So in EnemyController, EnemyDestroyed(pointValue) ends up calling Enemy_EnemyDestroyed(pointValue) from within the GameSceneController class.

The GameSceneController also creates its own event of type EnemyDestroyedHandler named ScoreUpdatedOnKill. This uses the same delegate type created in the EnemyController script, so they just need to create the event here in the GameSceneController script. It should be noted that this can be done simply because they are using the same signature delegate type here, it does not necessarily even have to do with the fact that its a similar game related event. This event is then called within the Enemy_EnemyDestroyed method, which is subscribed to the EnemyController EnemyDestroyed event already. So this creates a chain of method calls from a single event.

This event, ScoreUpdatedOnKill, is used to update the HUD score value. To accomplish this, we need the HUDController class to subscribe to the event. So they go to the HUDController and create a reference to the GameSceneController class (since subscribers must have a reference to the publisher). Since the GameSceneController is an object that will persist throughout the entirety of the game, they simply create the reference to it and subscribe to the event within the GameSceneController within the Start method of the HUDController.

ScoreUpdatedOnKill, the GameSceneController event, is given the method GameSceneController_ScoreUpdatedOnKill from the HUDController. This method simply calls the method UpdateScore which changes the text to match the new score.

It is important to note that since this is a chain of events, as opposed to multiple subscribers subscribing to a single event, the value passed throughout the chain can change. The EnemyDestroyed event in EnemyController uses the int pointValue, so that the GameSceneController method Enemy_EnemyDestroyed subscribed to it adds that value to the totalPoints, which is held within the GameSceneController. The GameSceneController then passes totalPoints (not pointValue) into its event, ScoreUpdatedOnKill, which is the event the HUDController subscribes to in order to update the score. This makes sure it displays the new total score as opposed to just the value for the last score gained.

I wanted to make this difference clear since I also noted that multiple subscribers to a single parameterized event will all use the same parameters. This chain of events simply use the same delegate type because they just happen to want to use the same signature (return type void with a single int parameter) and nothing more.

Multiple Subscribers

You can have several classes subscribe to the same event.

What was weird for this example was that they just wanted to reenable the firing mechanism for the player when an enemy was destroyed, using the same ScoreUpdatedOnKill event. This event requires a method of return type void with an int parameter. So they made a method satisfying this, but it doesn’t use the int parameter at all. It simply calls another method that is just void return type with no input parameter, EnableProjectile (which lets the player fire again). Since you can simply subscribe to an event with a method that just calls another method, your method actually doing work does not particularly have to match the signature of the original delegate type.

Conclusion

Actions (C#): types in the System namespace that allow you to encapsulate methods without explicitly defining a delegate

Actions are inherently delegate types so they keep you from having to separately define a delegate when creating an event. They mention that they will just use actions from here on for the course, so I assume this is generally better and cleaner practice this way. This does make more sense with my recent understanding that the delegate signature type is not particularly that crucial for the actual outcome when dealing with this subscriber and publisher pattern.

SUMMARY

Subscribers need a reference to their publisher in order to use their events, so look for places where you are already creating this reference for improved efficiency. An event can have multiple subscribers, so calling that event can call multiple methods from multiple objects. You can also create chains of actions that call methods while starting other events. Actions are a more compact and cleaner way to create an event without the need for separately creating the delegate.

UnityLearn – Beginner Programming – Observer Pattern – Pt. 01 – Handling Events

Novemeber 4, 2019

Beginner Programming: Unity Game Dev Courses

Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Handling Events

The Demo Project

This section focused on handling inter-object communications. They covered direct object calls, tight coupling, and loose coupling. This topic was covered using mostly delegates and events.

Events: something that happens within the context of one object to be communicated to other

The first objective of the tutorial was to restric the rate of fire of the player ship so that it could only have one projectile on the screen at any time. They could not fire again until that projectile was destroyed (which in this case, only occurred once it left the screen).

Direct Object Calls

Direct Object Calls: directly make a call from within one object to a method in another object (usually through public methods)

Example: The ship needed to be able to fire again when the projectile reached the end of the screen. The projectile used a reference to the PlayerController to call a method within it when the projectile reached the end of the screen to reenable the firing mechanism.

Costs: This forces you to expose a lot of methods within objects so that others may access them.

Tight Coupling

Tight Coupling: making objects dependent on one another; can be done by having one object call a method found in another object

Costs:

  • Difficult to maintain and debug
  • Not easily scalable
  • Impedes collaboration (like Git or Collab)
  • Complicates Unit Testing

Delegates and Events

This section looks to use delegates and events to get around the issues found with direct object calls and tight coupling. These allow for loose coupling.

Important Notes
  • One way to think of delegates is that they are method variables.
  • All events have an underlying delegate type.

Example: The ProjectileController script created a public delegate and public event. It then called that event within its code somewhere (which would then execute any methods assigned to that event). The PlayerController assigned one of its methods to that public event when instantiating the projectile. The ProjectileController could then effectively call that PlayerController method by calling the event within it.

SUMMARY

Delegates and events can provide a nice way to create loose coupling, which can lead to better and easier to maintain ways to allow your objects to communicate between one another. Be careful with direct object calls and tight coupling as they can reduce encapsulation and lead to tricky debugging problems down the road.

Thesis Current Resources and Citations

October 31, 2019

Academic Resources

Current Citations

I just wanted to have a list of all my current sources I have compiled that may be useful for my thesis here. This list will be modified over time as I dtermine which topics I will not get to in my thesis, and which topics need more support, but this is a pretty good foundation for now.

  1. [1]J. L. Anderson and M. Barnett, “Learning Physics with Digital Game Simulations in Middle School Science,” Journal of Science Education and Technology, vol. 22, no. 6, pp. 914–926, Dec. 2013.
  2. [2]M.-V. Aponte, G. Levieux, and S. Natkin, “Measuring the level of difficulty in single player video games,” Entertainment Computing, vol. 2, no. 4, pp. 205–213, Jan. 2011.
  3. [3]S. Arnab et al., “Mapping learning and game mechanics for serious games analysis: Mapping learning and game mechanics,” British Journal of Educational Technology, vol. 46, no. 2, pp. 391–411, Mar. 2015.
  4. [4]E. Butler, E. Andersen, A. M. Smith, S. Gulwani, and Z. Popović, “Automatic Game Progression Design through Analysis of Solution Features,” 2015, pp. 2407–2416.
  5. [5]M. J. Callaghan, K. McCusker, J. L. Losada, J. Harkin, and S. Wilson, “Using Game-Based Learning in Virtual Worlds to Teach Electronic and Electrical Engineering,” IEEE Transactions on Industrial Informatics, vol. 9, no. 1, pp. 575–584, Feb. 2013.
  6. [6]M. Callaghan, M. Savin-Baden, N. McShane, and A. G. Eguiluz, “Mapping Learning and Game Mechanics for Serious Games Analysis in Engineering Education,” IEEE Transactions on Emerging Topics in Computing, vol. 5, no. 1, pp. 77–83, Jan. 2017.
  7. [7]D. B. Clark, B. C. Nelson, H.-Y. Chang, M. Martinez-Garza, K. Slack, and C. M. D’Angelo, “Exploring Newtonian mechanics in a conceptually-integrated digital game: Comparison of learning and affective outcomes for students in Taiwan and the United States,” Computers & Education, vol. 57, no. 3, pp. 2178–2195, Nov. 2011.
  8. [8]M. M. Cruz-Cunha, Ed., Handbook of Research on Serious Games as Educational, Business and Research Tools. IGI Global, 2012.
  9. [9]A. A. Deshpande and S. H. Huang, “Simulation games in engineering education: A state-of-the-art review,” Computer Applications in Engineering Education, vol. 19, no. 3, pp. 399–410, Sep. 2011.
  10. [10]M. D. Dickey, “Game design narrative for learning: Appropriating adventure game design narrative devices and techniques for the design of interactive learning environments,” Educational Technology Research and Development, vol. 54, no. 3, pp. 245–263, 2006.
  11. [11]J. Dormans and S. Bakkes, “Generating Missions and Spaces for Adaptable Play Experiences,” IEEE Transactions on Computational Intelligence and AI in Games, vol. 3, no. 3, pp. 216–228, Sep. 2011.
  12. [12]M. Ebner and A. Holzinger, “Successful implementation of user-centered game based learning in higher education: An example from civil engineering,” Computers & Education, vol. 49, no. 3, pp. 873–890, Nov. 2007.
  13. [13]M. Eraut, “Non-formal learning and tacit knowledge in professional work,” British Journal of Educational Psychology, vol. 70, no. 1, pp. 113–136, Mar. 2000.
  14. [14]D. Farrell and D. Moffat, “Cognitive Walkthrough for Learning Through Game Mechanics,” in European Conference on Games Based Learning, 2013, p. 163.
  15. [15]H. Fernandez, K. Mikami, and K. Kondo, “Adaptable game experience through procedural content generation and brain computer interface,” 2016, pp. 1–2.
  16. [16]A. Foster, “Games and motivation to learn science: Personal identity, applicability, relevance and meaningfulness,” Journal of Interactive Learning Research, vol. 19, no. 4, p. 597, 2008.
  17. [17]C. Franzwa, Y. Tang, A. Johnson, and T. Bielefeldt, “Balancing Fun and Learning in a Serious Game Design:,” International Journal of Game-Based Learning, vol. 4, no. 4, pp. 37–57, Oct. 2014.
  18. [18]J. Fraser, M. Katchabaw, and R. E. Mercer, “A methodological approach to identifying and quantifying video game difficulty factors,” Entertainment Computing, vol. 5, no. 4, pp. 441–449, Dec. 2014.
  19. [19]J. P. Gee, “What video games have to teach us about learning and literacy,” Computers in Entertainment, vol. 1, no. 1, p. 20, Oct. 2003.
  20. [20]J. P. Gee, “Good video games and good learning,” in Phi Kappa Phi Forum, 2005, vol. 85, p. 33.
  21. [21]B. Gregorcic and M. Bodin, “Algodoo: A Tool for Encouraging Creativity in Physics Teaching and Learning,” The Physics Teacher, vol. 55, no. 1, pp. 25–28, Jan. 2017.
  22. [22]R. H. Mulder, “Exploring feedback incidents, their characteristics and the informal learning activities that emanate from them,” European Journal of Training and Development, vol. 37, no. 1, pp. 49–71, Jan. 2013.
  23. [23]T. Hainey, T. M. Connolly, E. A. Boyle, A. Wilson, and A. Razak, “A systematic literature review of games-based learning empirical evidence in primary education,” Computers & Education, vol. 102, pp. 202–223, Nov. 2016.
  24. [24]P. Hämäläinen, X. Ma, J. Takatalo, and J. Togelius, “Predictive Physics Simulation in Game Mechanics,” 2017, pp. 497–505.
  25. [25]M. Hendrikx, S. Meijer, J. Van Der Velden, and A. Iosup, “Procedural content generation for games: A survey,” ACM Transactions on Multimedia Computing, Communications, and Applications, vol. 9, no. 1, pp. 1–22, Feb. 2013.
  26. [26]R. Hunicke, M. LeBlanc, and R. Zubek, “MDA: A Formal Approach to Game Design and Game Research,” p. 5.
  27. [27]I. Iacovides, P. McAndrew, E. Scanlon, and J. Aczel, “The gaming involvement and informal learning framework,” Simulation & Gaming, vol. 45, no. 4–5, pp. 611–626, 2014.
  28. [28]P. Lameras, S. Arnab, I. Dunwell, C. Stewart, S. Clarke, and P. Petridis, “Essential features of serious games design in higher education: Linking learning attributes to game mechanics: Essential features of serious games design,” British Journal of Educational Technology, vol. 48, no. 4, pp. 972–994, Jun. 2017.
  29. [29]H. B. Lisboa, “3D VIRTUAL ENVIRONMENTS FOR MANUFACTURING AUTOMATION,” vol. 6, p. 9, 2014.
  30. [30]R. Lopes, T. Tutenel, and R. Bidarra, “Using gameplay semantics to procedurally generate player-matching game worlds,” 2012, pp. 1–8.
  31. [31]S. D. Mohanty and S. Cantu, “Teaching introductory undergraduate physics using commercial video games,” Physics Education, vol. 46, no. 5, p. 570, 2011.
  32. [32]S. Moser, J. Zumbach, and I. Deibl, “The effect of metacognitive training and prompting on learning success in simulation-based physics learning,” Science Education, vol. 101, no. 6, pp. 944–967, Nov. 2017.
  33. [33]C. Peach, D. Rohrick, D. Kilb, J. Orcutt, E. Simms, and J. Driscoll, “DEEP learning: Promoting informal STEM learning through ocean research videogames,” in Oceans-San Diego, 2013, 2013, pp. 1–4.
  34. [34]J.-N. Proulx, M. Romero, and S. Arnab, “Learning Mechanics and Game Mechanics Under the Perspective of Self-Determination Theory to Foster Motivation in Digital Game Based Learning,” Simulation & Gaming, vol. 48, no. 1, pp. 81–97, Feb. 2017.
  35. [35]M. Qian and K. R. Clark, “Game-based Learning and 21st century skills: A review of recent research,” Computers in Human Behavior, vol. 63, pp. 50–58, Oct. 2016.
  36. [36]S. Sampayo-Vargas, C. J. Cope, Z. He, and G. J. Byrne, “The effectiveness of adaptive difficulty adjustments on students’ motivation and learning in an educational computer game,” Computers & Education, vol. 69, pp. 452–462, Nov. 2013.
  37. [37]M. Shaker, M. H. Sarhan, O. A. Naameh, N. Shaker, and J. Togelius, “Automatic generation and analysis of physics-based puzzle games,” 2013, pp. 1–8.
  38. [38]N. Shaker, M. Nicolau, G. N. Yannakakis, J. Togelius, and M. O’Neill, “Evolving levels for Super Mario Bros using grammatical evolution,” 2012, pp. 304–311.
  39. [39]G. Smith, “Understanding procedural content generation: a design-centric analysis of the role of PCG in games,” 2014, pp. 917–926.
  40. [40]V. Wendel, M. Gutjahr, S. Göbel, and R. Steinmetz, “Designing collaborative multiplayer serious games: Escape from Wilson Island—A multiplayer 3D serious game for collaborative learning in teams,” Education and Information Technologies, vol. 18, no. 2, pp. 287–308, Jun. 2013.
  41. [41]K. A. Wilson et al., “Relationships Between Game Attributes and Learning Outcomes: Review and Research Proposals,” Simulation & Gaming, vol. 40, no. 2, pp. 217–266, Apr. 2009.
  42. [42]J.-C. Woo, “Digital Game-Based Learning Supports Student Motivation, Cognitive Success, and Performance Outcomes.,” Journal of Educational Technology & Society, vol. 17, no. 3, 2014.
  43. [43]G. N. Yannakakis and J. Togelius, “Experience-Driven Procedural Content Generation,” IEEE Transactions on Affective Computing, vol. 2, no. 3, pp. 147–161, Jul. 2011.

Drawing and Animating 2D Pixel Art in Photoshop

October 30, 2019

Pixel Art for Games

Photoshop Tutorials

How To Draw Pixel Art | Tutorial

Tutorial #1 – Link

By: TipTut


How To Animate Pixel Art | Tutorial

Tutorial #2 – Link

By: TipTut


I will most likely be doing some more work on some of my older 2D pixel art games, so I wanted to brush up and get some more basic tutorials on pixel art in photoshop to get me back up to speed and hopefully learn some new things along the way. This creator had the benefit of following up with an animation video as well, which is a nice bonus to tag on. I may need to reset my Photoshop settings so it is proper for pixel art again.

UnityLearn – Beginner Programming – Tips & Considerations – Pt. 05

October 29, 2019

Beginner Programming: Unity Game Dev Courses

Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Tips & Considerations

Unity’s Order of Events

Unity’s Order of Events:
Awake : OnEnable : Start : Update : OnDisable : OnDestroy

  • Awake: first function called when object is instantiated; true whether added to scene in editor or instantiated in code
  • OnEnable: event; fired when enabled gameObject is instantiated or when a disabled object is enabled
  • Start: after Awake, and OnEnable, but before the 1st frame update
  • Update: happens every frame after initialization methods
    • FixedUpdate: frame-independent and occurs before physics calculations are performed
    • LateUpdate: called once per frame after update method has completed execution
  • OnDisable: event; fires when object is disabled, or before it is destroyed
  • OnDestroy: execute when object is destroyed in code, or when scene containing it is unloaded

Reference Caching: creating a field for a reference and getting that reference once during initialization to use whenever that object is needed

This information is very useful to understand when you start referencing a lot of objects throughout your code and have a lot of pieces working together. This will help with avoiding errors, and debugging when those types of issues do come up. This is something I ran into a lot when working on my scene manager project as getting references was a bit of a pain and the timing was very crucial.

Using Attributes

Attributes (C#): powerful method of associating metadata, or declarative information, with code
Unity provides number of attributes to avoid mistakes and enhance functionality of inspector.

Some common attributes used in Unity include:

  • SerializeField: makes a field accessible to the inspector
  • Range
  • Header
  • Space
  • RequireComponent

Public fields are inherently accessible by the inspector, but many other access modifiers hide the field from the inspector (like private). To get around this, you add the SerializeField attribute.

The RequireComponent attribute takes in a type and automatically adds that component type to the same gameObject whenever this object is placed. It also ensures that the other component cannot be removed while this component exists on the gameObject.

How to Draw with Colored Pencils

October 28, 2019

Colored Pencils

Better Fundamentals

DO’S & DON’TS: How to Draw with Colored Pencils

Tutorial #1 – Link

By: Kirsty Partridge Art


I am trying to get back into some activities I liked doing in the past but have not found time to do them recently. Using colored pencils was always something I enjoyed, so I would like to get back to using them to just do some simple coloring and sketching. This seemed like a good time to improve on how I did this as well, so this video seemed like a good start to learn some basics to work on if you want to actually get better at using colored pencils.

UnityLearn – Beginner Programming – Delegates, Events, and Actions – Pt. 04

October 25, 2019

Beginner Programming: Unity Game Dev Courses

Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Delegates, Events, and Actions

Delegates

Delegate: in C#, a type designed to hold a reference to a method in a delegate object

  • Delgates are created using the “delegate” keyword.
  • They are defined by their signature, meaning their return type.
  • Finally they have parameters which they take in, similar to methods.

Using a delegate allowed us to parameterize a method. Using the delegate as a parameter for a method also allows us to use any method which matches that delegate’s signature to satisfy the parameter.

These are some snippets from two scripts, GameSceneController and EnemyController, that show some basics of utilizing delegates:

– In GameSceneController script
public delegate void TextOutputHandler(string text);

public void OutputText (string output)
{
Debug.LogFormat(“{0} output by GameSceneController”, output);
}

– In EnemyController script
void Update()
{
MoveEnemy(gameSceneController.OutputText);
}

private void MoveEnemy(TextOutputHandler outputHandler)
{
transform.Translate(Vector2.down * Time.deltaTime, Space.World);

float bottom = transform.position.y – halfHeight;

if(bottom <= -gameSceneController.screenBounds.y)
{
outputHandler(“Enemy at bottom”);
gameSceneController.KillObject(this);
}
}

For example, in our case we created a public void delegate with a string parameter called TextOutputHandler. Then another one of our methods, MoveEnemy, took a TextOutputHandler as a parameter, named outputHandler. Any method matching the signature of the delegate (in this case, public void with input parameter string) can satisfy the input parameter for the MoveEnemy method. As can be seen in the example, whatever method is passed in will be given the string “Enemy at bottom”.

A delegate used this way is commonly known as a “Callback”.

Events

C# Events: enable a class or object to notify other classes or objects when something of interest occurs.
Publisher: class that sends the event
Subscriber: class that receives/handles the event

Things like Unity’s UI elements use events inherently. For example, the Button script uses events to tell scripts when to activate when a button is clicked. This is different from a basic way of doing inputs which checks every frame if a button is being pressed (which is called “polling”). This is also why creating UI elements in Unity automatically creates an EventSystem object for you.

In C#, events are declared using the “event” keyword, and all events have an underlying delegate type.
C# events are multicast delegates.
Multicast delegate: delegate that can reference multiple methods

To help with my understanding, I tried testing the setup without having an EnemyController parameter to see why it was needed. I discovered it was necessary to pass along the reference so the small event system knew which object to destroy when calling the EnemyAtBottom method. Using Destroy(this.gameObject) or Destroy(gameObject) both just destroyed the SceneController as opposed to the individual enemies. This also helped me understand that adding a method to an event does not pass the method over as an equivalent, it simply means that when that event is called, that any methods assigned to it are also called in their current location in a class. So even though the event was being called in the EnemyController script, the method I added to it was still called within the GameSceneController script, which makes sense.

Example:

In EnemyController script


public delegate void EnemyEscapedHandler(EnemyController enemy);

public class EnemyController : Shape, IKillable
{
public event EnemyEscapedHandler EnemyEscaped;

void Update()
{
MoveEnemy();
}

private void MoveEnemy()
{
transform.Translate(Vector2.down * Time.deltaTime, Space.World);

float bottom = transform.position.y – halfHeight;

if(bottom <= -gameSceneController.screenBounds.y)
{
if(EnemyEscaped != null)
{
EnemyEscaped(this);
}
// Can be simplified to:
// EnemyEscaped?.Invoke(this);
}
}
}

In GameSceneController script:


public class GameSceneController : MonoBehaviour
{
private IEnumerator SpawnEnemies()
{
WaitForSeconds wait = new WaitForSeconds(2);

while (true)
{
float horizontalPosition = Random.Range(-screenBounds.x, screenBounds.x);
Vector2 spawnPosition = new Vector2(horizontalPosition, screenBounds.y);

EnemyController enemy = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity);

enemy.EnemyEscaped += EnemyAtBottom;

yield return wait;
}
}

private void EnemyAtBottom(EnemyController enemy)
{
Destroy(enemy.gameObject);
Debug.Log(“Enemy escaped”);
}
}

I’ve simplified the scripts down to just the parts dealing with the events to make it easier to follow. As I understand it, we create the delegate: public delegate void EnemyEscapedHandler(EnemyController enemy) in the EnemyController script (but outside of the EnemyController class). Within the EnemyController class, we create an event of the type EnemyEscapedHandler, so this event can take on methods with the same signature as EnemyEscapedHandler. Within the MoveEnemy method, we invoke the EnemyEscaped event and satisfy its parameters by passing in this, which is the unique instance of the EnemyController script (after checking that there is a method assigned to this event).

Then in the GameSceneController script, we see that when we instantiate an enemy, we keep a reference to its EnemyController script. This is to assign the EnemyAtBottom method to each one’s EnemyEscaped event. Now anytime EnemyEscaped is called in the EnemyController script, it will then call the EnemyAtBottom script here, passing whatever parameter it (EnemyEscaped) has to the parameter for EnemyAtBottom. In this case, passing this in EnemyEscaped ensures that EnemyAtBottom knows which enemy to destroy.

Actions

Actions (C#): types in the System namespace that allow you to encapsulate methods without explicitly defining a delegate
In fact, they are delegates
Actions can be generic

An Action is just an event delegate that does not need another delegate to be created first to be used as a reference. The Action itself determines what parameters are necessary for the passed methods.

Thesis Project – Concepts for System to Control Script Timings

October 24, 2019

Working with the HFFWS

System for Properly Timing Running of Scripts

Discovering the Timing Issues with the HFFWS Tools

While working with the Rope script in the Human API, I was again encountering timing issues when trying to accomplish tasks through script. I was having trouble instantiating and connecting rigid bodies to the ends of the rope in script. I ran several tests to confirm the timing issues I was having again.

Test #1
  • 2 Rigid Body objects exist in scene
  • Script has 2 gameObject references for those objects
  • Script sets startBody and endBody references to those objects at Start

This did not work. The references could be seen as being properly assigned in the Rope script in the editor, but that connection was being made too late and having no affect on the objects.

Test #2
  • 2 Rigid Body objects exist in scene
  • Script has 2 gameObject references for those objects
  • Script sets startBody and endBody references to those objects at Awake

This did have the intended result of connecting the rigid body objects to the rope.

Test #3
  • All done in Awake
  • 2 Rigid Body objects instantiated into scene
  • Script sets startBody and endBody references to those objects at Awake

This did work once, although I had some issues repeating it afterward. The objects were prefabs which were instantiated in Awake. GameObject references were created for these, and used for the values of the Rope script startBody and endBody, all done in Awake as well.

Test #4
  • Start or Awake is irrelevant here
  • Object with Rope script starts deactivated
  • 2 Rigid Body objects instantiated into scene
  • Script sets startBody and endBody references to those objects
  • Rope object is then activated

This is the best solution that works, as it can be done in Awake or Start. This leads to a much more controllable environment which is ideal for our system. This will be the foundation for the main systematic approach.

System for Controlling Awake Calls for HFFWS Objects

Deactivating/Activating Objects or Enabling/Disabling Scripts

The combinations of all of these tests, most notably Test #4, showed that some important connections are made during the Awake phase of many HFFWS objects. It is important to note that Test #4 specifically indicates that a lot of the important functionality may be happening in the Awake method of the scripts of the individual objects themselves (which is important to differentiate from some system wide class that is making connections in Awake). This important differntiation leads to this option of simply deactivating objects with HFFWS scripts (or possibly disabling the scripts specifically) to force their Awake methods not to run before my systems are ready for them. I can run my scripts, and then activate the HFFWS objects so their Awake methods run at that point instead.

This concept seems the most promising for safely and consistently controlling the timing of my script elements versus those of the HFFWS objects (since I do not have access to their core scripts). As this is a common issue I have run into with many objects, it makes sense to make a small script to attach to any prefabs I will be instantiating which can just hold references to gameObjects and/or scripts that I will need to have deactivated or disabled initially, and then activate or enable after everything is properly set.

Script Execution Order

This was actually the first idea to get around these timing issues, but it seems less safe so it will most likely only be used as a last resort. The HFFWS package already has a lot of various scripts ordered in the script execution order of Unity, so there is already a precedent set by them for when a lot of things should run relative to each other.

To test that this worked in the first place, I ran a test with everything being set in Awake not working but moving it to the first script in the script execution order actually made it work. This was a similar test with connecting rigid bodies to a Rope script object.

This is not an ideal process to use however as it can easily lead to weird behavior that is very hard to debug and will not scale well in general. Because of these factors, I will mostly be looking to expand upon the other system concept.