We're going to take a look at how to create methods and put them into our power, Geologix.
We're going to look at the ad member power shall command it.
We're also going to take a look at the member type called script method.
These are methods.
They're created inside your scripts.
We're going to be using the name parameter to provide a name to our method and then the value parameter
to tell you what to do when this method is called.
Let's take a look at some code here.
We're going to go ahead and create a brand new object.
This object is only going to have one property that we can stay focused on what we're trying to actually
accomplish.
We're going to simulate some type of a moving object.
So I'm going to put this in the memory and I'm going to go ahead and display that object.
It currently has a speed value represented as zero.
Let's take a look at the members of this object.
You can see that we have our standard default methods provided by power show and we have our one property.
This objects not able to do a whole lot because we can't tell it to start moving.
So we're going to add a method in this case that's going to be something similar.
If we press the accelerator on our car, we're going to call this method accelerate and then we're going
to provide to the value.
The value is going to be this objects current speed property will now be equal to whatever it is right
now.
Plus five, the this variable represents the object that this method is a part of and it can call any
property from that object and use it and modify it.
I'm going to go ahead and read this into memory.
You can see how we're piping our object into ad member.
Remember, dollar sign object is just a variable that holds a copy of her object.
I'm going to go ahead and read this in the memory and now I'm going to ask for the member data here.
You could see we have our original current speed property and now we have our method called accelerate.
Since our method did not have any input provided to it, we're going to be using a empty set of parentheses.
We'll change this in just a few minutes.
I'm going to go ahead and call our method.
And now let's take a look at the value of the object, you could see the current speed is now five.
If I were to call this again and check it out, you could see it's now 10.
That's because we hardcoded five as the increase in our speed.
Let's add an argument that we get to provide the speed that we're going to increase by here, we have
our object being piped in to add member reconned a member type parameter.
And what we're going to be doing with it is creating a new script method of do take note that I'm using
the back to character to try and keep the screen very clean here.
The back tactic may be difficult to see on the American keyboard.
It is above the tab key and allows me if I press enter right after it to continue typing on the next
line.
Here we have the name parameter we're going to call our method accelerate more.
This time we're going to provide a value inside the value.
We have some some more code.
We're calling this object's current speed property and we're going to set an equal to the current speed
property plus a value called amount.
Amount is actually a parameter.
We're going to pass information into this particular script method by default amount is going to be
zero.
So if you call this method and don't provide it anything, it's not going to accelerate.
But this is where we can provide a value to increase our speed.
Let's add this method to our object.
Let's confirm that is there and accelerate more is present.
I'm going to go ahead and accelerate this object by factor of fifty.
Remember the current value.
Let's just check out the current value.
Is that ten?
I'm going to accelerate more.
And now I'm going to take a look at the current value of the object.
And our current speed is 60.
That is what a method allows you to do.
It allows you to take some type of action against the object that that method is part of.
What we learned in this chapter is what a method is.
It is simply software that's added to the object at the consumer of the object can call and use.
We learned how to not only create methods, but also call the methods, all methods.
And in parentheses, even if they do not get a value, they end in an empty parentheses set.
We learned how to create methods that do not accept values and we learned how to create methods that
do accept values.
You're going to find methods to be very specialized cases.
Most of the time we are dealing with properties.
However, as power still continues to evolve, particularly in the Power Channel five, you're going
to find that power usage is going to go beyond the ability for us to just manager environments and we'll
be able to create more applications using powerful.