how to add a function to Leveling a Character useing the Fibonacci sequence?

my current code for my character lvling system is very basic

 public float Xp = 0f; //floats can be used for later lvls so gathering xp takes longer
 public float Health = 100f;
 public int Level = 1; // i believe it makes since to start off as one instead of zero 
 public float XpToNxtLvl = 0f;
 public int FibonacciLevel = 0;

 XpGain(float amount )
{
     Xp += amount;
     if( Xp >= XpToNxtLvl)
     {
         NextLvl();
      }
}
 NextLvl()
{
     XpToNxtLvl += 1100;
     Health += 50;
}

new formula

fn = fn-1 + fn-2
would i make n (Level+1) and f (XpToNxtLvl) is it that easy?

(FibonacciLevel+1))-1 + (FibonacciLevel+1))-2

old formula

XpToNxtLvl += ((XpToNxtLvl * (Level + 1)) - 1 + (XpToNxtLvl * (Level + 1)) - 2);

my Answer

XpGain(float Fibonacci)
    {
         Xp += Fibonacci;
         if( Xp >= XpToNxtLvl)
         {
             NextLvl();
          }
     }
 NextLvl()
    {
         FibonacciLevel = Level +1;
         XpToNxtLvl += ((FibonacciLevel-1) + (FibonacciLevel-2))
         Health += 50;
         Level += 1;
    }

I believe there could be some mistakes which is why I’m posting this question
thank you for your time reading this sorry if its long i appreciate any feedback

theres another post about an infinte lvl system
http://answers.unity3d.com/questions/973992/infinite-level-system.html

who makes use of a private multiplier variable would it make since to turn the fibonacci formula instead into a private variable?
the basic code is from a couple of rpg tutorial series i was using to learn from so since it not my own other than variable changes ill just say i got it from the HardlyBriefProgramming series as well as naman jain both on youtube.

You don’t seem to understand what that formula stands for. It’s a “recurrence relation”. The “f” in fn = fn-1 + fn-2 doesn’t stand for a constant. “fn” stands for a particular number in the sequencem specifically the “nth” number. The fibonacci sequence is a common example for recursion, however it’s quite inefficient to use a recursion for calculating the sequence.

public static int Fib(int aIndex)
{
    if (aIndex <= 0) // important, breaking condition for the recursion
        return 0;
    if (aIndex == 1) // important, breaking condition for the recursion
        return 1;
    return Fib(aIndex-1) + Fib(aIndex-2); // recursion
}

This function will return the nth fibonacci number. So if you call

Fib(10);

it will return “55” the “10th” fibonacci number:

 n      Fib(n)
----------
 0  -   0
 1  -   1
 2  -   1
 3  -   2
 4  -   3
 5  -   5
 6  -   8
 7  -  13
 8  -  21
 9  -  34
10  -  55

As i said calculating the fibonacci sequance recursively is quite inefficient, It’s easier to calculate it iterative

public static int Fib(int aIndex)
{
    int n1 = 0;
    int n2 = 1;
    for(int i = 1; i < aIndex; i++)
    {
        int tmp = n1 + n2;
        n1 = n2;
        n2 = tmp;
    }
    return n1;
}

This does the same but without recursion.

In your case instead of storing only “XpToNxtLvl” you could store the last two numbers instead of the last one. So basically when you level up you just do “one iteration” of the for loop yourself to get the next number in the sequence. Of course you could use the “Fib()” method to calculate a certain fibonacci number, but for higher numbers it’s unnecessary to count up from the very beginning if you can simply store the last two numbers instead.

edit

Here’s a class that automatically builds a lookup table for Fibonacci numbers:

using UnityEngine;
using System.Collections.Generic;

public class Fib
{
    public static Fib Global = new Fib();
    private List<int> m_Numbers = new List<int>();
    public Fib()
    {
        m_Numbers.Add(0);
        m_Numbers.Add(1);
    }
    public int this[int aIndex]
    {
        get { return GetNumber(aIndex); }
    }

    public int GetNumber(int aIndex)
    {
        if (aIndex < 0)
            throw new System.ArgumentOutOfRangeException("aIndex must be positive");
        if (aIndex >= m_Numbers.Count)
            GenerateUpTo(aIndex);
        return m_Numbers[aIndex];
    }

    private void GenerateUpTo(int aIndex)
    {
        for(int i = m_Numbers.Count; i <= aIndex; i++)
        {
            m_Numbers.Add(m_Numbers[i-2] + m_Numbers[i-1]);
        }
    }
}

You can create an instance of that class somewhere (and store it for later use) or use the static “Global” instance. Of course the “Global” instance is not thread-safe. To get a certain number you can simply do:

int xp1 = Fib.Global[8];  // returns 21
int xp2 = Fib.Global[34];  // returns 5,702,887

Keep in mind that a signed 32 bit integer can only represent the fibonacci numbers up to “level” 46. The 47th number is 2,971,215,073 and a signed integer only goes up to “2,147,483,647”.

Even when using “ulong” as type (unsigned 64bit integer) you can only go up to fib number 93 which is 12,200,160,415,121,876,738

It depends on how “accurate” you actually need them to be. Of course you can exchange “int” with “double” in my class which gives you a much larger range, but the numbers quickly get inaccurate. If you just need “some level” that’s probably fine. However keep in mind when using floating point numbers the higher the number gets, the greater the smallest representable amount will be. So at high numbers you can’t add small amounts to that number.

This is the script that I got working as close to Fibonacci numbers as i could for my xp to lvl. i guess i didn’t really need to ask this question or maybe it was the wrong question either way it works now.

the script that i made

public float Xp = 0f;
    	public float XpToNxtLvl = 0f;
    	public float Health = 100f;
    	public float StatPoints = 0f;
    	public float Level = 1f;
        public Texture2D button;
   //   public float FibonacciLevel = 0f;
  //    onclick event for button
    	public void OnMouseDown()
    	{
    		Xp += 1;
    		print("+1Xp");
    	}
    //  onclickRelease event for button
    	public void OnMouseUp()
    	{
    		XpGain ();
    		if(Xp >= XpToNxtLvl)
    		{
    			print("Level Up!");
    		}
    	}

     //  not sure if this is needed but possible use in future probaly could just combine 
     //  it with nxtLvl function
        	void XpGain()
        	{
        		if( Xp >= XpToNxtLvl)
        		{
        			NextLvl();
        		}
        	}
        //  LevelUP!
        	void NextLvl()
        	{
        		Xp -= XpToNxtLvl;
        		Health += 50;
        		StatPoints += 10;
        		Level += 1;
        	}
        //  Button and other user interface things
        	void OnGUI()
        	{	
        		if (GUI.Button (new Rect (10, 70, 100, 20), "Click")) 
        		{
        			OnMouseDown ();
        			OnMouseUp ();
        		}	
        	}
            // Use this for initialization	
        //	  void Start () 
        //	  {
        //	  }   	
          // Update is called once per frame
         	void Update () 
            	{
            //     Binets Formula for getting fibonacci numbers aka using the golden ratio
//                  to get fibonacci numbers
            		XpToNxtLvl = ((Mathf.Pow(1.618034f,Level)) - (1f-(Mathf.Pow(1.618034f,Level)))) / Mathf.Sqrt(5f);
            	}
            }