Operation order in the InteractWithCombat function

Greetings all! First off, I’m absolutely loving this course, so kudos and abundant thanks to the lovely folks at GameDev. My question might be a bit pedantic, but I’d love to hear the communities’ thoughts, so here goes: In the InteractWithCombat function, I noticed that Sam ran the foreach loop then checked for Input.OnMouseButtonDown, and I got this working without any issue… but I also reversed the order, checked for the mouse button press first, then grabbed the RaycastHit array (Why am I picturing Rick saying those two words over and over as a tongue twister while chuckling?), and ran the foreach loop, and this also works. So… my question regards performance and optimal operational order. Would there be any real difference between the two cases? Short of running tests with 100k iterations, I’m more than content to get opinions from more experienced coders. I realize that a foreach that goes through only a couple objects every frame would probably have no noticeable impact, but my brain is wired for code efficiency, and checking for the mouse click at the beginning makes more sense to me. Please be brutal. I can take it… or I have a supply of tissues handy for worst case! :smile:

The altered order for the InteractWithCombat function:

        void InteractWithCombat() {

            if (Input.GetMouseButtonDown(0)) {
                RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());

                foreach (RaycastHit hit in hits) {

                    CombatTarget target = hit.transform.GetComponent<CombatTarget>();

                    if (target == null) { continue; }

                    GetComponent<Fighter>().Attack(target);
                }
            }
        }

…I suppose I should have watched the next video. sheepish aura So, I see why Sam is running the operations in the order that he is, but as a purely theoretical question, I’d still be interested in opinions and remarks :smiley: Cheers!

It gets even more important in the Final polish section because then we’ll be creating cursor affordances. We want the cursor to reflect “Hey, you can attack here” or “hey, you can move here”… if we only do the raytrace when the mouse button is pressed, that functionality is impossible…

Now… if you’re developing the game for Android or iOS, where the cursor is not really a thing, then it would actually make more sense to only do the raytrace when the touch is pressed because you’ll see a performance gain on those slower phone processors.

Perfect :slight_smile: Thanks for the feedback, Brian. As soon as I was a minute into the next video I realized why Sam was coding it the way he was, and I really like the idea of cursor affordance (In fact, I’ve already been toying with having it in the game I’m intending to build, so this is great seeing it implemented beforehand :smiley: ) I hadn’t considered it being practically useless on mobile though, so thank-you very much for that comment! Cheers!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms