Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created April 21, 2022 14:19
Show Gist options
  • Save kurtdekker/59aec776d764ae1635fe867f737c6b78 to your computer and use it in GitHub Desktop.
Save kurtdekker/59aec776d764ae1635fe867f737c6b78 to your computer and use it in GitHub Desktop.
Simple demonstration of looking up XP into a Player Level table
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// @kurtdekker - ultra-simple XP to Level lookup table
// drop it on a blank GameObject to test it, look in the console
public class SearchXPTable : MonoBehaviour
{
// CAUTION: This is subject to all of Unity3D's serialization rules!!!
// eg, once set in your scene or prefab, these default ctor() values
// are NEVER USED AGAIN!!
[Header("Must be ascending order always:")]
public int[] XPBenchmarks = new int[] { 10, 20, 50, 100, 200, 500, 1000};
int ComputeLevelFromXP( int xp)
{
for (int i = 0; i < XPBenchmarks.Length; i++)
{
if (xp < XPBenchmarks[i])
{
return i + 1;
}
}
return XPBenchmarks.Length + 1;
}
void Start ()
{
// demos the above by blasting 2000 tests through it to console
for (int xp = 0; xp <= 2000; xp++)
{
int level = ComputeLevelFromXP( xp);
Debug.Log( System.String.Format( "xp = {0} means level = {1}", xp, level));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment