DemoProductions / shmup

3 stars 2 forks source link

Enemy attempts to instantiate and use a weapon it doesn't have #83

Closed flip40 closed 8 years ago

flip40 commented 8 years ago

The Enemy prefabs don't have weapons, which causes a boatload of errors.

I would think we should test for the prefab before attempting instantiation, as well as test for a weapon before shooting. While we expect there to be a weapon, the errors we get should be avoided, if at least because they are annoying (or more importantly to avoid potential secondary errors, even if Unity seems to handle these and move on).

No worries for efficiency due to the extra bool checks (predictive branching and such negates it).

ghost commented 8 years ago

What is predictive branching?

flip40 commented 8 years ago

The specifics are a bit complicated, but basically when pipelining, the cpu is executing instructions before the last one has ended. With branching, this means that you don't know what instructions you will need next, and have to wait for the last instruction to finish so you know which branch to take. Predictive branching helps keep this from delaying the pipeline by guessing the correct branch and starting to execute those instructions before it knows it is correct (then fixing it if it was wrong)... Predictive branching is smart enough that if an object has a weapon (in this case), it will know the if statement has been true, and will effectively almost skip doing the check entirely by preloading and executing the instructions for the correct branch.

If the weapon is set during a run, the statement will always be true and the prediction will be for true, negating much of the delay of having a branching statement at all (and vice versa).

Tbh the statement of predictive branching making the if statement negligible was perhaps unnecessary, considering how fast computers are these days anyways it would be negligible regardless.

flip40 commented 8 years ago

87