- Home /
Is it possible to assign custom functions to a Scriptable Object?
Hello, I was wondering if it is possible to have a field in a Scriptable Object that stores functions, or methods. I know it may sound weird, but here is what I'm trying to do: Let's say I want to make a game like Hearthstone, where I can cast spells. Spells cost mana and have a name, which I can store in a Scriptable Object. The problem is, each spell has different effects, such as draw a card, or deal 2 damage, and it gets way more complicated than that. I have thought about some solutions, such as storing every effect in the Scriptable Object and having an int value that determines which effect the spell will use, but I think it would just be too messy. Here is what I have now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName ="New Spell",menuName ="Spell")]
public class Spell : ScriptableObject {
public new string name;
public string description;
public Sprite image;
public int summoningCost;
}
Any ideas?
Answer by Captain_Pineapple · Apr 13, 2018 at 03:50 PM
Well if you just say that spell is your base class you can go on an inherit different spells from this class. Then you can have a List where you can store whatever spell that is inherited from your base "Spell" class and have a mutual function (e.g. public void castSpell()) which can be called from each Spell object and all inherited object.
Further you can go on and define different actions for the "castSpell" function in each inherited spell.
I hope i got your problem right and this helps. In case you have more questions about this or it was unclear go ahead an ask. You should read a bit into class inheritance and overriding parent class functions.
Advantage of this option is that you can easily assign default values in the parent class as well as custom values for each inherited spell.
EDIT: The implementation of this can be seen in this Post here where the same issue was discussed in more detail.
Thanks for the suggestion, I have been trying to do a similar thing, but I have just started learning about class inheritance. I'll try using your method but I need to learn a bit more first.
Wow, I just changed a few things and I got it to work, basically what you said: Inheriting from a class with an empty "virtual void castSpell()" and then creating an "override void castSpell()" that contains the custom effect for that specific spell. Wasn't so hard after all :D
This is a year later, but I'm in a similar predicament. Using inheritance with SOs is actually the solution I came up with, also, but in my game the entities using the abilities could gain or lose them dynamically... so how on earth would you reference all those SOs. And do you have to manually create an asset for every single inherited spell you create to go along with it?
Answer by MarioSantoso · Apr 13, 2018 at 03:45 PM
Not that I know of. But here's my suggestion.
You create the scripts for each effects, put them in gameobjects and make prefabs of them. In your "GameManager" or whatever object, have a script containing a list of that effects prefabs.
In your scriptable object, make a single field, can be int or enum that you can use as ID reference instantiate the appropriate prefabs to start/create the effects.
So whenever you deal a card, just instantiate an effect based on the ID reference.
Answer by ShamimAkhter · Jun 21, 2019 at 10:40 PM
Yes, you can have custom functions in your scriptable objects. Take this example:
public float speed_kmph;
public float speed_mph;
public float Kmph_To_Mph(float kmph)
{
return 0.6f * kmph;
}
A vehicle will store its current speed in this ScripableObject. The current speed is stored as km per hour (speed_kmph). And speed in miles per hour (speed_mph) can be obtained by using that public function. You don't have to calculate the speed_mph the same way like you needed with speed_kmph, like rigidbody.velocity.x then inverse transform of that etc.
Your answer
Follow this Question
Related Questions
what is wrong whis this? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Ensure that function only gets called once in a frame 2 Answers