Is creating new Instances of the same class repeatedly instead of reusing them a problem?
I am working on a Battle arena game, and i am following a tutorial i found on Youtube in regards to creating an ability factory (i'll leave link below in case)
In the tutorials there an AbilityFactory script, which gets called into every time an ability is used. and it look like this
public static Ability GetAbility(string abilityType)
{
if (abilititiesByName.CointainsKey(abilityType))
{
Type type = abilitiesByName[abilityType];
var ability = Activator.CreateInstance(type) as Ability;
return ability;
}
return null;
}
the GetAbility function use Activator.CreateInstance, which as far as i am concerned will create a new instance of the scripts every time an ability it used, is this the case? and if so, is there any problems associated with this way of using abilities. especially in regard to performance, if a lot of abilities is being used during a game.
And is there any better alternative to this, if this way causes problems?
link to video: https://www.youtube.com/watch?v=FGVkio4bnPQ&t=755s&ab_channel=JasonWeimann
Answer by xxmariofer · Dec 16, 2020 at 12:36 PM
Obviously if you can store and reuse the "Abilities" would be better. And knowing if it will cause problems is imposible without knowing the full game and seeing the full code, if you are creating abilities every frame and storing them forever it will end up giving issues, but in a game in which you create an ability once and you have multiple abilitytypes storing them would be worse
The way it works, is each ability i considered an AbilityBehaviour so each individual AbilityBehaviour will have it's own script that inherits from a scripts called AbilityBehaviour, and each individual AbilityBehaviour will be stored in a dictionary in the AbilityFactory.
the problem i'm having is that i don't know how to store a reference to each AbilityBehaviour script in the AbilityFactory. i'm getting all the scripts using "Assembly.GetAssembly", and then i wan't to loop through this list, creating an instance of each AbilityBehaviour script, and store a reference to each scripts in the dictionary.
I know i can use "Activator.CreateInstance" to loop through and and create an instance of each AbilityBehaviour, however i don't know how to then add a reference of it to the dictionary with it's associated name. in this case i want the key of the dictionary to be a string, witch is the unique name found in each AbilityBehaviour scrips, and the value to be a reference to the script. so i call a function from within a specific AbilityBehaviour scripts, when i want to use the ability.
public static class AbilityBehaviours
{
static Dictionary<string, Type> abilityBehaviours;
static bool isInstialized => abilityBehaviours != null;
static void InitializeFactory()
{
if(isInstialized) { return; }
var abilityBehaviourList = Assembly.GetAssembly(typeof(AbilityBehaviour)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(AbilityBehaviour)));
abilityBehaviours = new Dictionary<string, Type>();
//dont know how to do this?
foreach(var type in abilityBehaviourList)
{
var tempBehaviour = Activator.CreateInstance(type) as AbilityBehaviour;
abilityBehaviours.Add()
}
}
}
public abstract class AbilityBehaviour
{
public abstract string Behaviourname { get; }
public abstract void UseAbility();
}
I am not sure if I understood your full issue, are you asking about somehting like this?
abilityBehaviours = new Dictionary<string, Type>();
//dont know how to do this?
foreach(var type in abilityBehaviourList)
{
var tempBehaviour = Activator.CreateInstance(type) as AbilityBehaviour;
abilityBehaviours.Add(tempBehaviour.Behaviorname, tempBehaviour)
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613021729im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object reference not set to instance for my timer and my text 0 Answers
Why do I keep getting this error? 1 Answer
Referencing another object's instance 0 Answers
Сonstructor return null 0 Answers