Results 1 to 5 of 5

Thread: Overriding Twice

  1. #1

    Overriding Twice

    System: Windows Vista
    Compiler/IDE: Lazarus .9.28.2, FPC 2.2.4
    Libraries/API: Andorra 2D (Not quite relevant, please try to ignore the superfluous code that deals with Andorra if you can.)

    Well, I'm trying to flesh out a very flexible system at the moment for game development with Andorra, but to do so, I need to override some key procedures that are already overridden...

    Here is the current class, taken from a tutorial and modified very slightly.
    [pascal]
    TSpr = class(TImageSpriteEx)
    protected
    procedure DoMove(timegap:double);override;
    procedure DoDraw;override;
    public
    procedure onDraw;virtual;

    col:boolean;
    xspeed,yspeed: integer;
    end;
    [/pascal]
    The thing is, I have other units that create instances of TSpr, but I'd like to override DoDraw again, for example.

    Instantiating a TSpr:
    [pascal]
    spr := TSpr.Create(engine);
    with spr do
    begin
    Image := gui.Items[1];
    X := 0;
    Y := 0;
    CollisionTester := ColTest;
    end;
    [/pascal]
    What I would like to do would be something along the lines of
    [pascal]
    spr := TSpr.Create(engine);
    elGUI.add(spr);
    with spr do
    begin
    Image := gui.Items[1];
    X := 0;
    Y := 0;
    CollisionTester := ColTest;
    DoDraw := SecondOverride;
    end;

    procedure SecondOverride;
    begin
    Color := rgb(200,200,200);
    end;
    [/pascal]
    The idea is that there would be one main "object" class, where I can specify which functions handle collisions, drawing, and so on, while also having that main object track some variables such as the current x and y speeds.

    With my hacked up idea above, a few things:
    1. Is that even possible?
    2. If that code was even going to work, wouldn't I have to make a class of TSpr, and change it to procedure TSpr.SecondOverride; ?
    3. PS: Color is a property of TImageSpriteEx, and thus of TSpr, so the thing I'd hope is that if I was able to override this function again, it would realize that Color was a property of TSpr which is a property of TImageSpriteEx, so I'd be able to throw that variable in there and have it affect the image as expected.

    The other idea that I had, that I've now thrown out would be this:
    (Forward declaration of DoDraw of TSpr, how it currently is)
    [pascal]
    procedure TSpr.DoDraw;
    begin
    if not col then
    Color := rgb(200,200,200);
    inherited;
    Color := rgb(255,255,255);
    end;
    [/pascal]
    I was thinking (and tried) doing this:
    [pascal]
    procedure TSpr.DoDraw;
    begin
    onDraw;
    inherited;
    end;

    procedure TSpr.onDraw;
    begin
    if not col then
    Color := rgb(200,200,200);
    Color := rgb(255,255,255);
    end;
    [/pascal]
    But this resulted in the TSpr not being visible at all.

    Still learning! Thanks for your patience.

  2. #2

    Re: Overriding Twice

    onDraw approach is propably what you are after. I'll try show you the details.

    [quote=dazappa ]
    [pascal]type
    TDrawProcedure = procedure();
    // add "of object" at end if you want to define procedure in a class
    // and you propably do want.. so onDraw knows the sprite's variables

    TSpr = class(TImageSpriteEx)
    protected
    procedure DoMove(timegap:double); override;
    procedure DoDraw; override;
    public
    onDraw: TDrawProcedure; // It is a pointer to procedure

    col:boolean;
    xspeed,yspeed: integer;
    end;
    [/pascal]

    [pascal]
    spr := TSpr.Create(engine);
    elGUI.add(spr);
    with spr do
    begin
    Image := gui.Items[1];
    X := 0;
    Y := 0;
    CollisionTester := ColTest;
    onDraw := SecondOverride;
    end;

    procedure SecondOverride;
    begin
    Color := rgb(200,200,200);
    end;
    [/pascal]

    [pascal]
    procedure TSpr.DoDraw;
    begin
    if assigned(onDraw) then onDraw;
    inherited;
    end;
    [/pascal]

  3. #3

    Re: Overriding Twice

    Hi, yes I would like SecondOverride to know of the sprite's variables, so I defined it as procedure of object. However, I'm fairly sure it needs tweaking to make it work with procedure of object, as just changing it to a procedure of object gives a compilation error (which I was not able to solve with trusty Google): "Main.pas(134,26) Error: Incompatible types: got "DecondOverride;" expected "<procedure variable type of procedure of object;Register>"" Where the line contains the onDraw:=SecondOverride;

    The code you did provide so far is compilable, however. It's just not useful if I have no access to the variables .

    - Thanks for the help so far.

  4. #4

    Re: Overriding Twice

    Object procedures are defined in the class like mentioned.

    so Not this:
    [pascal]procedure SecondOverride;
    begin
    Color := rgb(200,200,200);
    end;[/pascal]

    but this works:
    [pascal]type
    TMySprite = class(TSpr)
    public
    procedure SecondOverride; // It's defined within class, not in unit functions
    end;

    procedure TMySprite.SecondOverride;
    begin
    Color := rgb(200,200,200);
    end;
    ...
    spr := TMySprite.Create(engine);
    with spr do
    begin
    onDraw := SecondOverride;
    end;
    [/pascal]

  5. #5

    Re: Overriding Twice

    Hm, I was in the middle of doing that too. (I wasn't sure if you could have a class of a class, but wanted to try because it made sense for this)

    When I do assign SecondOverride (through "spr.onDraw := TMySprite.secondOverride;"), it seg faults on that line where it sets the color variable... to narrow it down, I just used another variable, "Alpha := 255;" to make sure the rgb function wasn't the cause, but it seems that any variable of the parent's parent (TImageSpriteEx) causes the crash. I believe I can set the parent's variables just fine (like xspeed)

    Is that typical for this type of situation? I believe I could devise some sort of dirty workaround, but just wanted to double check.

    I believe this one is my own fault, although I'm not sure exactly why setting the Alpha/Color variables in certain areas causes the seg fault, but doesn't in others (If I set Alpha in the actual DoDraw event, and not a function called from it, it works, as well as setting the Alpha during Andorra's drawing procedure)... but this is probably more on the side of an Andorra tweak now.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •