{"author_link":"\/users\/huvaakoodia","author_name":"HuvaaKoodia","author_uid":"huvaakoodia","comments":[],"epoch":1503338946,"event":"LD39","format":"md","ldjam_node_id":49136,"likes":7,"metadata":{"p_key":"100936","p_author":"HuvaaKoodia","p_authorkey":"1000996","p_urlkey":"316646","p_title":"Event database in Unity","p_cat":"LDJam ","p_event":"LD39","p_time":"1503338946","p_likes":"7","p_comments":"0","p_status":"WAYBACK","us_key":"1000996","us_name":"HuvaaKoodia","us_username":"huvaakoodia","event_start":"1501200000","event_key":"74","event_name":"LD 39"},"node":{"_collation":{"body_sanitizer":"TextUtils::SanitizeHTML via existing importer","event":"LD39","removed_author":false},"_superparent":32802,"_trust":7,"author":996,"body":"I used two useful, yet simple, data type tricks in [Mr Mayor's Energy Crisis](https:\/\/ldjam.com\/events\/ludum-dare\/39\/mr-mayors-energy-crisis). The first one [AnimationCurves for NonAnimations](https:\/\/ldjam.com\/events\/ludum-dare\/39\/mr-mayors-energy-crisis\/using-animationcurves-for-nonanimations), the second one you're looking at right now.\n\n### Events!\n\nThere were many of them, 35 to be precise, and they needed to be stored somewhere. In a longer project I might have used JSON, CSV or one of my own custom formats from the past, but for a jam I needed something simpler and most importantly quicker to setup.  \n:thinking::bulb: Serialized objects!\n\n### Object?\n\nC# objects are made out of a class like this one.\n![EventData1.PNG](\/\/\/raw\/4e3\/z\/8f79.png)\n*You should know this.*\n\nIf you make a public object out of the class, it won't show up in the inspector though.\n\n![EventDatabase1.PNG](\/\/\/raw\/4e3\/z\/8f7a.png)\n*Trust me, nothing will show up.*\n\n![Inspector1.PNG](\/\/\/raw\/4e3\/z\/8f7b.png)\n*What did I tell you!*\n\n### Serialized?\n\nBut if you add the [System.Serializable] attribute to the class, things will change.\n\n![EventData2.PNG](\/\/\/raw\/4e3\/z\/8f7c.png)\n*Such a little thing*\n\n![Inspector2.PNG](\/\/\/raw\/4e3\/z\/8f7d.png)\n*Makes all the difference*\n\n### What now, boss?\n\nWell, lets make an array of events.\n\n![EventDatabase2.png](\/\/\/raw\/4e3\/z\/8f88.png)\n*More events!*\n\nAnd add a text variable to the event class with the [TextArea] attribute.\n\n![EventData3.PNG](\/\/\/raw\/4e3\/z\/8f7f.png)\n*You'll see what the attribute does in the next bit.*\n\nNow editing multiple events in the editor is smooth sailing\n\n![Inspector3.PNG](\/\/\/raw\/4e3\/z\/8f80.png)\n*It expands the text area! Who would have thought!*\n\n### So ah, how to use this thing?\n\nWe need to be able to find events from the database and easily access it. A linear search function will do as a quick and dirty solution for the first issue.\n\n![EventDatabase3.PNG](\/\/\/raw\/4e3\/z\/8f81.png)\n*Sometimes a simple for loop is all you need.*\n\nThe second problem is trickier, but turning the database into a singleton takes care of it.\n\n![EventDatabase4.PNG](\/\/\/raw\/4e3\/z\/8f82.png)\n*If you don't know what this means, just wait and see.*\n\nAnd here is how to use the database from any script.\n\n![Usage1.PNG](\/\/\/raw\/4e3\/z\/8f85.png)\n*At this point you can do whatever you want with the data in the event object.*\n\n### Possible improvements?\n\nThere are a few:\n* Use an enum to find events instead of the name string. This cuts down on typo related bugs.\n* Rather than using linear search you can sort the array at Start() based on the enum and then find events via direct index at O(1) time. *Lightning fast*, as they say in the industry.\n\n### Conclusion\n\nSimple to code, easy to edit. That's about it and here is the [code](https:\/\/pastebin.com\/D7yPBiZA).\n\nCheers!\n","comments":0,"created":"2017-08-21T14:22:35Z","files":[],"files-timestamp":0,"id":49136,"love":7,"love-timestamp":"2017-08-23T06:48:05Z","meta":[],"modified":"2017-08-23T06:48:05Z","name":"Event database in Unity","node-timestamp":"2017-08-21T18:09:06Z","parent":42158,"parents":[1,5,9,32802,42158],"path":"\/events\/ludum-dare\/39\/mr-mayors-energy-crisis\/event-database-in-unity","published":"2017-08-21T18:09:06Z","scope":"public","slug":"event-database-in-unity","subsubtype":"","subtype":"","type":"post","version":141097},"node_metadata":{"n_key":"49136","n_urlkey":"316646","n_parent":"42158","n_path":"\/events\/ludum-dare\/39\/mr-mayors-energy-crisis\/event-database-in-unity","n_slug":"event-database-in-unity","n_type":"post","n_subtype":"","n_subsubtype":"","n_author":"996","n_created":"1503325355","n_modified":"1503470885","n_version":"141097","n_status":"WAYBACK"},"source_url":"https:\/\/ldjam.com\/events\/ludum-dare\/39\/mr-mayors-energy-crisis\/event-database-in-unity","text":"I used two useful, yet simple, data type tricks in [Mr Mayor's Energy Crisis](https:\/\/ldjam.com\/events\/ludum-dare\/39\/mr-mayors-energy-crisis). The first one [AnimationCurves for NonAnimations](https:\/\/ldjam.com\/events\/ludum-dare\/39\/mr-mayors-energy-crisis\/using-animationcurves-for-nonanimations), the second one you're looking at right now.\n\n### Events!\n\nThere were many of them, 35 to be precise, and they needed to be stored somewhere. In a longer project I might have used JSON, CSV or one of my own custom formats from the past, but for a jam I needed something simpler and most importantly quicker to setup.  \n:thinking::bulb: Serialized objects!\n\n### Object?\n\nC# objects are made out of a class like this one.\n![EventData1.PNG](\/\/\/raw\/4e3\/z\/8f79.png)\n*You should know this.*\n\nIf you make a public object out of the class, it won't show up in the inspector though.\n\n![EventDatabase1.PNG](\/\/\/raw\/4e3\/z\/8f7a.png)\n*Trust me, nothing will show up.*\n\n![Inspector1.PNG](\/\/\/raw\/4e3\/z\/8f7b.png)\n*What did I tell you!*\n\n### Serialized?\n\nBut if you add the [System.Serializable] attribute to the class, things will change.\n\n![EventData2.PNG](\/\/\/raw\/4e3\/z\/8f7c.png)\n*Such a little thing*\n\n![Inspector2.PNG](\/\/\/raw\/4e3\/z\/8f7d.png)\n*Makes all the difference*\n\n### What now, boss?\n\nWell, lets make an array of events.\n\n![EventDatabase2.png](\/\/\/raw\/4e3\/z\/8f88.png)\n*More events!*\n\nAnd add a text variable to the event class with the [TextArea] attribute.\n\n![EventData3.PNG](\/\/\/raw\/4e3\/z\/8f7f.png)\n*You'll see what the attribute does in the next bit.*\n\nNow editing multiple events in the editor is smooth sailing\n\n![Inspector3.PNG](\/\/\/raw\/4e3\/z\/8f80.png)\n*It expands the text area! Who would have thought!*\n\n### So ah, how to use this thing?\n\nWe need to be able to find events from the database and easily access it. A linear search function will do as a quick and dirty solution for the first issue.\n\n![EventDatabase3.PNG](\/\/\/raw\/4e3\/z\/8f81.png)\n*Sometimes a simple for loop is all you need.*\n\nThe second problem is trickier, but turning the database into a singleton takes care of it.\n\n![EventDatabase4.PNG](\/\/\/raw\/4e3\/z\/8f82.png)\n*If you don't know what this means, just wait and see.*\n\nAnd here is how to use the database from any script.\n\n![Usage1.PNG](\/\/\/raw\/4e3\/z\/8f85.png)\n*At this point you can do whatever you want with the data in the event object.*\n\n### Possible improvements?\n\nThere are a few:\n* Use an enum to find events instead of the name string. This cuts down on typo related bugs.\n* Rather than using linear search you can sort the array at Start() based on the enum and then find events via direct index at O(1) time. *Lightning fast*, as they say in the industry.\n\n### Conclusion\n\nSimple to code, easy to edit. That's about it and here is the [code](https:\/\/pastebin.com\/D7yPBiZA).\n\nCheers!\n","title":"Event database in Unity","wayback_source":[]}