Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

The Challenge of Tweeting with Unity

A topic by Josh McMillan created May 13, 2023 Views: 122 Replies: 5
Viewing posts 1 to 6

So far, I've made two attempts at this challenge with Unity. The main issues you run into are:

  • Every MonoBehaviour you create costs an immediate 50 characters for boilerplate alone. This includes the class definition and referencing the through "using UnityEngine;"
  • Storing components using GetComponent are very expensive character-wise. Direct references or using SerializedFields are much cheaper by comparison.
  • Your most expensive code calls will be to the engine. Best practice would be to create a single character wrapper method for the engine call if it is used more than once (just once can be left as that's the minimum characters you can make - a wrapper would be more expensive in that case.

The two games I attempted were:

  1.  An overly ambitious tank game (very impractical, didn't get far), and
  2. A nearly complete Simon Says game, complete with animations (boilerplate code brought the codebase to 530 characters, a massively strong attempt!)

So the last attempt I'll make is a mini-golf game, that 1) a single script that 2) uses a wrapper method for input, no calls to System.Collections.Generic (what killed my Simon Says game), and 3) heavily utilize physics and animations (native to engine, need few calls if any)

If anyone is curious about the code for the Simon Says game, here it was (it literally ran everything from animations to logic and input):

using System.Collections;using System.Collections.Generic;using UnityEngine;class G:MonoBehaviour{[SerializeField]Animator a;List<int> s=new List<int>();bool p;void Start(){StartCoroutine("l");}bool k(string i)=>Input.GetButtonDown(i);void q(int c){a.SetTrigger(c);}IEnumerator l(){while (!p){yield return new WaitForSeconds(0.5f);int val=Random.Range(1,5);s.Add(val);q(val);foreach (var i in s){int c=0;while (c==0){if (k("R")) c=1;if (k("G")) c=2;if (k("Y")) c=3;if (k("B")) c=4;yield return null;}q(c);if (i!=c){p = true;break;}}}}

You can also use a wrapper for the MonoBehaviour class like this : using UnityEngine;public class M:MonoBehaviour{}, and that helps greatly cut down the number of characters used from 50 to merely 19 per script, doubling the theoretical number of scripts with only a 37 character penalty in doing so. For multiple scripts, this is the way to go.

I've done it. Here's the code for my game now!

using UnityEngine;class M:MonoBehaviour{}using UnityEngine;class H:M{public bool h;public Rigidbody R;public Transform T;public Animator A;void OnTriggerEnter(Collider c){if(c.tag!="p")return;if(h){R.velocity=Vector3.zero;R.position=T.position;}else{A.SetTrigger("r");}}}using UnityEngine;class G:M{float i(string a)=>Input.GetAxis(a);public Rigidbody R;public float S;public Transform C;void FixedUpdate(){R.AddForce(new Vector3(i("h"),0.0f,i("v"))*S);C.position=transform.position;}}

And here's each individual script broken out:

// MonoBehaviour wrapper (to avoid repeating such a long inheritence)

using UnityEngine;

class M:MonoBehaviour

{

}

// Hole/Story Object (This is a two-object script. One handles entering a trigger and teleporting the player which is the hole, the other shows an animation whenever a trigger is entered.)

using UnityEngine;

class H : M{

public bool h;

public Rigidbody R;

public Transform T;

public Animator A;


void OnTriggerEnter(Collider c) {

if (c.tag != "p") return;

if (h) {

R.velocity = Vector3.zero;

R.position=T.position;

}

else

{

A.SetTrigger("r");

}

}

}

// And this is the player controller, which captures input using a small macro optimization

using UnityEngine;

class G : M {

public Rigidbody R;

public float S;

public Transform C;

void FixedUpdate() {

R.AddForce(new Vector3(Input.GetAxis("h"),0.0f,Input.GetAxis("v"))*S);

C.position=transform.position;

}

}

... I just did the math. I was wrong. I did my input wrong, complicated it, and added characters. The NEW character count is now 473, down from 488. Still, super excited to finally have mechanics! Now to build a game :P

In the end, I gutted the whole "golf controller" style for directly controlling the golf ball rolling (which is oddly similar to Unity's primary tutorial, Roll-a-Ball), which yielded a lot more fun ideas and results.

(+1)

Here's the corrected code ;P

using UnityEngine;class M:MonoBehaviour{}using UnityEngine;class H:M{public bool h;public Rigidbody R;public Transform T;public Animator A;void OnTriggerEnter(Collider c){if(c.tag!="p")return;if(h){R.velocity=Vector3.zero;R.position=T.position;}else{A.SetTrigger("r");}}}using UnityEngine;class G:M{public Rigidbody R;public float S;public Transform C;void FixedUpdate(){R.AddForce(new Vector3(Input.GetAxis("h"),0.0f,Input.GetAxis("v"))*S);C.position=transform.position;}}

HostSubmitted(+1)

Glad you were able to work out a solution! :)