- Home /
How optimize script
I use switch to choose weapon shoot mechanic. Problem that i not want to check enum value each time when i do shot. I need to check only once (on awake for example) what option i need. Maybe switch not best for this situation, so i listen all possible suggestions.
public enum Shoot_Type
{
BEAM, SHELL
}
public Shoot_Type Type_Of_Shoot;
void Update()
{
if (Input.GetButtonDown("Fire"))
switch (Type_Of_Shoot)
{
case Shoot_Type.BEAM:
Beam_Mechanic();
break;
case Shoot_Type.SHELL:
Shell_Mechanic();
break;
}
}
Answer by Inok · Sep 25, 2015 at 10:39 PM
Ok, looks like i found solution with help of Suddoha. As he suggest i use "action":
public Action Shoot_Mechanic; // declare variable of type Action
void Awake()
{
switch (Type_Of_Shoot)
{
case Shoot_Type.BEAM:
Shoot_Mechanic = Beam_Mechanic;
break;
case Shoot_Type.SHELL:
Shoot_Mechanic = Shell_Mechanic;
break;
}
}
void Update()
{
if (Input.GetButtonDown("Fire"))
Shoot_Mechanic(); // call action that hold chosen in awake method
}
void Beam_Mechanic()
{
do stuff 1;
}
void Shell_Mechanic()
{
do stuff 2;
}
Is this actually working for you? I am checking it out and I have not used action.
Answer by Suddoha · Sep 25, 2015 at 05:14 PM
At the moment there's not much to optimize. The type will only be evaluated when the key is pressed down and there aren't many options. Switch-case and if statements are generally pretty performant.
However, there are other more elegant ways of dealing with such a situation, especially when the behaviour gets more complex and the code starts to get lengthy. You could have a look into delegates / events. There are already nice quick-to-use wrapper such as Action (or the generic version) and Func (which returns a value and can have a few parameters as well). These types can encapsulate different methods with the same signature.
The advantage would be a much shorter and cleaner code (1 line, if you do null-checks, 2 lines) in Update and additionally no comparison (which is what you've asked for), only once each time you switch the type of fire-mechanic and that can be handled in a different method which is not called frequently.
(A similar approach is based on a class-hierarchy or interface system, but that's probably too much for just a single method.)
Answer by Wolf_Tech · Sep 26, 2015 at 05:50 PM
Hmm I would try setting another enum called whatever in start to shoot_type then check if it has changed something like this:
public enum lastShoot_Type
{
BEAM, SHELL
}
public lastShoot_Type lastShootType
public enum Shoot_Type
{
BEAM, SHELL
}
public Shoot_Type Type_Of_Shoot;
void Start()
{
lastShootType = Type_OfShoot;
}
void Update()
{
if(lastShootType != Type_OfShoot)
{
lastShootType = Type_OfShoot
}
if (Input.GetButtonDown("Fire"))
if(lastShootType == BEAM)
{
Beam_Mechanic();
}
else if(lastShootType == SHELL
{
Shell_Mechanic();
}
}
I am not 100% if this will be better but this is how I would do it and if you want you can try it and see if it improves performance.
Answer by jmgek · Sep 25, 2015 at 04:42 PM
public bool isShootingBeam;
//Update
if(isShootingBeam)
Beam_Mechanic();
else
Shell_Mechanic();
Is this what you want? If you don't know what a enum is it is just a "Int" Value that we assign words to.
That's basically the same with a boolean and only allows 2 different states, so he would need to fall back to the enum approach anyway, if he wanted more than 2 "shoot mechanics". :S
How i can not know what is enum if i use it in my example :)
lol sorry, a lot of people don't understand enums. Great posting your answer by the way : )