Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Fikule · Apr 17, 2016 at 12:17 PM · c#arraystorage

Storing and Accessing Abilities

Hi, I am making a card system in c# and want to assign the cards attributes based on an ID as a new card object is created.

Basically each ability will have (roughly) Name Ability Image Description Text Target Method (how you select a target) Ability Method (the ability does stuff here)

I mean, do I just make an ability class that stores this info and then generate an array of abilities in one long file of code?

Should each ability have a class of it's own? Should I somehow implement it with prefabs?

I can think of ways to do it, but they seem inefficient and weird. I am just wondering what the best way would be?

My other issue is I want this to be pretty modular as I might want to use it to store character abilities in another game for example.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MrDiVolo · Apr 17, 2016 at 03:36 PM

There are three potential ways I would do this; a class array in the Inspector, a database, or a text file. The first of those is the easiest, but if you're planning a large project and you're going to have a lot of these cards, I'd try option number 2, and a text file as your last resort (as it's the same as a database but with less structure).


Inspector example

alt text alt text

In the above example, I've created a CardManager that stores an array of Card. Each card has the example fields you listed above, with the exception of AbilityMethod; I didn't know if this and TargetMethod were meant as actually methods, however, if they were meant as fields, you could use a switch statement on the Ability enum, to determine the method? (just a suggestion as I don't know much about the project)


If you want an example of a database then I can try to draw one up for you, but the basics of it would be a setup similar to the classes listed above, a table each for; Card, Ability and Targetting. (In modern database programs you should be able to link the image field in the table to an actual image on the HDD, but it depends what you're working with). Once read using something like OLE DB, you will still have to load the images using the string that will likely be the actual version of the 'image' used in the database.

And finally for the text (CSV) file you would have to use a string path for the location of the image instead, and use a StreamReader for reading/writing.

Update - 17/04/16 (20:50)

As @Soraphis mentioned, using an Excel spreadsheet/Google Sheets file to create the data would be a good idea. Then export the spreadsheet as a .csv file (alternatively, as I'm about to explain, you could just use the CardManager, and then Serialize the data). Next I would create a custom inspector, that allowed you to browse for this CSV file, and import the data into a List of your Card class. From here you can then re-export the data read in, but in a binary file format. This secures the data as only you know what the data structure of the Card class is, how you wrote this list to the file, and how to read it back out. Below is an example a full solution for this, so if you want to try it for yourself first then here are the links;

  1. StreamReader

  2. BinaryFormatter (Serialize & Deserialize)

The derived solution would take up way too much room, as such here is a video of me walking through what I came up with;

https://youtu.be/eOoOeQNHfos

As for directly pointing to methods, I still think it would be best to just have a separate class that knows what methods each enum ability and target point to.

I hope this helped.


cardgameinspector.png (24.6 kB)
cardgamecode.png (33.1 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fikule · Apr 17, 2016 at 04:49 PM 0
Share

Heya, this is a very concise and informative answer, so thank you!

Ideally I would want a direct pointer to the method used for targeting and for the ability itself. i.e. I might make a card called Fireball that uses Target_Enemy() and Ability_Fireball() that would use those when perfor$$anonymous$$g the action, though I would likely have to make it more complex as it goes.

In terms of the database, would there be any risk of the player modifying values within it? For example, if I ended up storing the damage of the Fireball card in the database, could they go in and edit it? $$anonymous$$y thought was I would have to have all of the data built into the executable.

avatar image Soraphis Fikule · Apr 17, 2016 at 06:06 PM 0
Share

if all your data is local on the player's system, theres always a method to cheat. so i would not bother with that. (you could implement some checksum tests or whatever, but given enough time theres always a way around)

your card has knows the primary key of the ability. search the database for this key and fill a ability-objekt with the data of the database.


you'll run into problems if you want to do animation and stuff, when using an ability. there are some solutions: hardcode those animations in an AbilityAnimation class finding them via reflections by the ability's name

use some kind of scripting language (e.g. LUA) to script the animations. save those scripts as strings in the database(or where ever, see my other answer)

there may be more solutions (e.g. animation assets) but i think the above solutions would be the best/easiest

avatar image
0

Answer by Soraphis · Apr 17, 2016 at 05:55 PM

As an alternative @MrDiVolo's Textfile solution:

Create a class "AbilityDatabase" extending from ScriptableObject. the database holds an array of "Ability" objects (your ability class), which should be serializeable.

add the "createassetmenu" attribute to the AbilityDatabase and create an instance of it. now you have a prefab which holds all your abilities.


well, this solution has the same flaws as the database solution and the the textfile solution.

what you COULD do:

create a excel/google-docs table where you hold all your data, export it to csv and as text asset into unity, parse this file and generate a c# file with the abilities in it.

if you add or change an ability just generate the c# file new. you can access all abilities from code.

i'd prefere this solution instead of writing the code file all myself

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Fastest way to store huge amounts of data? 2 Answers

C# declaring an array of transform arrays 1 Answer

C# Set Current Array GameObject to Active 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges