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.