Objects are the most important things in VyScript. Most things are
objects in some form, so it is crucial to understand what an
object is.
In simplistic terms, an
object is a container that holds named
variables (you can give them any name you like) and each one points to some type of value. That value can be a
number, a
string, a
boolean, an
array, an
object, a
function, or other various
values.
The most basic form of an
object takes on the JSON (JavaScript Object Notation) format. When written, a JSON looks something like
{"var1": 1, "var2": 2}, where
var1 (a
variable with a name represented by a
string) has the value of
1 and
var2 has the value of
2.
While basic
objects have a JSON-like format, the
object can come in many forms. Below are some examples of a few of those forms.
Note:Do not add any example code to your project unless specifically told to.
{var1: 1, var2: 2}
{'var1': 1, 'var2': 2}
{"var1": 1, "var2": 2}
Object Types
Basic
objects are individual
objects that have their own values and do not share any values or have any relationship to other
objects. An
Object is a special type of
object that can be used as a blueprint to create new
objects which have default values and share certain properties. An
Object has a
type in order to identify it. Below is some example code of how we can define our own
Object type that is defined as
MyObject and has two variables called
myVar1 and
myVar2 which equal
1 and
2.
MyObject
var myVar1 = 1
var myVar2 = 2
Proper indentation is
required in VyScript. In order to indent a line, simply insert a tab into the code with the tab key. Each indentation determines the ownership of the line of code. In the code above, both
variables have one indentation to show they belong to the
MyObject Object.
MyObject is the parent of the
variables.
Most
Objects are allowed to have child
types. Children will inherit the properties from all parent
Object types. Children will override any property the parent has given them if it has the same one set. Below is some example code of how to create child
Object types.
MyObject
var myVar1 = 1
var myVar2 = 2
Child1
var myVar1 = 45
var myVar3 = 'child1'
Child2
var myVar2 = 54
var myVar3 = 'child2'
In the code above we define three
Object types, with two of them inheriting properties from a parent.
MyObject is the lowest level parent and it gives every child the
myVar1 and
myVar2 properties.
MyObject/Child1 and
MyObject/Child2 (
Note:when writing
Object types, each
Object is separated by a forward slash) are the child
Objects.
MyObject/Child1 overrides its parent's
myVar1 with
45 and defines its own
myVar3 variable which is a
string with the value
'child1'.
MyObject/Child2 overrides its parent's
myVar2 with
54 and defines its own
myVar3 variable which is a
string with the value
'child2'.
Note:Putting
var infront of a
Object variable definition is optional as long as you provide a value, but it can be helpful to quickly and easily see what the line represents and know for sure that the
variable is custom.
Default Object Types
VyScript has some
Object types built-in to it to help developers work with the Vylocity engine.
The most important
Object is the
Diob, which stands for
display object. Most engine features require a
Diob, so in most cases we will be working with
Objects that are or inherit from
Diob.
We wont be going over all the different
Object types that inherit from
Diob in this tutorial, but below is a list of them.
While
Diob is the most used, there are a few other built-in
Object types. Here is a list of those
Objects.
First Project
Now that we understand the basics of
Object types, we can set a few properties of one of the built-in ones to make some changes to our project.
Note:If you did not do the first
First Project tutorial, be sure to head back and do it first. The rest of this tutorial will assume that it has already been done.
The
World Object is the game's main
Object. Only one of them exists and the engine creates it for you automatically. It holds
variables and
functions which are needed to set game (the world) settings and access resources or
Objects that exist in the world. One of those settings allows us to set the size of the game window, so that is what we will do first.
Open the
main code file in our
First Project project and enter the following code.
World
gameWidth = 960
gameHeight = 540
This code sets the game's width to
960 pixels and the game's height to
540 pixels.
Note:Built-in
Object variables do not have
var included in it's definition.
Because these values are the default sizes for the engine, they wont technically make any difference, so try a few other numbers and then build and run the project to see it in action for yourself. Once you are satisfied, change it back so that we're all on the same page moving forward.
Now we need something to look at other than a black screen. In the previous tutorial we created some icons, but now we need to define some
Diobs in the code so we have something to assign our icons to and some things to place down on our map.
As we learned in the previous tutorial, a map is made up of a grid of
Tiles. Each coordinate on the map has one
Tile and each
Tile is a container that holds overlapping
Diobs in it's
contents. The engine automatically gives each coordinate a
Tile so that the developer does not have to (but they can be replaced with a custom
Tile if desired). Because of this, we can simply change the default
Tile type to give certain properties to every
Tile on our map, so add the following code to the bottom of our
main code file.
Tile
atlasName = 'icons'
iconName = 'tile'
This code gives every
Tile an
atlasName of
'icons' and an
iconName of
'tile'. We defined these two things when we created our
icons icon atlas file. The
atlasName is the icon atlas file we want to use and
iconName is the name of the icon inside of that icon atlas.
So now we can build our project (Project->Build) and go to the
map map file we created in the previous tutorial. Each
Tile should now be using the icon we assigned it. If we run the project (Project->Run) we should now see the map using our icons and be able to move the screen around on the map by using the arrow keys or the WASD keys.
Every player that connects to the
World is given a
Client, and every
Client has ownership and control over a player
Mob, known as a
playerMob. A
Mob automatically inherits from
Movable (and that automatically inherits from
Diob) which allows the
Mob to move around on the map and interact with things. If not specified otherwise, every new
Client creates a new
Mob to act as the
playerMob.
Now that the map has a visual representation, let's also give our player a visual representation. Add the following code at the bottom of our
main code file.
Mob
atlasName = 'icons'
iconName = 'player'
xCoord = 5
yCoord = 5
This code gives every
Mob an
atlasName of
'icons', an
iconName of
'player', an
xCoord of
5, and a
yCoord of
5. The
xCoord is a
Diob's
x coordinate on the map and the
yCoord is a
Diob's
y coordinate on the map, starting from the top-left of the map. So our player should be located at
5,
5 when it is created, which is
5 Tiles from the top edge of the map and
5 Tiles from the left edge of the map.
Build and run the project and you should now see yourself moving around on the map.
Let's now create some child
types.
Change your
Mob type definition code to look something like the code below.
Mob
atlasName = 'icons'
Player
iconName = 'player'
xCoord = 5
yCoord = 5
Monster
iconName = 'monster'
Our project now has two new
Object types called
Mob/Player and
Mob/Monster. We will use
Mob/Player as our player character and
Mob/Monster as a generic monster.
In order for the engine to know that we want the player character to be
Mob/Player we will need to define it. This can be done by setting the
mainMob variable under
World. We simply point it to
Mob/Player and every new
Client that connects will have a
playerMob using our new
Mob/Player Object type.
Find your
World definition and change it to look like the code below.
World
gameWidth = 960
gameHeight = 540
mainMob = 'Mob/Player'
Now that we have a monster
Mob, we will want to put some
Mob/Monster down on our map.
In order for the map editor to know about our new
types, we have to build the project. So build the project and then open the
map map file.
On the left side of the map editor is a box labeled
Diob Types. This is a list of all the
Object types that exist in our game that we can place down on our map.
Click on the
+ in the list that is next to the
Mob label to show our two new
types. Select
Mob/Monster, which will just be labeled
Monster, and then place a few of them on the map in the middle of the page by simply clicking.
Build and run the project and as we move around on the map, we should see our monsters, which we are unable to move through because they are dense/solid.
Every
Diob has a
density variable that determines if things collide with it or not. Any
Diob that is a
Mob or inherits from it will automatically have a
density of 1, which makes it solid.
Mob is meant more for living things in the game, things that might have some sort of intelligence. But we also need things that are used for decoration or maybe just used for simple interactions.
Let's add some new
types to our project that we can place on the map for decoration. Add the following code to the bottom of your
main code file.
Diob
atlasName = 'icons'
Solid
iconName = 'solid'
density = 1
NotSolid
iconName = 'not_solid'
plane = 1
layer = 20
We now have two new
types in our project called
Diob/Solid and
Diob/NotSolid. As the names indicate,
Diob/Solid is solid and
Diob/NotSolid is not, however
Diob/NotSolid has
plane set to
1 and
layer set to
20. The
plane and
layer variables determine the order of things being drawn onto the screen. Things with higher
planes will be drawn after things with lower
planes and things with higher
layers will be drawn after things with lower
layers (so they will appear above or closer to the viewer) with
planes taking priority. So something with a higher
plane will always be above something with a lower
plane no matter what the
layer is set to. You can think of it like a decimal where the combination of our set
plane and
layer comes out to be
1.20.
Mob has a default
layer of
4 and a default
plane of
1 so our player will move under our
Diob/NotSolid.
Build the project and add some of our new
types to the
map map file. Once done, build again and run the game and then run around the map to see how the engine handles everything.
Before we end this tutorial, let's add some new child
types to our
Tile definition so we can place down some
Tiles that have different appearances.
Find your
Tile definition in the
main code file and make it look like the code below.
Tile
atlasName = 'icons'
iconName = 'tile'
Grass
iconName = 'grass'
Dirt
iconName = 'dirt'
Build the project, open the
map map file and place some of our new
Tile/Grass and
Tile/Dirt on the map. Then build the project again and run it to see the final results of this tutorial in action.
Note:As you place down
Tiles you will notice that the one you placed down replaces the old one. This is because each coordinate is designed to have one single
Tile which acts as a "floor" for the map visually. You can stack tiles by holding the
Shift or
Ctrl keys, but it is not recommended unless you only want to add an addition visual thing that is not interacted with.
If you followed everything in this tutorial, the source of your project should look something like this:
https://vylocity.com/ide/Vylocity/FirstProject1/
References