Creating many instances of a class

I am programming a simple state machine to control my player, and I created a factory pattern that constructs a new instance of a state every time it is entered. Should I be concerned about performance when using a system like this? Would it be better to just save one instance of each class to a dictionary and switch between those? (This is unideal as certain parameters change in each instance of the classes.)

1 Answer

1

I’d certainly prefer having all states pre-allocated. It’s not a big worry if you do it once in a while though.

I am not sure about the dictionary, why do you need the lookup? Can’t you just keep a reference to each state object?

If you want, I have a state machine framework that has no runtime allocations and message arguments are statically typed. States can have entry parameters, which perhaps what you need.

Thanks! I'll look into this.

–

I guess that If I want people to use it and participate, I should add some documentation :) For now there's a sample project at least. Feel free to ask anything

–