Need help understanding how to initalize a particular example

Hi everyone,

For everyone who keeps telling me I lack basic knowledge in Pascal, I will repeat as many times as necessary that the tutorials you give me don’t explain the concepts very well in layman terms, like for example in this example:

{$mode objfpc}{$H+}{$J-}
uses SysUtils;

type
  TFruit = class
    procedure Eat;
  end;

  TApple = class(TFruit)
    procedure Eat;
  end;

procedure TFruit.Eat;
begin
  Writeln('Eating a fruit');
end;

procedure TApple.Eat;
begin
  Writeln('Eating an apple');
end;

procedure DoSomethingWithAFruit(const Fruit: TFruit);
begin
  Writeln('We have a fruit with class ', Fruit.ClassName);
  Writeln('We eat it:');
  Fruit.Eat;
end;

var
  Apple: TApple; // Note: you could as well declare "Apple: TFruit" here
begin
  Apple := TApple.Create;
  try
    DoSomethingWithAFruit(Apple);
  finally FreeAndNil(Apple) end;
end.

It is never explained that “Fruit.Eat” is called under DoSomething because you pass an instance of a class to a function, and not the class itself (TFruit).

None of the other tutorials given explain/clarify that detail either; can someone show me a series of tutorials that actually does rather than just assumes I know the details already? Do the people who give me these kind of tutorials not understand this is exactly my point?

Anyway, my issue in question is with my code, which I am told lacks initalization of ThirdPersonNavigation, but I am not clear as to how exactly I should initalize it, like whether I should give it a var assignment or string assignment, with the string being the name of the ThirdPersonNavigation in the main Viewport.

{
  Copyright 2023-2023 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ Main view, where most of the application logic takes place. }
unit GameViewMain;

interface

uses Classes,
  CastleVectors, CastleComponentSerialize,
  CastleUIControls, CastleControls, CastleKeysMouse,
  CastleThirdPersonNavigation, CastleViewport, CastleScene,
  CastleCameras, CastleTransform, CastleInputs,
  CastleSceneCore, CastleDebugTransform;

type
  TMyThirdPersonNavigation = class(TCastleThirdPersonNavigation)
  protected
  procedure AssignAvatar;
  published
    SceneHumanLegs1, SceneHumanBase: TCastleScene;
    HumanBase1: TCastleTransform;
  end;

  { Main view, where most of the application logic takes place. }
  TViewMain = class(TCastleView)
  published
    { Components designed using CGE editor.
      These fields will be automatically initialized at Start. }
    LabelFps: TCastleLabel;
  public
    constructor Create(AOwner: TComponent); override;
    procedure Start; override;
    procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
    function Press(const Event: TInputPressRelease): Boolean; override;
  end;

var
  ViewMain: TViewMain;
  ThirdPersonNavigation: TMyThirdPersonNavigation;

implementation

uses SysUtils;

procedure TMyThirdPersonNavigation.AssignAvatar;
begin
  ThirdPersonNavigation.AvatarHierarchy := HumanBase1;
end;

{ TViewMain ----------------------------------------------------------------- }

constructor TViewMain.Create(AOwner: TComponent);
begin
  inherited;
  DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
end;

procedure TViewMain.Start;
begin
  ThirdPersonNavigation.AssignAvatar;
  inherited;
end;

procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
begin
  inherited;
  { This virtual method is executed every frame (many times per second). }
  Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code');
  LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
end;

function TViewMain.Press(const Event: TInputPressRelease): Boolean;
begin
  Result := inherited;
  if Result then Exit; // allow the ancestor to handle keys

  { This virtual method is executed when user presses
    a key, a mouse button, or touches a touch-screen.

    Note that each UI control has also events like OnPress and OnClick.
    These events can be used to handle the "press", if it should do something
    specific when used in that UI control.
    The TViewMain.Press method should be used to handle keys
    not handled in children controls.
  }

  // Use this to handle keys:
  {
  if Event.IsKey(keyXxx) then
  begin
    // DoSomething;
    Exit(true); // key was handled
  end;
  }
end;

end.

