Harlowe Check If Number

6 min read Oct 04, 2024
Harlowe Check If Number

Harlowe: Checking if a Variable Holds a Number

Harlowe, the scripting language used in Choice of Games' visual novels, provides a versatile toolkit for creating dynamic and engaging stories. One common need in game development is to determine if a variable contains a numerical value. This is useful for a variety of scenarios, such as:

  • Validating user input: Ensure that the player enters a number when required.
  • Conditional branching: Guide the story based on whether a variable represents a numeric value.
  • Performing calculations: Avoid errors by only performing mathematical operations on valid numerical data.

Fortunately, Harlowe offers a straightforward way to check if a variable contains a number using its built-in functions. Let's explore how to achieve this.

Using is:num

Harlowe's is:num function is specifically designed to test if a variable holds a numeric value. Here's the basic syntax:

(is:num $myVariable)

This code snippet will return true if $myVariable contains a number, and false otherwise.

Example:

* "What's your lucky number?"
* "I'm thinking of [num: $luckyNumber]. Is it [it: $luckyNumber]?"

    * "Yes, that's right!"
    * (if: (is:num $luckyNumber))
        * "That's amazing! You got it."
    * (else:)
        * "Hmm, not quite. Try again."

In this example, is:num is used to check if the player's input ($luckyNumber) is indeed a number. If it is, the story proceeds with a positive response; otherwise, it prompts the player for another guess.

Handling Different Data Types

Remember that Harlowe variables can store various data types, including numbers, text, and lists. It's important to be aware of these distinctions when checking for numeric values.

Example:

* $myText = "Hello!"
* $myNumber = 123
* $myList = [1, 2, 3]

* (if: (is:num $myText))
    * "This is a number!" (This will not be displayed as $myText is not a number.)
* (else:)
    * "This is not a number."

* (if: (is:num $myNumber))
    * "This is a number!" (This will be displayed as $myNumber is a number.)
* (else:)
    * "This is not a number."

* (if: (is:num $myList))
    * "This is a number!" (This will not be displayed as $myList is a list.)
* (else:)
    * "This is not a number."

In this example, we see how is:num works with different data types:

  • $myText stores text, so is:num returns false.
  • $myNumber stores a numerical value, so is:num returns true.
  • $myList stores a list of numbers, so is:num returns false.

Combining is:num with Other Logic

is:num can be combined with other Harlowe functions and operators to create more complex logic.

Example:

* "Enter a number between 1 and 10:"
* $userInput = input
* (if: (is:num $userInput) and ($userInput > 0) and ($userInput <= 10))
    * "You entered a valid number!"
* (else:)
    * "Invalid input. Please enter a number between 1 and 10."

In this example, we check if the player's input is a number, and if it's within the specified range.

Tips and Best Practices

  • Error Handling: Always handle cases where the user input might not be a number. This can prevent unexpected errors in your game.
  • Clear Instructions: Provide clear instructions to players about the expected input, especially when requiring numerical data.
  • Testing: Thoroughly test your code with different input scenarios, including valid numbers, invalid inputs, and edge cases.

Conclusion

The is:num function is a powerful tool for enhancing your Harlowe scripts. By checking whether a variable holds a numeric value, you can ensure your game logic works correctly, provide a smoother player experience, and prevent unexpected errors. Remember to use this function wisely, along with other Harlowe features, to create engaging and dynamic interactive stories.