Controlling Multiple Objects lazarus / 31-LCL ---------------------------------------------------------------------- S4MD!ng · 2019-02-10 00:15 Hi Guys I've only just learnt how to implement classes in my projects but there si 2 things that i need to do but havent figured out how to 1) Be able to destroy the shape when Life = 0 in the code below: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure Shape.LifeReduction();begin  Life        :=Life-1;  if Life      = 0 then  begin    Shape.Destroy;  end;end;(This gives me the error [Identifier idents no member "Destroy"]) 2) Is there a way to be able call a certain procedure for every object of a certain type?     like, for example in this code, move every object in the Shape Class 10 units up? Here is a copy of the entire unit; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   Shape = class(TShape) //Inherits all the procedures, functions and variables of the class TShape    Private      X1, Y1:Integer;   //Current Position      Gradient1:Real;   //Gradient Number of the straight nine between it and the target point      Life:Integer;     //Life Total (Degrades and will delete the shape when reaches 0)    Public      Constructor Create(AOwner: TComponent); Override;      //Overrides the Base classes Constructor maybe?      Procedure Gradient(X2, Y2:integer);   //Sets Gradient      Procedure LifeReduction();          //Controls Life Points  end;   { TForm1 }   TForm1 = class(TForm)  Button1: TButton;    Player: TShape;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } constructor Shape.Create(AOwner: TComponent);    //Don't know what (AOwner:TComponent) does???begin  inherited;                                   //Don't know what this does either  Self.Parent := TForm1(AOwner);  X1          :=Random(320);  Y1          :=Random(240);  Self.top    :=Y1;  Self.Left   :=X1;  Self.Height :=16;  Self.Width  :=16;  Life        :=10end; Procedure Shape.Gradient(X2, Y2:integer);       //Obvious in its purposebegin  Gradient1   :=(Y2-Self.Y1)/(X2-Self.X1);end;Procedure Shape.LifeReduction();begin  Life        :=Life-1;  if Life      = 0 then  begin    Shape.Destroy;                              //Identifier Idents No Member "Destroy"  end;end; procedure TForm1.FormCreate(Sender: TObject);var  s1: Shape;begin  s1          := Shape.Create(Self);  s1.Gradient(Player.Left, Player.Top);end; procedure TForm1.Button1Click(Sender: TObject);  //Lets Me Create LOTS Of Shapes on the click of a buttonvar  s1: Shape;begin  s1          := Shape.Create(Self);            //Self Refers to TForm1 (Creates the Shape)  s1.Gradient(Player.Left, Player.Top);         //Runs the Procedure Gradient Target Objects Coordinatesend; end. ---------------------------------------------------------------------- lucamar · 2019-02-10 01:17 From specific to general: 1) Your Shape class doesn't have a Destroy method, hence the message. Add: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { in the interface }Shape = class(shape);  { rest of declarations }public  destructor Destroy; override;end; { in the implementation }destructor Shape.Destroy;begin  inherited Destroy;end; 2) It is rather unusual for an object to destroy itself but if you insist on doing so, don't over-qualify the call (and this is valid for all method calls from inside their class); simply do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure Shape.LifeReduction();begin  Life        :=Life-1;  if Life      = 0 then  begin    Destroy;    {or at most: Self.Destroy}  end;end; 3) That classs name "Shape" is ... a bad one. The usual convention for types is to start with a "T" and to have a name somehow related to its inheritance and its purpose. In your case, then, something like p.e.     TPlayerShape = class(TShape); 4) This is nitpicking but method names should be more pro-active, so instead of LifeReduction() use something like ReduceLife() 5) More nitpicking: Make Gradient a property with getter/setter methods and make it a TPoint 6) While we are at it, conventions dictate that private and protected fields are named starting with an "F", so: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       FX1, FY1:Integer;   //Current Position;      { I would use a TPoint for that: FCurrPos: TPoint }      FGradient:Real;   //Gradient Number of the straight nine between it and the target point      FLife:Integer;  And I'll stop here; too much already, isn't?  HTH ---------------------------------------------------------------------- S4MD!ng · 2019-02-10 01:45 Thanks for the help, I've gone through my code and made all the changes that you suggested about the general form of the code. Would there be a way for the program to make it so that everytime i press TButton1, it calls the procedure ReduceLife() for every object in the class, so that after 10 button presses, it will just delete the oldest object in the class, and create a new one? ---------------------------------------------------------------------- S4MD!ng · 2019-02-10 02:10 Also, I tried using the gradient as a property as you suggested, however, this is the first time that i have used the properties and it gives me the error "unit1.pas(25,65) Error: Illegal symbol for property access" Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TArrow = class(TShape) //Inherits all the procedures, functions and variables of the class TShape    Private      FX1, FY1:Integer;   //Current Position ---Addendum 1 Use TPoint instead (No idea how to do this)    FGradient1:Real;   //Gradient Number of the straight nine between it and the target point      FLife:Integer;     //Life Total (Degrades and will delete the shape when reaches 0)    Public      Constructor Create(AOwner: TComponent); Override;      Destructor Destroy; Override;      Procedure SetGradient(X2, Y2:integer);   //Sets Gradient      Function GetGradient:Real;      Procedure ReduceLife();          //Controls Life Points      property Gradient: Real read GetGradient write SetGradient;  end;How do I make this work properly? ---------------------------------------------------------------------- jamie · 2019-02-10 02:23 you need to create the GetGradient and SetGradient code.. The GetGradient is the Function that returns the REAL SetGradient is the Procedure that accepts a REAL these are methods in your class, you need to specify the header in the Private section and create the Body in the Implementation section of the UNIT.  also, it is customary to place a "f" at the start of the names for these.  You can not call standard functions / procedures from a property like that however, you can use a proxy to do it and that would be the methods of the class I spoke of. ---------------------------------------------------------------------- S4MD!ng · 2019-02-10 03:09 I moved FSetGradient and FGetGradient to the private section. and implemented both of them, but i still get the same error - "unit1.pas(25,67) Error: Illegal symbol for property access", but this only occurs for the write part, not the read. whats the problem here. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type        //Conventions Dictate that we private and protected fields are names starting with an "F"   { TArrow }   TArrow = class(TShape) //Inherits all the procedures, functions and variables of the class TShape    Private      FX1, FY1:Integer;   //Current Position ---Addendum 1 Use TPoint instead      FGradient1:Real;   //Gradient Number of the straight nine between it and the target point      FLife:Integer;     //Life Total (Degrades and will delete the shape when reaches 0)      Procedure FSetGradient(X2, Y2:integer);   //Sets Gradient      Function FGetGradient:Real;    Public      Constructor Create(AOwner: TComponent); Override;      Destructor Destroy; Override;      Procedure ReduceLife();            //Controls Life Points      property Gradient: Real read FGetGradient write FSetGradient;  end; {Then in the implementation} procedure TArrow.FSetGradient(X2, Y2:Integer);begin   FGradient1:=(Y2-FY1)/(X2-FX1);end;function TArrow.FGetGradient: Real;       //Obvious in its purposebegin   GetGradient:=FGradient1end;  Also, its probably a bit late for this but what is the point of using a property in the class, is there something I can do with it that I cant do with the functions alone or something? ---------------------------------------------------------------------- Handoko · 2019-02-10 03:14 Setter only takes 1 parameter: https://stackoverflow.com/questions/8823609/how-to-give-setter-a-2nd- parameter-in-delphi ---------------------------------------------------------------------- S4MD!ng · 2019-02-10 03:31 That Seems to have done the trick, Thanks ---------------------------------------------------------------------- lucamar · 2019-02-10 10:29 Quote from: S4MD!ng on February 10, 2019, 02:45:46 amWould there be a way for the program to make it so that everytime i press TButton1, it calls the procedure ReduceLife() for every object in the class, so that after 10 button presses, it will just delete the oldest object in the class, and create a new one? Depends on how you're creating the objects, but assuming it's by calling TArrow.Create(Self) with Self being the form it may be something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;begin  for i := 0 to ComponentCount - 1 do begin    if Components[i].InheritsFrom(TArrow) then      (Components[i] as TArrow).ReduceLife;  end;end; One caveat: there is already a TArrow class in the LCL! It may not matter much if you never use it but it would be best if you find another name for your class Re. making Gradient a property, my sugestion was that you make it a TPoint precisely to avoid this situation but I see now that it would complicate things a little. If you still need to set the gradient from two integers then add a method SetGradientTo(X, Y: integer), like you had before. ---------------------------------------------------------------------- Thaddy · 2019-02-10 12:42 Apart from the TArrow issue, I also would not use the real type.. That's been deprecated a long time ago. Use single or double. real is already aliased to double, btw. For legacy support we have real48. Real is historically a 48 bit type. That support is really only for legacy code that relies on a 48 bit width. ---------------------------------------------------------------------- S4MD!ng · 2019-02-10 17:12 Well, I tried to implement the code to reduce all of the objects life at once, (It produced an error after the 10th, but I know why it did that.) as such I've put each object in an array with 11 places, and am shifting the array 1 place to the left  after each button press (which also creates a new object, adds it to the array etc...) Im trying to call TDart.ReduceLife (I changed TArrow to TDart) for every object using Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure QueueShift(Queue: Array of TDart);var  tmp : TDart;  i,j : Integer;begin  for j := 0 to Length(Queue) do         {This Part Here}    begin       (Queue[j]).ReduceLife();    end;   tmp := Queue[0];  for i := 1 to Length(Queue) do    begin      Queue[i-1] := Queue[i];      Queue[Length(Queue)] := tmp;    end;end; However, it doesn't seem to be deleting the objects and also, when I press Button 1, it gives me the error "raised exception class 'External SIGSEGV' I the line Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FLife := FLife-1" How Would I fix this, and would the code do as intended, and shift each object 10 places, calling Reduce Life 10 times, and hence deleting the object. Here is an updated version of the Code if that helps... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type        //Conventions Dictate that we private and protected fields are names starting with an "F"   { TArrow }   TDart = class(TShape) //Inherits all the procedures, functions and variables of the class TShape    Private      FX1, FY1:Integer;   //Current Position ---Addendum 1 Use TPoint instead      FGradient1:Real;   //Gradient Number of the straight nine between it and the target point      FLife:Integer;     //Life Total (Degrades and will delete the shape when reaches 0)      Procedure FSetGradient(M:Real);   //Sets Gradient  ---Addendum 2 The set function requires only 1 parameter.      Function FGetGradient:Real;    Public      Constructor Create(AOwner: TComponent); Override;      Destructor Destroy; Override;      Procedure ReduceLife();            //Controls Life Points      property Gradient: Real read FGetGradient write FSetGradient;  end;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Player: TShape;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1;  TDartQueue:array[0..15] of TDart;implementation {$R *.lfm} { TForm1 }   constructor TDart.Create(AOwner: TComponent);    //Don't know what (AOwner:TComponent) does???begin  inherited;                                   //Don't know what this does either  Self.Parent := TForm1(AOwner);  FX1         :=Random(320);  FY1         :=Random(240);  Self.top    :=FY1;  Self.Left   :=FX1;  Self.Height :=16;  Self.Width  :=16;  FLife        :=10end; destructor TDart.Destroy;begin  inherited Destroy;end;procedure TDart.FSetGradient(M:Real);begin   FGradient1:=M;end;function TDart.FGetGradient: Real;       //Obvious in its purposebegin   FGetGradient:=FGradient1end;procedure TDart.ReduceLife();begin  FLife        := FLife-1;  if FLife      <= 0 then  begin    Destroy;                            //Identifier Idents No Member "Destroy"  end;end; procedure TForm1.FormCreate(Sender: TObject);var  s1: TDart;begin  s1          := TDart.Create(Self);  s1.FSetGradient((Play er.Top-s1.FY1)/(Player.Left-s1.FX1));end;procedure QueueShift(Queue: Array of TDart);var  tmp : TDart;  i,j : Integer;begin  for j := 0 to Length(Queue) do    begin       (Queue[j]).ReduceLife();    end;   tmp := Queue[0];  for i := 1 to Length(Queue) do    begin      Queue[i-1] := Queue[i];      Queue[Length(Queue)] := tmp;    end;end;   procedure TForm1.Button1Click(Sender: TObject);  //Lets Me Create LOTS Of Shapes on the click of a buttonvar  s1: TDart;begin  s1:= TDart.Create(Self);            //Self Refers to TForm1 (Creates the Shape)  s1.FSetGradient((Player.Top-s1.FY1)/(Player.Left-s1.FX1));         //Runs the Procedure Gradient Target Objects Coordinates  TDartQueue[15]:=s1;  QueueShift(TDartQueue);end; end. ---------------------------------------------------------------------- lucamar · 2019-02-10 18:37 I'm not very sure about exactly what you're trying to do, but try this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure QueueShift(Queue: Array of TDart);var  tmp : TDart;  i,j : Integer;begin  {Decrease life for each Dart object}  for j := 0 to High(Queue) do         {This Part Here}    Queue[j].ReduceLife();   { This moves each Dart object one place down except      the first, which is moved to the top of the queue}  tmp := Queue[0];  for i := 1 to High(Queue) do    Queue[i-1] := Queue[i];  Queue[High(Queue)] := tmp;end; I'm not satisfied with that, though, because as soon as any TDart object reaches FLife = 0 it gets destroyed, so any subsequent try to call to ReduceLife() will give you that SISSEGV exception. What would I do? Change TDart to this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TDart = class(TShape) //Inherits all the procedures, functions and variables of the class TShape    Private      FX1, FY1:Integer;   //Current Position ---Addendum 1 Use TPoint instead      FGradient1:Real;   //Gradient Number of the straight nine between it and the target point      FLife:Integer;     //Life Total (Degrades and will delete the shape when reaches 0)      Procedure SetGradient(M:Real);   //Sets Gradient  ---Addendum 2 The set function requires only 1 parameter.    Function GetGradient:Real;    Public      Constructor Create(AOwner: TComponent); Override;      Destructor Destroy; Override;      Procedure ReduceLife();            //Controls Life Points      property Life: Integer read FLife;      property Gradient: Real read FGetGradient write FSetGradient;  end; Then implement ReduceLife() like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDart.ReduceLife();begin  if FLife > 0 then    Dec(FLife);end; And QueueShift() would be like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.QueueShift(Queue: Array of TDart);var  tmp : TDart;  i,j : Integer;begin  for j := 0 to High(Queue) do begin    Queue[j].ReduceLife(); {Decrease life}    if Queue[j].Life = 0 then begin {Ended up dead?}      Queue[j].Free;                {free it up and ...}      {if it is any but the last, move all the others down}    if j < High(Queue) then        for i := j+1 to High(Queue) do        Queue[i-1] := Queue[i];      { ... and create a new one in the last position}      Queu[High(Queue)] := TDart.Create(Self)    end;  end;end; Note that doing it this way implies some more changes in other places and there are other places where things are not quite as they should. I don't have much more time now but I'll try later to do a clean-and- tidy-up of the rest of the code. But go ahead and try to do it yourself too, how else can you learn? ---------------------------------------------------------------------- User137 · 2019-02-10 19:31 If it's a visual where order doesn't matter (very common in games especially for particles and all moving objects), you can optimize it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.QueueShift(Queue: Array of TDart);var j : Integer;begin  for j := High(Queue) downto 0 do begin    Queue[j].ReduceLife(); {Decrease life}    if Queue[j].Life = 0 then begin {Ended up dead?}      Queue[j].Free;                {free it up and ...}      { ... and create a new one in the same index}      Queue[j] := TDart.Create(Self);       // Or if you want to just reduce the queue instead of making a new dart:      // Move last index to the free'd spot      // Queue[j]:=Queue[high(Queue)];      // And then shrink the array to discard the duplicate of last index      // setlength(Queue, high(Queue));      // Note: high(Queue) is same as length(Queue)-1    // Must go through array from last to first for this to work!    end;  end;end; ---------------------------------------------------------------------- S4MD!ng · 2019-02-10 23:32 In case this heps, ill give you some background information, basically, for my A Level coursework, im making a game, and an element of the game is a sort of doge the projectile minigame, where many Darts (TDart) move in specific patterns. The player, (which is a TImage that the player can controll) needs to avoid the darts. At the moment: I can create the darts I need to implement: Them moving around the screen in set patterns (for example moving up by 10 units) Detecting whether they have collided with the player (Which shouldn't be that hard, its only ALevel so it doesnt have to be optimised, so i can just do this in a function of the class, ) And once each of the projectiles have moved a set distance, they are destroyed (mayber they loose all their velocity or something {This is what the ReduceLife subroutine is meant to do, not reduce the life of the player, but of each projectile, so eventually they are destroyed.}) ---------------------------------------------------------------------- jamie · 2019-02-11 01:27 Then you'll need to get to know the TRect and its helper members to or use the lower level rect functions for intersect,   Iswithin etc  You'll also need to do a little trig and geometry because when you are trying to change direction of the shooter and the one getting shot also needs to be able to move to avoid the shooter, you need to take into account of G-forces and not allow them to be able to make sharp turns and instant takeoffs!  So setting the max g force per object and then using that as one of the parameters for the calculation to generate a S curve, bell curve etc.. ---------------------------------------------------------------------- S4MD!ng · 2019-02-11 01:57 OK, so i decided to go back to basics and try and make something that would do something visual, so that i know that it is doing something. So i changes ReduceLife() to also move the TDart up by 10. also, everytime that i press TButton1, it creates a new TDart, adds it to an array, and then calls, Reduce life for each item in the array. It works to some extent, when i press the button, it generates a Tshape, and moves it up by 10. but for every subsequent button press, it moves the fist object up by 10, but none of the others created by the same button that worked for the first one! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {Nothing has changes elsewhere}procedure TDart.ReduceLife();begin  if FLife > 0 then    Dec(FLife);      //decreases FLife    Self.Top:= Self.Top-10;end; procedure TForm1.Button1Click(Sender: TObject);  //Lets Me Create LOTS Of Shapes on the click of a buttonvar  s1: TDart;  j:integer;begin  s1:= TDart.Create(Self);            //Self Refers to TForm1 (Creates the Shape)  s1.FSetGradient((Player.Top-s1.FY1)/(Player.Left-s1.FX1));         //Runs the Procedure Gradient Target Objects Coordinates  TDartList[i] := s1;  for j := 0 to High(TDartList) do  begin    TDartList[j].ReduceLife;  end;  i:=i+1;  SetLength(TDartList, (High(TDartList)+1))end; begin  i := 0;  SetLength(TDartList, 1);end. ---------------------------------------------------------------------- jamie · 2019-02-11 22:19 The array is changing so the HIGH intrinsic function may not be work.  Try using Length(TDartList)-1 instead of High(TDartList)