Cause what I do for help with classes, is that I look at the tutorials under the “classes” section specifically, like any good help seeker would logically expect, but it just says “we have inheritance and virtual methods” without explaning more in-depth what inheritance actually does, like the rules of it or nothing like that.

I know the people who give me the tutorials are too busy with their own life to walk me through every step in painstaking detail themselves, which is fine and understandable, but I would expect the learning resources to actually be beginner friendly, rather than just assuming I know what inheritance already means for example.

Also yes, I am not a complete idiot like some people seem to think, and I do in fact know conceptually/in theory that you have to use a procedure after you declare it, or that you need to initailize a variable before using it or it’s undefined otherwise, cause I know that’s how programming in general works,

It’s just the details/implementation is where I get stuck, like my question above shows.

And again, the tutorials you guys give me do not clarify the details, only make crass assumptions that I somehow know Pascal inheritance already, which is asinine nonsense.

Also, all the heck I ever wanted to do was make a third person multi-part character in an easy to understand manner. Why the heck should I get bogged down in all these crazy details of what is polymorphism vs virtual methods, when all I wanted was a simple, straightforward way to add a character in a drag and drop style manner?

The tutorials we point you to do explain these details.

Please follow resources from More Resources to Learn Pascal | Castle Game Engine , the first link there (" Delphi Object Pascal Handbook - Free Object Pascal Handbook by Marco Cantu.) explains how to create classes. Searching in that document (Ctrl + F) easily finds text from “In Object Pascal, to create an instance of an object, …” .

As for why we cannot do this task by “drag and drop” (without coding): We in general work on making more and more (common) things available without coding in CGE. Including new navigation components that are under review now (see news: Using Delphi “welcome page” to show your README.md, also docs updates, also coming soon: new navigation components and Delphi + Linux – Castle Game Engine ). But it’s a never-ending work, there will always be complicated (or specialized to your project – that is the case with your multi-part avatar from MD3 in other thread) tasks that will require some programming (be it in Pascal or C# or UE blueprints :slight_smile: ).

1 Like

Okay, thank you for explaining to me that some of the tutorials do explain all the steps to me, so that way I can actually learn how to do it myself, which is I know your goal/objective for me, cause I won’t actually know how to do it otherwise, and I get that 100 percent.

Is it just that tutorial, or are there others too that explain the basics to me that you showed me?

Unfortunately, the site to get it from tells me it should appear in my email shortly, but it has never came up yet. I would assume just being patient and waiting some more will fix the issue though?

I got an email response with the free download of that book (from Free Object Pascal Handbook by Marco Cantu - Embarcadero ) in less than a minute today. It is automated, I guess. You should receive an email quickly. If you don’t check your spam folder. You can also fill the form again and double-check you gave a correct email address.

This is really weird because I did in fact double check I sent the correct email, and re-sent it but nothing came up in my spam or email within a minute, again. Do you believe that there might be a PDF version online I can find? I don’t think that’s illegal technically cause it’s free already.

I made sure to have the foresight to look it up myself, but unfortunately I only got a table of contents, and all the others either were the site with the nonworking link on my end, or other sites that no longer had the book open for download.

sigh

Okay, I learned from other tutorials like tutorialspoint.org that you can initalize variables as soon as you declare them, so I tried passing a string of “ThirdPersonNavigation” and ‘ThirdPersonNavigation’ and seeing which one works, but unfortunately learned that typed constants of interfaces and classes cannot be passed as vars in initalization.

If I cannot pass the name directly, do you know what exactly I should initalize the TCastleThirdPersonNavigation actually with? It doesn’t make sense not to reference the ThirdPersonNavigation object directly.

I get this is your point that if I read the documents I won’t run into these kinds of errors, but I don’t know what else I can do except learn by trial and error if the documents either assume I know the ins and outs of Pascal already, or are just plain old unavailable when they shouldn’t be.

Also on top of this, it doesn’t make much intuitive sense because beneath that is an initaliziation of a HumanBase1, except it is done with passing a class directly and it dosn’t throw an error even though mine is rejected for no good reason.

There are a lot of Pascal resources, also for beginners, linked on More Resources to Learn Pascal | Castle Game Engine . Please do follow them and learn Pascal. I have linked these resources to you many times by now, also in the previous thread.

You lack basic knowledge of Pascal (or even general OOP vocabulary from any programming language). Without this knowledge you will not solve your task of multi-part avatar. Your statements about Pascal are full of confusion. We do not have capacity to teach you Pascal from basics on this forum – you need to make an effort and digest the resources we point you to.

I do not know why you didn’t get an email from Free Object Pascal Handbook by Marco Cantu - Embarcadero . It worked for me 2 times today, almost immediately I got a response with link to PDF, with 2 different emails on 2 different domains. Try again, make sure you provide proper email, make sure you check the spam folder, try different email if you have one.

Yeah, and I get you don’t have the time but again, some of the resources assume you have a larger level of expertise than a beginner would usually have as I pointed out several times, and I would greatly prefer being referred to one that actually does spell out everything for me precisely so that you don’t end up having to.

Do maybe the other options you referred me to spell out every detail for me? If that is the case there is probably no need to ask about basic details anymore.

The Swineburne University playlist turned me off initially because it’s a YouTube playlist rather than something I can skim for the appropriate information, but I suppose if I sit through it it will spell out all the basics for me as well, like the rules for typing out things in Pascal overall, or how vars work in general compared to other languages?

Cause I think your point is that I am making assumptions about how var works from other languages, although in Pascal it’s different and I need to learn everything from the ground up?

So I read the tutorials, right, and I can understand how constants are not allowed in general, and I think from what I can tell reading more in-depth that a class is a pointer to an object, and I know a pointer is a reference to a piece of memory.

If I can’t reference TthirdPersonNavigation on account of it being implied a constant, that’s fine and I get that, but do you know what I should do instead in order to assign the ThirdPersonNavigation avatar to a HumanBase1?

Again, the examples that come with the engine are of no use here, and I am not sure changing the class to an actual object will actually work.

That’s a statement full of confusion. The problem is not about ThirdPersonNavigation being a constant, it’s not. As I wrote in MD3 - improve animations support - #241 by michalis , there are other problems (you didn’t initialize ThirdPersonNavigation so it is nil, moreover it doesn’t make sense to have ThirdPersonNavigation field inside TMyThirdPersonNavigation at all).

Please study the learning materials in More Resources to Learn Pascal | Castle Game Engine more.

I have even pointed 2 times in this thread even the exact paragraph in the book “Free Object Pascal Handbook by Marco Cantu” that shows how to initialize the object. Please look into it.

And please leave for some time the ThirdPersonNavigation issue. To get back to it, you must have better understanding how Pascal, and OOP languages in general, work.

Make simple exercise programs, e.g. create a class that represents a “car”, class that represents a “human”, add fields into car like “this is human that is driving”, “this is human in passenger’s seat”. Then do proper initialization and adjust them, e.g. add a method on “car” to “increase the age of both humans in the car by 1 year”. Write this program, run it, confirm it doesn’t crash and it changes the age of 2 humans.

That particular book still doesn’t download for me unfortunately, so I had to use other resources on that exact page, which seemed to imply that (in particular, the link Pascal Language Reference).

All the other ones, like the Swineburn University one and especially the Modern Programming Introduction for Beginners, definitely seem to want you to have basic knowledge first instead of going over the basics for you, which again is not okay for absolute beginners.

However, the first one, the book you reference, I know still doesn’t download on my computer (which is annoying as ever!) even after I retry it literally a thousand times, so maybe could you please give me an alternative link or a PDF version? I’m dead serious that it doesn’t download.

If you are really serious that it downloaded fine on your computer and therefore the problem is my computer, is there any other alternatives? For serious, here is what the page on Classes tells me in the Free Pascal tutorials on the Pascal and Lazrus Wiki, which is what caused my confusion:

Link to Class being called a Pointer to an Object; I know for a fact pointers usually store constant amounts of data cause it said so word for word on the section in pointers on the same wiki!