#get class by his attribute

1 messages ยท Page 1 of 1 (latest)

serene iris
#

// So my 3 generals classes : 

public class Spell {
  private Element[] elements;
  public float isElementGlyph(List<Vector2> dot)
    {
        return elements[0].glyph.IsInside(point);
    }
}

public class Element {
  public readonly Glyph glyph;
}

public class Glyph {
  private List<List<Vector2>> polygon;
  ...
  public isInside(List<Vector2> dot) {...}
}

// Then i have something like that 

public class Fire : Element {
  public Fire()
  {
    glyph = new Glyph();
    ...
  }
}

public class FireBall : Spell {
  public FireBall(elements) {
    this.elements = elements
  }
}

// And so in another class :

public class Module {
  getSpell(List<Vector2> dot) {
    *...*
  }
}
timid gale
#

so Vector2 is a grid position?

#

and getSpell trying to? I just trying to figure out what spell do you want to find with a Vector2

serene iris
#

Yea and no ๐Ÿ˜… no because the user will draw a glyph but we don't really care about the initial position

#

I want to get the correct spell because i have like FireBall, FireTornade and idk FireTorch (some invention because there is only FireBall xD but there will be more)

timid gale
#

hm

serene iris
#

Ok i misstype but that was a list of list of vector so my bad

timid gale
#

still dont really get it, but you need to keep track of the instances either way

serene iris
#

So i need to create an instance per spell ?

timid gale
#

somehow you gotta access the spell info, if you store it inside, or you have to make that data static

#

if you need to instantiate a class (for whatever reason) for each spell casting, usually there is a separate class like SpellDefinition that contains the data

#

the caster class just uses a reference to the definition to access its information

#

because you dont want each spell to contain ALL data

serene iris
#

How will the SpellDefinition be like ?

serene iris
timid gale
#

how are you storing your other data? where you reading them from? ScriptableObjects, database, json, etc?

#

if you hardcoding everything then this is though KEKW

serene iris
#

Well for now it's hardcoding but then it will be json

#

a json for all the element and another one for all the spells

timid gale
#

to provide some help, right now, the best you can do without turning your structure upside down is to make a singleton class that you can access

#

and make a List<Spell> which contains one instance of your spells

serene iris
#

ok ok

#

will try to do that

timid gale
#

and you can iterate through and look for the right one

serene iris
#

that was i was going to do at first

#

well anyway thanks

#

a lot !

timid gale
#

and if you really dont know the type of the spell you want to get, you can create instances with System.Activator.CreateInstance but this is dirty, unperformant, non-typesafe reflection

#

if you have to use it then you'll have other problem later on ๐Ÿ™‚