Recent

Author Topic: HowTo-tricks: Edit object State (TForm, TUserObject) with external procedure  (Read 2206 times)

Researching

  • Full Member
  • ***
  • Posts: 121
HowTo (Programming tricks )
Given: exists a class with certain properties and methods
For example: FormMain = class (TForm)
There is a button, which makes a call to external procedure.

External procedure must:
0. Detect the call origin (? Sender) // not sure this is right
1. Edit properties of FormMain (get it's interface: properties and methods),
    if propertyExists('Color'), then begin setColor ('blue'); end
2. Call method of FormMain
    if MethodExists('minimize'), then begin callMethod('minimize'); end;
3. if this is possible by any chance: add / remove properties, methods from CallerClass.

Two cases: procedure i located in
a. uProcedures.pas.
      -> uMainForm>uses uProcedures;
b. ObjectEditorClass, located in uObjectEditor
       ->uMainForm>uses uObjectEditor;
     
Edit class state with procedure, Edit TForm with external procedure.

furious programming

  • Hero Member
  • *****
  • Posts: 853
External procedure must:
0. Detect the call origin (? Sender) // not sure this is right

You can call the external procedure and pass the form object via parameter. No problem.

Quote
1. Edit properties of FormMain (get it's interface: properties and methods),
    if propertyExists('Color'), then begin setColor ('blue'); end

You can use RTTI functions to check if the passed object has published Color property and modify it, if it exists. Forms have this property, so testing is not needed. It is different if the target method parameter is of the general type (eg TObject).

Quote
2. Call method of FormMain
    if MethodExists('minimize'), then begin callMethod('minimize'); end;

Same as above, but target method must be published if you want to use RTTI.

Quote
3. if this is possible by any chance: add / remove properties, methods from CallerClass.

Don't understand this. It is no way to remove properties from classes. You can remove the content of property, but not whole property.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Researching

  • Full Member
  • ***
  • Posts: 121
Furious Programming, grateful for your attention and reply!
Please, reference source or code examples... Like:
We have
Requesting side
Code: Pascal  [Select][+][-]
  1.  unit MainUnit;
  2.  
  3. type
  4.   TProcessedObject = class (TObject)
  5.     public
  6.       ThisVarToModyfy : string;
  7.       ThisMethodToModyfy(a,b,c:longint; s,t,u:string):string; virtual{?};
  8.     end;
And the referenced side
Code: Pascal  [Select][+][-]
  1. unit LibraryUnit;
  2. {HERE = here name of sender, and some ?pointers to properties and/or methods }
  3. procedure ObjectModyfierProcedure ({HERE});
  4. begin
  5. <... the way to modyfy...>
  6. //TODO: Exactly HOW TO reference that object for modification?
  7. end;
for example:| how to add new component on form?
Real Life task:  conditionally add blocks of elements on the form as GroupBox>....
« Last Edit: November 15, 2018, 11:15:27 am by Researching »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Reference Site

Maybe something like this:

Code: Pascal  [Select][+][-]
  1. unit LibraryUnit;
  2.  
  3. uses
  4.   MainUnit;
  5.  
  6. procedure ObjectModyfierProcedure(AnObject: TProcessedObject);
  7. begin
  8.   AnObject.ThisVarToModyfy := 'something';
  9. end;

Requesting Site

Requesting side
Code: Pascal  [Select][+][-]
  1.  unit MainUnit;
  2.  
  3. type
  4.   TProcessedObject = class (TObject)
  5.     public
  6.       ThisVarToModyfy : string;
  7.       ThisMethodToModyfy(a,b,c:longint; s,t,u:string):string; virtual{?};
  8.     end;

Line #7 on the code above will cause compile time error. It is not valid in pascal.

Adding New Component On Form

for example:| how to add new component on form?

Did you mean adding new component on the from on runtime?
Try this:
http://forum.lazarus.freepascal.org/index.php?topic=37530.0
« Last Edit: November 15, 2018, 01:31:14 pm by Handoko »

Researching

  • Full Member
  • ***
  • Posts: 121
Grateful! Hadnoko! Not yet have a try... But seems hopeful.
Is it possible to code this (?) -->>
Code: Pascal  [Select][+][-]
  1. procedure ObjectModyfierProcedure(AnObject: TObject, var S:string); {?}
  2. {instead of }
  3. // procedure ObjectModyfierProcedure(AnObject: TProcessedObject);

Why compile time error?
Quote
Line #7 on the code above will cause compile time error. It is not valid in pascal.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Yes, you can write like this:

Code: Pascal  [Select][+][-]
  1. procedure ObjectModyfierProcedure(AnObject: TProcessedObject; const S: string);
  2. begin
  3.   AnObject.ThisVarToModyfy := S;
  4. end;

And my guess, the line #7 should be like this:

Code: Pascal  [Select][+][-]
  1.     function ThisMethodToModyfy(a,b,c:longint; const s,t,u:string): string; virtual;

 

TinyPortal © 2005-2018