#Initialize class with string

1 messages · Page 1 of 1 (latest)

glacial pebble
#

apoligies if formatting is bad, on phone.
i have an enemy class, with subclasses of different enemy types. I initialize them in a different script called enemyspawner, with

Enemy<EnemyType> enemy = new Enemy<EnemyType>("Enemy Name");

I want to have the enemy spawner script spawn random enemies from a list, but im not sure how I would have to do it. I tried a string list, like

Enemy<StringName[0]>

but it does not accept strings, only types. i thought of just having If statements for each different enemy type, but that gets annoying every time I want to make a new enemy type.
what ways can i go about this?

mystic flame
#

<> would need a type inside, it is not going to accept variables whether thats a string you declared or anything else

#

If you have a list of enemy names, you'll need to store the list of actual enemies somewhere and then loop through them to see which one has the name you're looking for.
Theres likely a better way to do this, but i dont exactly know what you're doing

glacial pebble
#

is there any way to make Type variables, or maybe a variable of the enemy class? temporarily I have an if loop that'll go through a string list of every enemy, then when an enemy is spawned it goes through every one, such as

if(enemyID == 1)
{
Enemy<Enemy1>
}

is that too inefficient? what other information should I give?

#

I can post code, not sure what though

mystic flame
#

you can declare different enemy types yes, but thats mainly if they have different functionality and run different code.

#

you could just link each enemy and a name using a dictionary. Then look up the name to get the appropiate enemy

mystic flame
glacial pebble
#

I'm not sure what you mean, by send anything. I thought it had to be the name of the subclass?

mystic flame
#

look at examples of generics online, especially with List<T>

#

you can create a list of any type. In your example, you are using a generic but only using it for 1 type, it doesnt really make sense

glacial pebble
#

I'm still new to classes and generics, so I did originally just follow a tutorial on enemy classes. I'm not home, so I'll probably ask this again later when I can actually see the code I wrote, but this is off what I remember

from what I understand, I made an Enemy<T> class, with the <T> being different types of enemy scripts. I'll definitely look into generics, but I thought that I was using it for multiple types already?

mystic flame
#

i just meant that generics are likely not what u need for this, but look into them to see the true use case from lists. So you dont have to implement something that doesnt work from the start

#

Its likely u just want inheritance. Or if the only difference between enemies is their name, then you dont need either.

glacial pebble
#

alright, i can see my code now, ill see if i can make sense of it

#
public class Enemy<T> where T : Enemy
{
    public GameObject GameObject;
    public T ScriptComponent;
    public Enemy (string name)
    {
        GameObject = new GameObject(name);
        ScriptComponent = GameObject.AddComponent<T>();
    }
}
#

so this is the generic class

#

so with T, I can make it a string instead?

#

or add a string component to it as well

mystic flame
#

yea the generics in here really dont make sense

#

you have a type T thats being constrained to Enemy only, but i assume its just being used to reference itself? Idk what the goal is here

#

public T ScriptComponent is really just
public Enemy ScriptComponent
and then you are adding this script onto an object seemingly, but it isnt a monobehaviour

#

you should make this a monobehaviour instead, then just drag it onto the object in the inspector

glacial pebble
#

I can post the rest, if that might help. id like to leave changing to a monobehaviour to last, as it will probably break some things and take a while

mystic flame
#

this script shouldnt be on a game object if its not a monobehaviour

#

leaving it to last is just a bad design choice and you'll break more things later

glacial pebble
#
public abstract class Enemy : MonoBehaviour
{
    // common components like rigidbody2d
    // common variables like health, damage
    protected abstract void MovementPattern();
    void Awake()
    {
        //Add common components
        Body = gameObject.AddComponent<Rigidbody2D>();
        Sprite = gameObject.AddComponent<SpriteRenderer>();
        Collider = gameObject.AddComponent<BoxCollider2D>();
        Sprite.sprite = Resources.Load<Sprite>("Sprites/EnemyGrunt");
    }
    //Insert all unique values to be determined at insantiation here
    public void Initialize(int speed, int direction, Vector3 position, int damage, int cost, int maxhealth, float hitstun)
    {
        // initialize the variables
    }
}
#

yeah im realizing that's what i should do

#

i shouldnt follow tutorials if i dont understand what they are doing

#

because i kinda shot myself in the foot by copying it blindly, and now i have 3 different enemies that rely on this method to spawn in

mystic flame
#

i mean its possible for it to work, but yea its likely pretty bad

#

i dont know what they're doing, its just bad code all around

glacial pebble
#

how efficient is using prefabs? i remember being told to find other ways to spawn enemies, but they were pretty convenient compared to this

mystic flame
#

you need prefabs if you want to spawn things. Unless u want to build them over and over again/hide them in every single scene

glacial pebble
#

i think thats what this tutorial was trying to do, it felt like it basically built the enemy from scratch every time

#

what downsides to prefabs have?

mystic flame
#

unless you're creating them poorly, not really any downsides

#

there isnt a good alternative because prefabs do what they're supposed to

glacial pebble
#

last question then, what would creating them poorly mean

#

otherwise im just gonna embrace prefabs

mystic flame
#

i wouldnt worry too much about it, just start experimenting with it.
I meant more like if you create the enemy prefab but everytime u create it u have to add a bunch of scripts to it or setup a ton of components