Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by ProtoTerminator · Mar 27, 2014 at 11:32 PM · classdata

many "characters" data

I'm making a game that will have many different characters that all have the same variables: move, range, attack, defense, maxHealth. Once created, none of these variables will change. For instance one will have

 byte move = 3;
 byte range = 2;
 byte attack = 2;
 byte defense = 5;
 byte maxHealth = 5;

My question is what's the best way to go about storing this data? Should I make a class for each character and pull it from there? Or just put the data into the variables as they are instantiated through direct code (which is how I'm doing it atm), or some other way?

Comment
Add comment · Show 1
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 rakkarage · Mar 28, 2014 at 01:09 AM 0
Share

http://gameprogram$$anonymous$$gpatterns.com/component.html

http://gameprogram$$anonymous$$gpatterns.com/type-object.html

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by dscroggi · Mar 28, 2014 at 12:55 AM

Great question

Definitely use classes, if the characters are different types use an abstract base class. The absolute best way is to use a database of course, next best would be a less permanent form of storage like xml, and worst would be hard coding constants. However depending on what you're doing any of those could be good.

If youre 100% sure that they will never ever change just put the values in the constructors.

For example abstract base class Character has the members listed above and accepts them in the ctor. Derived class Knight passes in his values etc. So to make it last longer and be more flexible you can put together a unity editor window with some inputs that export to xml with for each character type, then in your ctor you can read the xml/database.

Using a base class like that allows for methods that accept Character parameters so you can do your calculations in one place instead of having separate/duplicated ones for each character.

To take it one step further use a list of stats instead of a finite set of members. If you make your list recursive that opens a lot of possibilities as well cause now you have a character with a sword with an enchantment which each add to attack so in your stats class just either inherit from a stat list like class Stat : List or contain it and provide a method that calculates the value. This way when you call your Character.Fight() or whatever it can sum up all his equips/buffs and things attached to those.

Probably make some interface like ITarget which has that calculation method so in unity you can just to a raycast on some enemies and get a list of ITarget and pass each one some combat value. Proceed to epic developer status.

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 ProtoTerminator · Mar 28, 2014 at 06:03 AM 0
Share

Could you show me a sample of code showing how to do the xml thing? I've never used it before. Also, how will a unity editor window help me? (I've never done that before, either).

Thanks

avatar image ProtoTerminator · May 17, 2014 at 11:29 PM 0
Share

Okay, I looked up how to use xml. It seems to only work with primitive types. How would I save a texture or other non primitive data? And it only works with public attributes. How can I use it with private attributes? (that's more important as public is too easily abused)

avatar image
0

Answer by getyour411 · May 17, 2014 at 11:40 PM

XML or a database for this use case is overkill. Use the code as you are if that's working for you, and you say it is.

Comment
Add comment · Show 1 · 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 ProtoTerminator · May 22, 2014 at 07:18 AM 0
Share

It might be overkill, but I'm trying to be efficient as possible, and I'm still learning how to do that.

avatar image
0

Answer by Andres-Fernandez · May 22, 2014 at 07:23 AM

If all your characters have the same variables you are just defining a class. Create a Character class with those fields and set them in the constructor:

 public Class Character {
    byte move = 3;
    byte range = 2;
    byte attack = 2;
    byte defense = 5;
    byte maxHealth = 5;
 
    public Character(byte setMove, byte setRange, byte setAttack, byte setDefense, byte setMaxHealth) {
       move = setMove;
       range = setRange;
       attack = setAttack;
       defense = setDefense;
       maxHealth = setMaxHealth;
    }
 
 }

Then you can create different characters like this:

 Character myCharacterA = new Character(3, 2, 2, 5, 5);
 Character myCharacterB = new Character(3, 4, 1, 2, 4);
Comment
Add comment · Show 1 · 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 ProtoTerminator · May 26, 2014 at 02:11 AM 1
Share

Yeah, that's exactly how I was doing it before. I was just trying to make it last for more than one instance of the game, and able to transfer them between scenes. It also helps that other members of my $$anonymous$$m can more easily tweek the numbers without going into the code.

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reading and Storing External Data into Memory (From Text) 1 Answer

Database-like arrays 1 Answer

Trying to learn Generics 1 Answer

How to save and load any data type? 1 Answer

Lists and Structs instead of Arrays? 4 Answers


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