Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17625

How Can You Profit From C# 7.0 Local Functions in Your Game?

$
0
0

Have you ever crafted Unity C# code like this?

void ForceUpdateBackend()
{
  StartCoroutine(ForceUpdateBackend_Internal(path: Url));
}

IEnumerator ForceUpdateBackend_Internal(string path)
{
  var request = UnityWebRequest.Get(path);
  yield return request.SendWebRequest();
  Debug.Log(request.downloadHandler.text); // ...
}

Somehow, it feels uncomfortable to have two different functions that do kind of the same thing.

Wait, you haven't written such code?

Okay, what about this pattern?

void AvoidCollision()
{
  var penaltyLeft = CalculateCollisionPenalty(-transform.right);
  var penaltyForward = CalculateCollisionPenalty(transform.forward);
  var penaltyRight = CalculateCollisionPenalty(transform.right);
  // drive towards the safest direction
  // ...
}

int CalculateCollisionPenalty(Vector3 direction)
{
  var didHit = Physics.Raycast(transform.position, direction, out RaycastHit hit);
  if (didHit == false …

Viewing all articles
Browse latest Browse all 17625

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>