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 F-N · Feb 07, 2015 at 05:43 PM · c#classdatastruct

Best way to store a lot of data by script

Hi there!

I am making a kinda Age of Empires game.(I know this is too big for me but nvm) and I would like to store the data of all the different units inside of a script. The point is that most of the units can be made by most of the civilizations but not all units can be made by all civilizations. Also I would like to store the sprites in this script, but eacht civilization should have a different sprite for maybe the same units. For example: I would like to have a Turkish worker with a different sprite as a Roman worker.

Should I use structs or classes? And should I do this in 1 class of just in 1 struct? I think I should prefer the class because of the cheap data usage but I'm not an expert. :P And would it be smart to use the Resources folder for this?

Any help is appreciated!

PS: C# plz

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

1 Reply

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

Answer by nirharpaz · Feb 07, 2015 at 08:58 PM

I would create a class 'Unit' with the basic abilities and functions (like moving)

from 'Unit' inherit from there the classes 'Worker', 'CombatUnit' with their unique stats and stuff

from 'CombatUnit' inherit the archers and melee, catapults and whatever you want with their difenrences (like range, AOE and whatever)

And from each individual unity inherit the specific unit of each civ which defer in sprite basically.

learn about C# inheritance rules first.

It basically means that if you inherit class B from class A - or class B extends A, then B is some kind of A.

To make it more clear, all units are some kind of 'Unit' So when you do Class Worker extends Unit you actually gave worker all the characteristics f Unit w/o writing all the code twice.

Then 'Class TurkishWorker extends Worker' cause Turkish worker is some kind of worker and has the worker's ability. The final results are the complete unit and not an abstact representation of what you want the unit to do (there is no general unit appearing in the game or general worker), so they are finally able to receive the sprite that allow them to look - while still holding all the abilities of their ancestors: Worker and Unit.

hope it helps

Comment
Add comment · Show 10 · 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 F-N · Feb 07, 2015 at 10:42 PM 0
Share

Thank you for posting! At first I'm very glad u introduced me to inheritance, I've looked up some more information and it's so useful! And I have never used it before. ($$anonymous$$akes me feel noob :P) I have used it but I'm not sure it if I've used it right:

     public Worker worker = new Worker();
     public GaulWorker gaulWorker = new GaulWorker();
     public Spear$$anonymous$$an spear$$anonymous$$an;
     public GaulSpear$$anonymous$$an gaulSpear$$anonymous$$an = new GaulSpear$$anonymous$$an();
 
     public class Unit{
         public int costs;
         public int health;
         public int attack;
         public int moveSpeed;
         public int creationTime;
         public Sprite unitSprite;
     }
 
     public class Worker : Unit{
         public int workSpeed;
     }
     public class CombatUnit : Unit{
         public int range;
         public int runEnergy;
         public int morals;
     }
 
     public class Spear$$anonymous$$an : CombatUnit{
 
     }
     public class Archer : CombatUnit{
 
     }
 
     public class GaulWorker : Worker{
 
     }
     public class GaulSpear$$anonymous$$an : Spear$$anonymous$$an{
 
     }
 
     void Start(){
         gaulWorker.costs = 50;
         gaulWorker.health = 60;
         gaulWorker.attack = 3;
         gaulWorker.moveSpeed = 5;
         gaulWorker.creationTime = 50;
         gaulWorker.unitSprite = Resources.Load ("Units/Buildable/Gaul/worker", typeof(Sprite)) as Sprite;
     
         gaulWorker.workSpeed = 5;
 
         spear$$anonymous$$an.costs = 60;
         spear$$anonymous$$an.health = 60;
         spear$$anonymous$$an.attack = 5;
         spear$$anonymous$$an.creationTime = 50;
 
         spear$$anonymous$$an.runEnergy = 12;
 
         gaulSpear$$anonymous$$an.unitSprite = Resources.Load ("Units/Buildable/Gaul/spear$$anonymous$$an", typeof(Sprite)) as Sprite;
 
         gaulSpear$$anonymous$$an.range = 2;
         gaulSpear$$anonymous$$an.morals = 10;
     }

I'd like to know what I can improve about this because I've got the feeling that it isn't exactly the way u ment.

avatar image F-N · Feb 07, 2015 at 10:42 PM 0
Share

I go to bed now, Ill have another look tomorrow as soon as I can :)

avatar image nirharpaz · Feb 08, 2015 at 04:57 PM 0
Share

looks fine to me the specific classes can allow you to have unique sprites and also a room for future unique racial abilities.

3 comments about what you have done:

attack - can workers attack in self defense? if so, fine. if not move it to CombatUnit.

I see you also gave the spearman hitting range. when i offered you, I viewed the melee units hitting at collider range and not needing range. So, since you have done it this way, note that there is no more difference between spearman and archer. the classes Spear$$anonymous$$an and Archer I$$anonymous$$O are usless and you can inherit them straight from CombatUnit.

  public class GaulSpear$$anonymous$$an : CombatUnit{
      
  }



last, I see all your values are int. note that if 4 and 9 are int then 4/9=0; numbers in int are always rounded down (to keep them int). so if you are going to calc and * or / calculations to deter$$anonymous$$e damage, dodge or whatever, change it to float.

avatar image F-N · Feb 08, 2015 at 08:37 PM 0
Share

Thank you for your help! It is true that I want workers to be able to defend themselves.

I am not sure if it would be smart to remove the Spearman and Archer classes since it spares me a lot of time coding. This because all civilizations can make archers and spearmans but only the sprites are different. With the archer and spearman class I can fill this data for all spearmans of all civilizations.

And about the int comment: I think ill just use $$anonymous$$athf.Round() for the calculations, this shouldnt give any problems.

avatar image nirharpaz · Feb 08, 2015 at 09:03 PM 0
Share

As long as you are aware that's fine. some things like chances cannot be calced easily using only 0 or 1. In 1 of my games i had some terrible issues with errors for dividing things by 0, till i discovered it was an int variable for the score that made all the mess.

Another few things:

Remember you can also inherit functions.

Remember you can also give default variables, like

 int life = 10;

 

in 'unit' so every new unit, unless stated otherwise in its 'Start' will have 10 life as standard, rather then 0; (and not just the variable).

Last, lets say you made the function 'Attack()' in 'Unit' that gives a punch. Suddenly you want the archer to release a projectile at target position which is a different action... You can create, in the 'Archer' class (now you have a reason to have it) an OVERRIDING function of 'Attack()' telling it to fire a projectile at the target.

After doing this you will be able to tell both spear-man and archer to 'Attack()' and watch each 1 of them doing his thing. moreover, (check how to do it) if a unit gets too close to the archer, there is an option to tell the archer to use his ancestor's 'Attack()' function and start punching rather than shooting.

This combo of inheriting vars and functions and the ability to override them is the true power.

hope it helps

Show more comments

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

20 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

Related Questions

Lists and Structs instead of Arrays? 4 Answers

C# ArrayList access from other script 1 Answer

Multiple Cars not working 1 Answer

How to make classes/structs public to all scripts 0 Answers

Distribute terrain in zones 3 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