Creating functions which return a value

From QuestWiki
Jump to: navigation, search

We've already covered Using Functions earlier in this tutorial, where we created a function so that we could call the same script from multiple places.

We can also create functions which return a value. Previously, we called our function from a script command, but when a function returns a value, we can call it from an expression. The function will run, and the return value will be used in the expression.

Hopefully an example will make things clear. Let's make a simple function that will add two numbers together.

Create a function called "adder" and set its return type to "Integer". Our function will take two parameters, let's call them "number1" and "number2". Add these to the parameters list.

To the script, add a "Set this function's return value" command. Enter the expression:

number1 + number2

The function editor should now look like this:

Functionvalue.png

We can now call the function from any script command. Let's add a script command "test" just to check that it works.

In the script for the command, add a "print a message" command, and enter the expression:

"1 + 2 = " + adder(1, 2)

The command should look like this:

Callfunction.png

Run the game and type "test". You should get the correct output:

> test
1 + 2 = 3

Tutorial

Beginners

  1. Introduction
  2. Creating a simple game
  3. Interacting with objects

Intermediate

  1. Using scripts
  2. Custom attributes
  3. Custom commands
  4. More things to do with objects
  5. Using containers
  6. Moving objects during the game

Advanced

  1. Using timers and turn scripts
  2. Changing templates
  3. Text formatting
  4. Using lockable exits
  5. Using lockable containers
  6. Using walkthroughs
  7. Multiple choices - using a "switch" script
  8. Debugging your game
  9. Releasing your game

Elite

  1. Creating functions which return a value
  2. Using inherited types
  3. Using libraries
  4. Using Javascript
Personal tools
Namespaces
Variants
Actions
Navigation
Quest Documentation
Toolbox