Code Example RPG Level Up System in Ink Code Using Functions & Tunnels

This is an example of how to write an RPG level up system in pure Ink code using both Functions and Tunnels. The code is commented to show what each part of the script does. This type of reusable leveling up logic or character progression mechanics can be used in any type of game/story to give the player more skills as the game progresses and gets more challenging. The code below can be copy and pasted into Inky and it will be playable.

Code:
// Setup Level Variables
VAR level = 1
VAR experience = 0

// Setup Character Starting Stats Variables
VAR strength = 10
VAR dexterity = 10
VAR vitality = 10
VAR luck = 10
VAR intelligence = 20

// Setup Hit Points (HP) and Magic Points (MP) Variables
VAR maxHP = 0
~ maxHP = 50 + (vitality * 4) // Maximum Hit Points depend on Vitality stats
VAR currentHP = 0
~ currentHP = maxHP // Start with maximum Hit Points
VAR maxMP = 0
~ maxMP = 40 + (intelligence * 5) // Maximum Magic Points depend on Intelligence stats.
VAR currentMP = 0
~ currentMP = maxMP // Start with maximum Magic Points

// Setup Gold Variables
VAR gold = 100 // Starting amount of gold

// Setup Experience Required For Each Level Here
VAR experienceForNextLevel = 1000

// Start of the story
-> start

=== function check_level_up ===
    {
        - experience >= experienceForNextLevel: // Check if the user's experience is equal to or greater than what's needed to reach the next level. If it is, then increase the level, update the experience needed for the next level, and return true
            ~ level += 1
            // Customize how each stat will increases with each level
            {
                - level == 2:
                    ~ strength += RANDOM(5, 20)
                    ~ dexterity += RANDOM(1, 5)
                    ~ vitality += RANDOM(1, 5)
                    ~ luck += RANDOM(1, 5)
                    ~ intelligence += RANDOM(5, 20)
                    ~ maxHP += vitality * 2
                    ~ maxMP += intelligence * 4
                    ~ gold += RANDOM(50, 150)
                - level == 3:
                    ~ strength += RANDOM(10, 40)
                    ~ dexterity += RANDOM(5, 20)
                    ~ vitality += RANDOM(1, 5)
                    ~ luck += RANDOM(1, 5)
                    ~ intelligence += RANDOM(5, 20)
                    ~ maxHP += vitality * 2
                    ~ maxMP += intelligence * 5
                    ~ gold += RANDOM(500, 700)
                - level == 4:
                    ~ strength += RANDOM(1, 5)
                    ~ dexterity += RANDOM(1, 5)
                    ~ vitality += RANDOM(5, 20)
                    ~ luck += RANDOM(1, 5)
                    ~ intelligence += RANDOM(5, 20)
                    ~ maxHP += vitality * 2
                    ~ maxMP += intelligence * 7
                    ~ gold += RANDOM(50, 150)
                - level >= 5: // The last entry says "equal to or greater than" so that the same increase will continue to happen for each level after this one
                    ~ strength += RANDOM(10, 25)
                    ~ dexterity += RANDOM(10, 25)
                    ~ vitality += RANDOM(10, 25)
                    ~ luck += RANDOM(10, 25)
                    ~ intelligence += RANDOM(5, 20)
                    ~ maxHP += vitality * 2
                    ~ maxMP += intelligence * 8
                    ~ gold += RANDOM(50, 150)
            }
            ~ experienceForNextLevel = calculate_experience_for_next_level(level) // Update the variable that contains the amount of experience needed to reach the next level
            ~ return true
        - else:
            ~ return false // If no new level is reached, return false so that our level_up_check_and_text knot knows that we need more experience before reaching the next level
    }

=== function calculate_experience_for_next_level(currentLevel) ===
    // Hard coded level experience requirements for the fist few levels
    {
        - currentLevel == 1: ~ return 2000 // Experience needed to reach level 2
        - currentLevel == 2: ~ return 4000 // Experience needed to reach level 3
        - currentLevel == 3: ~ return 10000 // Experience needed to reach level 4
        - currentLevel == 4: ~ return 20000 // Experience needed to reach level 5
        - else: ~ return experience * (RANDOM(1,2) + (currentLevel * RANDOM(1,2))) // The last entry says for all levels above 5, make a calculation using current experience and some random logic to determine the experience needed for the next level
    }
    
== start
You're standing in the forest.
You have {experience} experience.

Player Stats: // Output player stats so we can make sure they're working during writing of the game
Level: {level}
Strength: {strength}
Dexterity: {dexterity}
Vitality: {vitality}
Luck: {luck}
Intelligence: {intelligence}
Max HP: {maxHP}
Max MP: {maxMP}
Gold: {gold}
+ Get Experience
~ experience = (experience + 100000) // Give the player 100,000 experience for testing the code
You get 100000 experience.
-> level_up_check_and_text -> // Tunnel statement to the knot to check for a level up and then come back here
-> start

== level_up_check_and_text ==
    {level >= 5: // Set a maximum level in the game to prevent unexpected behavior
    You've reached level 5, the highest level possible, you should be able to beat the game now!
    ->-> // Tunnel return statement to skip the level up check
    }
    ~ temp leveledUp = check_level_up() // Call the level up function to get a true (leveled up) or false (need more experience to level up) if the maximum level hasn't been reached
    {
        - leveledUp: // Text to show if the user leveled up
            You've reached level {level}! Your stats have increased, and your maximum hit points and magic points have gone up. You've also earned a gold bonus.
            -> level_up_check_and_text // Recheck in case the user made more than one level in a single move
        - else: // Text to show if the user needs more experience to level up
            You need {experienceForNextLevel - experience} more experience points to reach level {level + 1}.
    }
    ->-> // Tunnel return statement

Notes:
  • The above code uses some of Ink's advanced features including Tunnels, Functions, Conditional Logic, Random Numbers, and Math Logic.
  • This leveling system hard codes the experience needed and stats updates for first few levels and then uses calculations and randomness for the higher levels. You can easily add as many hard coded levels as you want, even hard coding the logic for every level up in the game.
  • The "level_up_check_and_text" knot is used to separate the text from the functions (logic) and it also includes logic to set a maximum level. Setting a maximum level is recommended, even if it's a very high maximum, since Ink could reach it's maximum number limit if experience becomes too high, and there is a risk of entering a loop or other unexpected behavior if you don't set a maximum.
  • When setting up initial user stats and when increasing user stats during level up, consider using calculations that take other stats into account. In this example, we take vitality into account when calculating hit points and we take intelligence into account when calculating magic points. By using math formulas that change stats based on other connected stats (mixed with random numbers), you create a more immersive experience where each play of the game is unique.
  • This example code does not have any logic related to how the player gets experience in the game, we give them experience every time they click the "Get Experience" choice (~ experience = (experience + 100000)) so that we can test the code. Experience in your game could be gained by completing tasks, finding items, reaching locations, winning battles, reading books, etc.
  • There are different ways to accomplish things in Ink code. If this level up system doesn't meet your needs, you could rewrite it using only Tunnels or adding more features, etc.
If you have questions about this code or suggestions on improving the code, please leave a reply in this thread.
 
Back
Top