Tutorial - Loops & Arrays – Ylands

From Bohemia Interactive Community
Jump to navigation Jump to search

Arrays are basically groups of ordered variables. Arrays can hold objects of various types (but in that case you need to make sure your code takes that into account) which can be accessed via index (starting with 0).

As for the loops, Ylands support various types - While, Repeat, Count with and For each.


Note:

Since Ylands visual scripting is, in a way, subset of Java Script, you may refer to JS manual when you need a more thorough explanation of a specific instruction.


The Table

Whenever you enter the Trigger Zone next to the table, all things your character possess will be printed into the chat window. This is done via a simple foreach loop. This loop is very easy to work with but has some shortcomings when compared to Count with (less control over the loop, can’t easily count iterations etc.).


The Chest

Here we work with arrays. When the player presses the button we first get their inventory content as an array, use an instruction that selects a random entry from that array and the resulting item we put into a container.

Please note that we use a duplicate code in events executed when pressed both depressed item is interacted with. This isn’t a very nice way of doing things and here we use it only as a simple example. Whenever two pieces of code are the same or very similar, it makes sense to put them inside a Custom Instruction - but we’ll get to that later.


The Door

The door opens when the player has the statue in the inventory. The check is very simple - once again we get the array inventory items (of the entity that initiated the interaction with the door - the player) and inside of it we look for a specific item.


Note:

While in the mentioned example we are detecting a specific object, this wouldn’t work if we placed two identical statues in the scene and from the script pointed to one. The door would only open when the exact statue (with the exact ID) was present in the inventory. If you want to detect an object of a certain type (any iron sword instead of THE iron sword), you have to work with entity types instead of specific entities.