• 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 /
  • Help Room /
avatar image
0
Question by Mekanofreak · Aug 09, 2020 at 07:45 PM · databasexmlinheritancestoragespells

Best way to store a large ammount of spells ?

Hi, I'm currently working on a spell system and I kind of hit wall on how to store each spell.

The spells in question have some common property , ex: a name, a description and a cost to cast, they also have common method like OnEquip(), OnCast(), OnDoneCasting(), ect. So I tried using inheritance and created a base class and was creating script that inherited from that class, but as the number of spell grew it as become hard to manage. I then tried using the "composition over inheritance" idea, but again, the number of possible combination of element, effect,animation ect was still a huge problem. So now I wonder if I have other options ? Some people in simmilar situation where told to use database/XML to store enemy and weapons, how would I go about doing that ?

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 DialBlitzness · Aug 10, 2020 at 01:29 PM

Hello !

I can show you my solution, i explain this in another topic but it's possibly the best for you. Here : Loader and database

I think writing C # or XML is the same, so you might as well stay in a controlled environment.

Comment
Add comment · Show 3 · 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 Mekanofreak · Aug 10, 2020 at 03:01 PM 0
Share

Your method is certainly an interesting idea, I will try to implement it, aside from some edge case it should work for most of my spells, and I could have a single script that simply reference the spells database to know what was casted and apply the right effect at the right time, maybe by storing them as : DataSpells[1] = PopulateDataSpell("fireball", cost, damage, effect, animation, "it goes boom when it hit something"); But that leave me with the problem of the various effect of the spells, for exemple, a fireball will light you on fire, a lightning ball will immobilise you for x seconds, ect, and also for the various animations, how do I reference those ? Create a list of effect, and add them as parameters like in my exemple and reference them by name to find them ? Also, in your exemple I see a "Remplir les données", a fellow french speaker ?

avatar image DialBlitzness Mekanofreak · Aug 10, 2020 at 03:14 PM 0
Share

Haha sorry, i'm a french developer, i forgot to rewrite this comment :D

You fully understood the idea for the implementation of spells.

- For the different animations, you have to add in DataSpell an "AnimationName" property that you will fill like the rest in the "Database" class. This name must be the name of the prefab that contains your animation. Then, create a DataSpell.Instantiate () method which will have the role of creating the instance of this animation.

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 using System;
 
 [System.Serializable]
 public class DataSpells
 {
     public string Name = string.Empty;
     public string Description = string.Empty;
     public string ParticleEffect = string.Empty;
     public string SoundEffect = string.Empty;
     public string tag = string.Empty;
 
     public DataSpells(){}
 
     public DataSpells(string name, string tag)
     {
         this.Name = name;
         this.tag = tag;
     }
 
     public GameObject Instantiate(float PlayerXPosition, float PlayerYPosition)
     {
         // Création de l'effet dans la scène
         GameObject go = GameObject.Instantiate(
             ((GameObject)AssetDatabase.LoadAssetAtPath(LinkEffect(this.Name), typeof(GameObject)))
             , new Vector3(PlayerXPosition, PlayerYPosition, 0), Quaternion.identity) as GameObject;
         return go;
     }
 
     public string LinkEffect(string name)
     {
         return "YourPath"+name+".prefab";
     }
 }
 



- For the different effects, if it's turn-based combat, you can create a unique script to associate with each spell concerned according to their effects. But if it is in real time in the environment with multiple gameobjects, I would advise you rather to associate a script similar to all the gameobjects which will make the object react according to the spell which strikes it, thanks to OnCollideEnter (), OnTriggerEnter () or OnParticleCollide () for example.

avatar image Mekanofreak DialBlitzness · Aug 10, 2020 at 05:41 PM 1
Share

This is kind of what I am doing at the moment, I have a single script for each spells. I am actually in the process of recoding everything using your recommendation, for now it seem to work and instead of creating a script for each spells I made a few list for the effects, animation and sound and a script that just use the parameter of the spell to find what it need in the list and instantiate them (actually not instantiate, for many things I use a "pool" created at startup, so I dont slow down my code by instantiating continuously).

the Script then put all the pieces together and fire the spell so a spells kinda look like this for the fireball : dataSpell("fireball", mana cost, damage, animation name for in hand, casting, and impact, sound for in hand casting and impact). I wonder if makings an enum for all the possible prefabs cathegories and create a simple class to contain the spell so they are easy to edit and create using those enum would make sence !

I'm from quebec "alors bonjour a toi et pas besoin de t'excuser !"

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

216 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Whats the best option for an item database for a single player game 2 Answers

Saving data with files 1 Answer

Best data type for storing large quantities of data? 0 Answers

Inventory that loads data from an XML 0 Answers

How to handle inventory in 2d game, via xml, no game objects (inheritance? interface?) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges