top of page
Search
  • Writer's pictureEthan Toney

Don't Over-Engineer Your Code

If you're anything like me, you tend to overthink things. Today I wrote two scripts, one for picking up resources and another for the resource object. It went like this:


  1. The Character pickup system sends a message to the common resource script to let the script know it's time to work.

  2. The common resource script finds the UI text object and lets it know to update the text based on the string of the resource. It also handles the destruction of the object.

While this method worked for all objects, what didn't work were the moments I wasn't near an object. The initial plan was to have a UI system like Skyrim, which, I believe, uses a raycast. So I implemented a raycast that would hit a resource object and update the screen text. But if I wasn't hitting a resource object, I wanted the UI to disable. Well, after I added two resource objects, the script would break for all other objects except the very first object spawned. See the issue? I was using the common resource script to enable and disable the UI. I quickly realized what a terrible idea that was and upon giving it some thought, it dawned on me what I should do.


Now:


  1. The Character pickup system caches and enables/disables the UI, asks the selected resource object its name, and upon pressing a keycode, will send a message to the common resource script.

  2. The common resource script holds the name of the resource and the function to destroy the object.


Snippet of work:

 private void Start()
    {
        textUI = GameObject.Find("textUI").GetComponent<TextMeshProUGUI();
        pressControl = GameObject.Find("textUI");
        pressControl.SetActive(false);
    }

    void FixedUpdate()
    {
        RaycastHit hit;
        
        if (Physics.Raycast(Camera.main.transform.position, transform.TransformDirection(Vector3.forward), out hit, 3f))
        {
            if (hit.collider.GetComponent<ICommonResource>() != null)
            {
                pressControl.SetActive(true);
                Debug.DrawRay(Camera.main.transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.green);
                selectedCommonResource = hit.collider.gameObject;
                textUI.text = selectedCommonResource.GetComponent<CommonResource>().commonResourceSO.objName;
                if (Input.GetKey(KeyCode.E))
                {
                    selectedCommonResource.SendMessage("OnPickUp");
                }
            }
        }
        else
        {
            pressControl.SetActive(false);
        }

P.S. That ICommonResource is an interface. Pretty useful in situations like this. I'm also using SendMessage(), which is something I normally try not to do but decided this was the quickest way to get it working. The performance impact is minimal for now, though I will be sure to investigate if it ever causes issues.

0 views0 comments

Recent Posts

See All

Back To Web Development

I've been building games and prototypes for the past few years now. I've gotten pretty good with C#. I had originally started in web...

The Gaming Industry Worsens

As we see more layoffs, the sacking of union workers, and more within the industry, now is the time to be reminded: corporate capitalism...

New Year News

I've started two projects this year that I plan on sticking with (alongside various smaller side projects I may or may not complete)....

Yorumlar


bottom of page