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
0.8.3:scripting [2021/10/13 13:35] – [Scripting] admin0.8.3:scripting [2021/10/13 13:42] (current) – [Global Functions] admin
Line 491: Line 491:
 </file> </file>
  
 +===== Global Constants =====
 +
 +<file pascal>
 +const 
 + DEFAULT_STR : String = 'Test' + '-Output-' + 'Test:';
 +</file>
 ===== Local Variables ===== ===== Local Variables =====
 Local variables are declared like Delphi syntax by the "var" identifier. Local variables are declared like Delphi syntax by the "var" identifier.
Line 593: Line 599:
 Those can be defined in all available viewspaces (private, protected, public, published) like fields. Those can be defined in all available viewspaces (private, protected, public, published) like fields.
  
-But besides the regular function and procedure declaration, Delphi offers constructors, destructors+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 and class functions / class procedures. They are declared by a different reserved word, because their
 behaviour is a bit different than the simple methods. behaviour is a bit different than the simple methods.
Line 602: Line 608:
 Regular functions/procedures are called by an instance (object) of the class, while static methods are called by the class itself. 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. 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> <file pascal>
Line 618: Line 626:
  procedure Output(AIdx : Integer);  procedure Output(AIdx : Integer);
  end;  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> </file>
 ==== Native ==== ==== Native ====