Classes
This tutorial shows how you can implement user-defined classes with private fields and public functions into your scripts.
Last updated
This tutorial shows how you can implement user-defined classes with private fields and public functions into your scripts.
Last updated
In this tutorial we will have a look at how classes are implemented in HaasScript and how you can use them.
Classes are useful in cases where you need to have multiple instances with same functionality. Examples of these instances would be a custom order management system and a bot system for easier multi-market management.
It is warmly recommended that you check out this page before continuing with this tutorial:
First off, we will start by creating a new script in the script editor.
Keep the script type as default (Script) and name it "Class Tutorial".
Classes in HaasScript are created inside a function as objects and new instances of these objects are returned when calling that function.
Here we start off by creating a function called Calculator, and we give it 2 input parameters.
There's our starting point.
Objects in HaasScript are as in any programming language; collections of accessible data fields and/or functions. Defining an object happens as follows:
Oh, what is that you say? It's an array? Pffft. I beg to differ...
Although this isn't exactly what we are looking for; this object is static - we can't create multiple instances of this object and we can't access the fields within the object itself:
Thankfully there's a solution for this: function to return objects. This way, whenever we call the function, we will get a new instance of that object and the things inside will also work a little different... Maybe?
Let's continue by giving our Calculator two fields and a function.
Great! But... Wait a minute... How do we access these fields and function? This function doesn't return anything!?
Right, of course. We create an object out of them by simply assigning the insides of the function to its new handlers:
There we go. There's one thing I have to mention though...
We need these - BAD.
The Value1
and Value2
are visible outside of our class, sure. But if we change the values, they wont change internally.
Let me show you what I mean...
Isn't it weird? Yes and no I'd say. The exposed fields that we change the value for are their own variables and they do not access the internal fields in any way. They do get their values from the internal value1
and value2
but only when the object is created. After that, they are what they are. Same would happen if we changed the Calculate
function to something else.
You could consider these as some type of "memory slots" that are specific to the created object.
I know it sounds confusing, but don't worry. I'll show you what we can do to grant us access to the insides:
There we go. Now we have 4 functions that actually "go inside" our class and does something in there. This way, we can manipulate the values and it will affect the outcome of our Calculate
method.
So let's try that earlier experiment again, this time with our getters and setters:
That's pretty much it for classes. You know what to do next...