- Home /
Spells - storage, etc... [c#]
I want to implement a spell system, however I lack the ability to think of a way to store spells. Each spell has a variable for "speed", "damage" and "cost". What is the easiest but most effective way to store these spells?
Thanks in advance!
What kind of storage are we talking about here? Do you need to load them at application start? Do you need to only store spells for the duration of the game? Do you need to store active spells?
Answer by clunk47 · Jul 23, 2013 at 05:24 PM
Here is an example I came up with. It is a very basic example, but should get you pointed in the right direction. Try this script out. This stores spells as booleans (true/false), and contains 3 sample powers (Damage, Defense, Regen). Each spell selected (by pressing 1, 2, or 3) changes these values. Try it out, hope it helps.
//C# Example
using UnityEngine;
using System.Collections;
public class SpellExample : MonoBehaviour
{
public bool Spell1 = false;
public bool Spell2 = false;
public bool Spell3 = false;
public int damage;
public int defense;
public int regen;
public string label;
void Update()
{
if(Input.GetKeyDown (KeyCode.Alpha1))
{
Spell3 = false;
Spell2 = false;
Spell1 = true;
}
if(Input.GetKeyDown(KeyCode.Alpha2))
{
Spell3 = false;
Spell1 = false;
Spell2 = true;
}
if(Input.GetKeyDown (KeyCode.Alpha3))
{
Spell2 = false;
Spell1 = false;
Spell3 = true;
}
if(Spell1)
{
label = "Spell 1 - Damage = "+damage+" - Defense = "+defense+" - Regen = "+regen;
damage = 50;
defense = 25;
regen = 5;
}
if(Spell2)
{
label = "Spell 2 - Damage = "+damage+" - Defense = "+defense+" - Regen = "+regen;
damage = 100;
defense = 10;
regen = 1;
}
if(Spell3)
{
label = "Spell 3 - Damage = "+damage+" - Defense = "+defense+" - Regen = "+regen;
damage = 10;
defense = 100;
regen = 0;
}
if(!Spell1 && !Spell2 && !Spell3)
label = "Press 1, 2, or 3 keys to choose spell";
}
void OnGUI()
{
GUILayout.Label(label);
}
}
That was absolutely fabulous man, thanks a lot! Really made my day!
Answer by rowdyp · Jul 23, 2013 at 07:40 PM
or could store them in something like
GameObject[] spells
then set the number in the inspector and assign each spell then just call the number the spell corresponds to when you need to play it.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Little help with S3 1 Answer
Unity config file/or reg key 1 Answer
Fire in Unity3D - Fire particles 1 Answer
Installation/Activation question 1 Answer