Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
0.8.3:scripting [2021/10/13 13:24] – [Scripting] admin | 0.8.3:scripting [2021/10/13 13:42] (current) – [Global Functions] admin | ||
---|---|---|---|
Line 491: | Line 491: | ||
</ | </ | ||
+ | ===== Global Constants ===== | ||
+ | |||
+ | <file pascal> | ||
+ | const | ||
+ | DEFAULT_STR : String = ' | ||
+ | </ | ||
===== Local Variables ===== | ===== Local Variables ===== | ||
Local variables are declared like Delphi syntax by the " | Local variables are declared like Delphi syntax by the " | ||
Line 561: | Line 567: | ||
In the public property " | In the public property " | ||
indirectly. We than have the option to influence behaviour. | indirectly. We than have the option to influence behaviour. | ||
+ | |||
+ | The second property " | ||
+ | |||
+ | The third property " | ||
+ | |||
+ | Note: You can mix getter/ | ||
<file pascal> | <file pascal> | ||
Line 577: | Line 589: | ||
property FieldBool : Boolean read GetFieldBool write SetFieldBool; | property FieldBool : Boolean read GetFieldBool write SetFieldBool; | ||
+ | property FieldStr : String read FFieldStr; | ||
+ | property FieldInt : Int32 read FFieldInt write FFieldInt; | ||
end; | end; | ||
</ | </ | ||
=== Methods === | === 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, | ||
+ | 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/ | ||
+ | So they have no instance context and therefore no access to fields or object methods. | ||
+ | |||
+ | This is getting important, when using " | ||
+ | |||
+ | <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; | ||
+ | </ | ||
+ | |||
+ | After our method head declaration is done, we need to implement the function. | ||
+ | |||
+ | <file pascal> | ||
+ | /// < | ||
+ | /// Test Scripting Unit | ||
+ | /// </ | ||
+ | unit app.test; | ||
+ | |||
+ | interface | ||
+ | |||
+ | const | ||
+ | DEFAULT_STR : String = ' | ||
+ | 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(' | ||
+ | end; | ||
+ | |||
+ | procedure TTest.SetFieldBool(AValue : Boolean); | ||
+ | begin | ||
+ | Self.FFieldBool := AValue; | ||
+ | System.WriteLn(' | ||
+ | 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() + | ||
+ | ' | ||
+ | '" | ||
+ | '", | ||
+ | '", | ||
+ | '", | ||
+ | '", | ||
+ | '" | ||
+ | LTmpStr := AIdx.ToString() + #9' [' + LSeed.ToString() + ' | ||
+ | ' => ' + 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. | ||
+ | </ | ||
==== Native ==== | ==== Native ==== | ||