Before you begin with this tutorial, make sure you have installed the Direct .. For a list of the operators, you can check the AngelScript manual. This series of tutorials is intended to allow anyone, whether already a programmer or not, to learn how to use Angelscript. Before starting, please read through. Copy over the earlier tutorial’s code: void AskQuestion(const string& in szQuestion, string& out szAnswer, const string& in.
|Published (Last):||17 July 2005|
|PDF File Size:||18.40 Mb|
|ePub File Size:||4.72 Mb|
|Price:||Free* [*Free Regsitration Required]|
Already have an account?
In this tutorial, we’re going to take the program we wrote in the Advanced user input and output tutorial, and rewrite it to use a class based approach. Create script file 2: Copy earlier tutorial 3: As before, create a script file in the directory where you have placed the Sandbox application. Name it something descriptive. The numeric question asking function has been provided for you here.
Now run the script to make sure everything works, and that your command line is set up to run the script. As you may recall from the earlier tutorial, we asked the user for some information, and then displayed it at the end. We’re going to put this information into a class:. This is how you declare a simple class. The name of the class is ‘Person’, with the variables we previously declared in the function as local variables now being member variables.
Now let’s rewrite our main function to use the class instead of the local variables:.
The syntax for declaring a class instance is identical to how you’ve declared variables before. The class’s variables are initialized automatically, with the house number having the value 0 assigned.
Accessing the variables is relatively straightforward: By default, member variables are publicly accessible, but it is possible for access to be restricted. We’ll cover this in a later tutorial. Run the code you’ve written. It should work exactly as it did before.
Now let’s move some of the code into separate functions. A method is a function that is part of a class. It can only be called using an instance of the class to which it belongs, and can access all of the class’s variables. Let’s put the last line of code into a method called Print:. Make sure to remove the “person.
There’s only one thing left to do now, and that is to take the user input and put it into its own function. We can do this in one of 2 ways; as a method, or a global function. Using a method here might seem like a good idea, but it actually closely relates your Person class with getting user input from cin. This can be problematic if there are multiple ways to get a person’s information.
So let’s use a global function; to do this, we need to give the function our person instance. There are 2 ways to do this, by using an out reference parameter, which we’ve already seen, and a handle. Let’s use the reference approach first:. Looks pretty good, right? Replace the code in main with a call to PersonFromCin personand everything should work fine. Unfortunately, this causes a problem. Not one we can see right now, but it becomes very obvious when we add something new.
Make sure to output the id in the Print method as well. Now run the script, and look at which id is output. Instead of 1, it’s 0.
How is this possible? The reason for this is that, when using reference parameters, the variable is always copied. An “in” reference is copied so the function can modify that, but does not affect the original variable. An “out” reference is copied from the function to the variable you give it.
This means that any changes you made to the variable before calling the function are lost. For this reason, when using classes, you should always use handles. Handles are a way to refer to the same class instance, without causing copies to be made. This is how we would implement PersonFromCin using handles:. Handles are declared using an ‘ ‘. Notice how the parameter is now named “pPerson”: Unlike references, handles allow the same object to be used, so Print now outputs the correct id. As a rule of thumb, when dealing with classes, you should always use handles, unless you explicitly want the angelscgipt that occurs with pass by value or tutorual pass by value is functionally identical to passing a non-const in reference.
However, if you try to use handles with string, cin tutogial cout, you’ll notice that the script fails to compile. This is because there are 2 types of classes in Angelscript. Value types are classes that behave like primitive types; you cannot pass a value type into a function and still refer to the same instance.
The second type of class is called a reference type. All classes declared in scripts themselves are reference types, as well as certain classes that the application provides.
A reference type is, as the name implies, reference counted, which means that every reference to a class’s instance is ‘counted’, ensuring that it continues to exist until all references to it have been removed.
This allows it to be safely passed into functions, provided you pass a handle to the instance, not the instance itself.
AngelScript Tutorial [Frictional Game Wiki]
Since value types are not reference counted, they may cease to exist at angelscrjpt time, which could cause code to access freed memory. This can cause anything from data corruption, arbitrary code execution, to crashing the entire program. Handles have a bit of unique syntax to them. Handles can be “null”, that means it is not a handle to any instance. Attempting to use a null instance will cause an exception, which halts code execution. Assigning an instance to a handle requires use to use the ‘ ‘ symbol again:.
If you call Print on it, it will output the same result, because it is tutprial same object. Determining whether a handle is null or not can be done like so:.
It is also possible to have function parameters that take references to handles. This is a bit of an edge case, but may prove useful in some situations:.
In this case, it is the handle, not the instance, which is being copied, so it still points to the same object. If this were an output reference, the handle would be null, but the handle that was passed in would be changed. If you were to angepscript this syntax, you’d also have to explicitly convert the instance to a handle to be able to run the code:. Using an ‘ ‘ before a class instance converts it to a handle.
Note that this only works with in references; since the handle is amgelscript temporary variable, it angelscfipt not be possible to pass this in directly to a function expecting an out reference. Also note that it is valid to explicitly convert a class instance, or even a handle, to a handle using ‘ ‘. This will cause no problems, and may make it easier to find conversions to handles. To tutoriak text into a link, highlight the text, then click on a page or file from the list above.
To edit this page, request access to the workspace. Classes, part 1 Page history last edited by Solokiller 3 years, 3 months ago. Page Tools Insert links Insert links to other pages or uploaded files. Pages Images and files. Insert a tutorkal to a new page. No images or files uttorial yet. Insert image from URL.
To join this workspace, request access. Show 0 new item s. Classes, part 1 Page history last edited by Solokiller 3 years, 3 months ago In this tutorial, we’re going to take the program we wrote in the Advanced user input and output tutorial, and rewrite it to use a class based approach.
Angelsceipt Images and files Insert a link to a new page Loading Insert image from URL Tip: Join this workspace To join this workspace, request access.