Code Example Generate Random Enemy For Fight Scene

This is an example showing one way to generate a random enemy for a fight scene in pure Ink code. The code is commented to show what's happening in each variable, knot, and function. The code is playable (if you copy and paste it into Inky, it will work).

Code:
// Enemy List. Add as many as you want.
VAR Goblin="Goblin"
VAR Troll="Troll"
VAR Dragon="Dragon"

// Initialize needed variables
VAR currentEnemy = ""
VAR enemyHealth = 0
VAR enemyAttack = 0
VAR enemyDefense = 0
VAR randomNumber = 0

// Go to first knot to start the story
-> first_knot

// Function to choose the enemy.
=== function ChooseEnemy()
    ~ randomNumber = RANDOM(1, 3)
    {randomNumber:
        - 1: ~ currentEnemy = Goblin
        - 2: ~ currentEnemy = Troll
        - 3: ~ currentEnemy = Dragon
    }
    // No need to return anything from the function as we're modifying a global variable

// Function to set the enemy's stats. Set them manually or use Random to roll a dice for the stats.
=== function SetEnemyStats()
    {currentEnemy:
        - Goblin:
            ~ enemyHealth = RANDOM(50,100)
            ~ enemyAttack = 5
            ~ enemyDefense = 5
        - Troll:
            ~ enemyHealth = 100
            ~ enemyAttack = RANDOM(1,10)
            ~ enemyDefense = 10
        - Dragon:
            ~ enemyHealth = 500
            ~ enemyAttack = 50
            ~ enemyDefense = 50
    }

// First knot is just a walk to the fight scene.
== first_knot
You are walking down the hallway.
+ Keep Walking
-> fight_scene


== fight_scene
As you walk down the hallway, you hear a strange sound...

//  Call the functions to choose a random enemy and set the enemy's stats.
~ ChooseEnemy()
~ SetEnemyStats()

// Tell the reader what enemy was choosen
It's a {currentEnemy}!

// Tell the reader the stats of the enemy choosen.
You are about to battle a {currentEnemy}!
Its stats are:
Health: {enemyHealth}
Attack: {enemyAttack}
Defense: {enemyDefense}

// Send the reader to the battle or let them run away.
+ Fight ->
-> battle_knot
+ Flee ->
-> first_knot

== battle_knot
// There is no battle code in this example.
-> END

Notes:
  • This code as written only generates a single "currentEnemy". If you want your reader to fight a group of enemies, you'll need to modify the code or look for other ways to do that.
  • This code uses the built in Ink random number generator for some stats. "RANDOM(50,100)" generates a random number between 50 and 100). Whenever you want to generate random numbers, use RANDOM(MIN,MAX) with two numbers (MIN and MAX) where the first represents the minimum number to be generated and second the maximum number.
  • In Ink, there are many different ways to do the same thing, this is just one example. If it doesn't work for you, please check for other methods.
  • There is no actual fight/battle code in this example, this just shows a way to generate random enemies.
  • This code is not only useful for enemies. It can be modified to generate a random sex partner, vehicle, item, NPC, or anything else you need to generate randomly.
Sources that were used in working on this code: Travel's Tale on GitHub, Procedural Generation in Ink at Digital Ephemera

If you have questions on the code or suggestions to improve the code, feel free to discuss in the replies.
 
Last edited:
Back
Top