Ylands Tutorial - Custom instructions

From Bohemia Interactive Community
Jump to navigation Jump to search

Custom instructions are pieces of code that perform some action and can return a value - either a single variable or an array.

Even though it may sound difficult, it really isn’t. Imagine you have a certain code that you need to perform with three different objects. One way is to have three instances of this code, each affecting a different object. The other way is to create a custom instruction which contains this code and performs it with any object you provide it with.

Whenever you have a code that needs to be re-used or several pieces of code that do a similar thing and can be merged into one which works with objects and parameters you provide, then it’s a good idea to use custom script.

Another reason could be to make your code cleaner and easily readable. Imagine that when some event is triggered you need to perform several actions - each of them consisting of several tiles. If you just snap all these tiles one after another and attach them to the event you will get a long, ugly code. If you, however, encapsulate each of the logical segments into Custom instructions and call those instead, you (and anyone else) will be able to find your way around this code much faster.

The Trigger Zone

When you enter the Trigger Zone its script will check what (if any) book you have on you. It will tell you if it’s the correct book - the actual logic for checking that is in neatly named Custom Instruction. You just provide a book and a Player whose inventory should be checked and it will return true or false based on the result.

While this Custom Instruction’s name indicates it is used to find a book, it would work with other items as well so you could easily change the instruction into something like Does player have item. Also you can see that there’s not much going on in this Custom Instruction but you can (and probably will) use those to perform quite complex operations.