- Home /
Alternative way to have a class to store data
I am quite certain that theres an easier way to have a class which stores data shared throughout multiple classes but I can't figure out how. I'm not quite sure how to put this into words but I want a better way to access data based on an input in a unique class. The way I am currently doing this is using a class with public functions. When I need to I instantiate the class and call the function. Heres an example of what i'm doing:
Accessed Class:
using UnityEngine;
using System.Collections;
public class ItemHandler : MonoBehaviour {
public float getDamage(string weapon)
{
switch(weapon)
{
case "Sword":
return 1000;
case "Mace":
return 2000;
default:
return 100;
}
}
}
Using the other class:
float x = new ItemHandler().getDamage("Sword");
Is there any better way for me to do this so that I don't have to create a new object each time?
Answer by weenchehawk · Jan 05, 2015 at 10:35 PM
Almost certainly there is, but there are a number of approaches & I'm not 100% sure I understand what you want to do. Let me start with what I think you are asking for & some possible approaches.
I think you're looking to have an easy way to access general application data - in your example you're getting weapon stats but I'm sure you intent to use it for other things.
Option 1
The first simple improvement you could make is to make getDamage a static function. This will mean you no longer need to instantiate the data storage class
public static float getDamage(string weapon)
{
switch(weapon)
{
case "Sword":
return 1000;
case "Mace":
return 2000;
default:
return 100;
}
}
now you can just call
float x = ItemHandler().getDamage("Sword");
Option 2
The next thing you can do is consider some structure in your storage method.
public class Weapon : MonoBehaviour
{
public float Cost;
public float Damage;
public float Weight;
}
public class AvailableWeapons : MonoBehaviour
{
public static Weapon Sword = new Weapon();
public static Weapon Mace = new Weapon();
void Start()
{
Sword.Cost = 20f;
Sword.Damage = 8f;
Sword.Weight = 4f;
Mace.Cost = 15f;
Mace.Damage = 6f;
Mace.Weight = 3f;
}
}
now you can just call
float x = AvailableWeapons.Sword.Damage;
Option 3
You could have a weapon prefab and simply attach a copy of the Weapon class (from above) to it in the unity editor, then in the inspector fill in the stats, and when you hit somebody with the weapon, you can call something like
void OnAttack()
{
Target.health = Target.health - gameObject.GetComponent<Weapon>().Damage;
}
This has the added bonus of still working when the player changes weapon.
Personally I'd just go with Option 3
Answer by Rainbirth · Jan 06, 2015 at 03:41 AM
I recomend you that start working with CSV data for your weapons, and characters, it scalates very quickly if you're trying to attempt to create a RPG game, from my point of view you'll need massive amount of data. I suggest to read some tutorials on using CSV to store your values and use of dictionary or lists.
check out this tutorial. http://forum.unity3d.com/threads/aligning-object-to-surface-normal-with-quaternion-lookrotation.63619/
That way you'll be able to load data for your enemies, weapons, items, etc.
CSV for scalability? You ever tried to add a new field to a CSV after it has been populated with data? Nested fields? CSVs are great for extracting complete data from built application. Not the best for game data, especially during development where data structure changes frequently.
JSON or a X$$anonymous$$L solution is better.
Your answer
![](https://koobas.hobune.stream/wayback/20220613175628im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Editting a custom javascript class in the inspector 1 Answer
Auto Commenting lines 1 Answer
Access class variables of selected object 1 Answer
Accessing a variable inside a class inside other script... 2 Answers