Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
0.8.3:scripting [2021/10/13 13:09] – [Routines] admin0.8.3:scripting [2021/10/13 13:41] – [Scripting] admin
Line 143: Line 143:
  
 ==== Global Functions and Procedures ==== ==== Global Functions and Procedures ====
 +
 +<file pascal>
 +program demo;
 +
 +uses
 + System.SysUtils;
 +
 + function Calculate(A : Integer; B : Double) : Double;
 + begin
 +   Result := A * B;
 + end;
 + 
 + procedure Output(AStr : String; _A : Integer; _B : Double);
 + var LStr : String;
 + begin
 +   LStr := Calculate(_A, _B).ToString();
 +   System.WriteLn(AStr + LStr);
 + end;
 +
 + procedure Main();
 + begin
 +   Output('Result = ', 10, 123.45);
 + end;
 +
 +begin
 +  Main();
 +end.
 +</file>
 ==== Special Features ==== ==== Special Features ====
 GorillaScript provides some useful features for faster development, which are not compatible with Delphi. So don't use those, if you'd like to use your source in both (Delphi & GorillaScript) GorillaScript provides some useful features for faster development, which are not compatible with Delphi. So don't use those, if you'd like to use your source in both (Delphi & GorillaScript)
Line 514: Line 542:
 === Fields === === Fields ===
  
-=== Methods ===+<file pascal> 
 + TTest class 
 + protected 
 + FFieldBool : Boolean; 
 + FFieldStr : String; 
 + FFieldInt : Int32; 
 +  
 + public 
 + FFieldDbl : Double; 
 + FFieldObj : TTest; 
 + end; 
 +</file>
  
 === Properties === === Properties ===
 +
 +Properties are a very useful tool to access fields directly or by a getter or setter method.
 +In the following example we extend our TTest class by a GetFieldBool() and SetFieldBool() protected method.
 +In the public property "FieldBool" we can access the protected field "FFieldBool" by those methods
 +indirectly. We than have the option to influence behaviour.
 +
 +The second property "FieldStr" is set to read-only on our protected field "FFieldStr", which forbids writing to. Of course this can also be done by getter or setter methods.
 +
 +The third property "FieldInt" allows reading and writing operations directly on the protected field "FFieldInt"
 +
 +Note: You can mix getter/setter methods and direct field access on read/write.
 +
 +<file pascal>
 + TTest = class
 + protected
 + FFieldBool : Boolean;
 + FFieldStr : String;
 + FFieldInt : Int32;
 +
 + function GetFieldBool() : Boolean;
 + procedure SetFieldBool(AValue : Boolean);
 +
 + public
 + FFieldDbl : Double;
 + FFieldObj : TTest;
 +
 + property FieldBool : Boolean read GetFieldBool write SetFieldBool;
 + property FieldStr : String read FFieldStr;
 + property FieldInt : Int32 read FFieldInt write FFieldInt;
 + end;
 +</file>
 +
 +=== Methods ===
 +
 +Functions and procedures declared in a class, interface or record are called methods.
 +Those can be defined in all available viewspaces (private, protected, public, published) like fields.
 +
 +But besides a simple object method declaration, Delphi offers constructors, destructors
 +and class functions / class procedures. They are declared by a different reserved word, because their
 +behaviour is a bit different than the simple methods.
 +
 +Constructors are getting automatically called on instance creation and destructors when an instance is about to be destroyed.
 +
 +Class functions / procedures OR static methods are like simple functions and procedures, but they are called in different context.
 +Regular functions/procedures are called by an instance (object) of the class, while static methods are called by the class itself.
 +So they have no instance context and therefore no access to fields or object methods.
 +
 +This is getting important, when using "Self" in those methods. For static methods it is the class type itself, but for object methods it's the object.
 +
 +<file pascal>
 + TTest = class
 + protected
 + [...]
 + function Calculate(AFactor : Double) : Double; virtual;
 +
 + public
 + [...]
 + constructor Create(); virtual;
 + destructor Destroy(); override;
 +
 + class function GetDefaultStr() : String;
 +
 + procedure Output(AIdx : Integer);
 + end;
 +</file>
 +
 +After our method head declaration is done, we need to implement the function.
 +
 +<file pascal>
 +/// <summary>
 +/// Test Scripting Unit
 +/// </summary>
 +unit app.test;
 +
 +interface
 +
 +const 
 + DEFAULT_STR : String = 'Test' + '-Output-' + 'Test:';
 +type
 + TTest = class
 + [...]
 + end;
 +
 +implementation
 +
 +{ TTest }
 +
 +constructor TTest.Create();
 +begin
 + inherited Create();
 +
 + // use property test
 + Self.FieldBool := true;
 +
 + Self.FFieldStr := 'Hello World';
 + Self.FFieldInt := 124;
 + Self.FFieldDbl := 1.25;
 + Self.FFieldObj := Self;
 +end;
 +
 +destructor TTest.Destroy();
 +begin
 + inherited Destroy();
 +end;
 +
 +function TTest.GetFieldBool() : Boolean;
 +begin
 + Result := Self.FFieldBool;
 + System.WriteLn('TTest.GetFieldBool() called');
 +end;
 +
 +procedure TTest.SetFieldBool(AValue : Boolean);
 +begin
 + Self.FFieldBool := AValue;
 + System.WriteLn('TTest.SetFieldBool() called');
 +end;
 +
 +class function TTest.GetDefaultStr() : String;
 +begin
 + Result := DEFAULT_STR;
 +end;
 +
 +procedure TTest.Output(AIdx : Integer);
 +var LTmpStr : String;
 +    LSeed : Single;
 +begin
 +    LSeed := System.RandomFloat();
 + LTmpStr := TTest.GetDefaultStr() +
 + 'Object{' +
 + '"fieldStr":"' + Self.FFieldStr +
 + '", "fieldInt":"' + Self.FFieldInt.ToString() +
 + '", "fieldDbl":"' + Self.FFieldDbl.ToString() +
 + '", "fieldBool":"' + Self.FieldBool.ToString() +
 + '", "fieldObj":"' + Self.FFieldObj.ToJSON() +
 + '"}';
 + LTmpStr := AIdx.ToString() + #9' [' + LSeed.ToString() + ']'#9#9 + LTmpStr + 
 + ' => ' + Self.Calculate(AIdx * 0.5).ToString();
 + System.WriteLn(LTmpStr);
 +end;
 +
 +function TTest.Calculate(AFactor : Double) : Double;
 +begin
 + Result := Self.FFieldInt.ToDouble() + Self.FFieldDbl * AFactor;
 +end;
 +
 +end.
 +</file>
 ==== Native ==== ==== Native ====