- Home /
Creating A Class Variable That Works With Any Class
Hey there :)
I am not sure how to ask this, so I have some rough code to help explain. Pretty much I have many different classes for each type of 'Mod', so each button I have to write out what class to look for, which take require lots of code per button.
These are all the classes for each type of Mod:
var attackMods : AttackModClass[];
var cockpitMods : CockpitModClass[];
var defenceMods : DefenceModClass[];
var intelligenceMods : IntelligenceModsClass[];
var mobilityMods : MobilityModsClass[];
var structuralMods : StructuralModsClass[];
var utilityMods : UtilityModsClass[];
Is there any way I can I have a generic class variable, like this:
var selectionMod : Class[] //< what do I name this?
Then just set a class to that variable whenever a button is click, eg:
//button 1 clicked
selectionMod = attackMods;
selectionMod[0];
//button 2 clicked
selectionMod = defenceMods;
selectionMod[0];
//button 3 clicked... etc
And NOT do this, which is what I am currently doing for each button:
//button 1 clicked
x =0
attackMods[x].name
attackMods[x].id
attackMods[x].value
.. etc
//button 2 clicked
x =0
defenceMods[x].name
defenceMods[x].value
defenceMods[x].id
..etc
//button 3 clicked.. etc
This would result in a lot less code in every button, making everything easier to edit.
It's quite hard for me to explain, so if you do not understand I can explain further :)
Thanks, Magnus
a class that can be accesed by anyclass is a static variable...
class $$anonymous$$yClass {
static int $$anonymous$$yVar
}
$$anonymous$$yClass.$$anonymous$$yVar //can be used anywhere
now the problem m with static variables is that they NEVER get collected until the program is ter$$anonymous$$ated even if no instances of $$anonymous$$yClass exist $$anonymous$$yVar will be in $$anonymous$$emory e.g Exit to Windows.
what i did was make an abstract base class , then make a generic list of that abstract class and forced all mod-classes to inherit from the abstract class. then the compiler will always know that the values you are using for that list will be there. RuntimeGUI uses this to handle interfaces.
alternatively you can use dynamic typing or object.
It looks like what you need is inheritance. So you will have a $$anonymous$$odsClass
that extends $$anonymous$$onoBehaviour as usual, and all your other classes, will extend it ins$$anonymous$$d.
DanyB not just inheritance, if did it this way it would just be a list of $$anonymous$$odclass, anything that is e.g attack$$anonymous$$ods is cast down thus removing its special properties. there is a wiki though...
Sorry guys, I really appreciate the replies :) Haven't got around to experimenting properly with your feedback.
I will do when I get a chance, Thanks :)