Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Another Pointer Question - TObjectList

  1. #1

    Another Pointer Question - TObjectList

    Hi,

    Sorry for asking what may sound like stupid questions, but I am yet again stuck with bl**dy pointers...

    I have a TObjectList with a load of TCreatures in it.

    I have a pointer type to a creature which works fine:-

    type PCreature = ^TCreature;


    I have a PCreature variable that I want to use to control my 'player' creature.

    eg. Player: PCreature;

    What I need to do is get the pointer to the actual TCreature in the List:-

    Something like:-

    Player := @lstCreatures.Items[iX];

    But it says 'variable required'. What am I doing wrong here? Can I not get a pointer to it this way?

    Thanks for your patience =D
    http://www.c5software.co.uk (site is being developed at the moment)

  2. #2

    Another Pointer Question - TObjectList

    Hi Gadget,

    The problem is the following code:
    [pascal]
    Player := @lstCreatures.Items[iX];
    [/pascal]
    Change it to:
    [pascal]
    Player^ := lstCreatures.Items[iX];
    [/pascal]

    this should do the trick
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  3. #3

    Another Pointer Question - TObjectList

    Quote Originally Posted by M109uk
    Hi Gadget,

    The problem is the following code:
    [pascal]
    Player := @lstCreatures.Items[iX];
    [/pascal]
    Change it to:
    [pascal]
    Player^ := lstCreatures.Items[iX];
    [/pascal]

    this should do the trick
    Thanks, but I still have a problem =/

    If I do what you suggested it says:- Incompatible types 'TCreature and TObject'

    If I type cast it like this:-

    Player^ := TCreature(lstCreatures.Items[iX]);

    I get an access violation?
    http://www.c5software.co.uk (site is being developed at the moment)

  4. #4

    Another Pointer Question - TObjectList

    Ok, i guess that was expected lol
    I mostly use TList and i use a function like:
    [pascal]
    function TMyList.GetItem(Index: Integer): TMyObject;
    begin
    Result := Nil;
    If (Index < 0) Or (Index > Count-1) Then Exit;
    Result := TMyObject(inherited Items[Index]);
    end;
    [/pascal]

    Try using:
    [pascal]
    Player := TCreature(lstCreatures.Items[iX]);
    [/pascal]
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  5. #5

    Another Pointer Question - TObjectList

    Quote Originally Posted by M109uk
    Ok, i guess that was expected lol
    I mostly use TList and i use a function like:
    [pascal]
    function TMyList.GetItem(Index: Integer): TMyObject;
    begin
    Result := Nil;
    If (Index < 0) Or (Index > Count-1) Then Exit;
    Result := TMyObject(inherited Items[Index]);
    end;
    [/pascal]

    Try using:
    [pascal]
    Player := TCreature(lstCreatures.Items[iX]);
    [/pascal]
    Now I get Incompatible types 'TCreature' and 'PCreature'

    :?
    http://www.c5software.co.uk (site is being developed at the moment)

  6. #6

    Another Pointer Question - TObjectList

    hmm ok.. giv me a sec to get some code up..

    Do you need to use:
    Player: PCreature; ?
    cant you use TCreature instead??

    Not sure if it will work but i dont get any errors:
    [pascal]
    Player := Pointer(lstCreatures[i]);
    [/pascal]
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  7. #7

    Another Pointer Question - TObjectList

    Quote Originally Posted by M109uk
    hmm ok.. giv me a sec to get some code up..

    Do you need to use:
    Player: PCreature; ?
    cant you use TCreature instead??

    Not sure if it will work but i dont get any errors:
    [pascal]
    Player := Pointer(lstCreatures[i]);
    [/pascal]
    If I use PCreature it means I don't have to type cast it. I don't know if that's the best way of doing it or not?

    Alternatively I could type cast it when I need to use it:-

    eg.

    TCreature(Player).DoAction;

    as opposed to

    Player.DoAction;

    I really need to get to grips completely with pointers and pointers to types :?

    I have used pointers throughout the game so far with no problems, it's just this that's bugging me (litteraly)
    http://www.c5software.co.uk (site is being developed at the moment)

  8. #8
    Anonymous
    Guest

    Re: Another Pointer Question - TObjectList

    Quote Originally Posted by Gadget
    Hi, Player := @lstCreatures.Items[iX];

    =D
    lstCreatures.Items , must be type TCreature, if not, error will occur..

  9. #9

    Another Pointer Question - TObjectList

    Quote Originally Posted by Gadget
    If I use PCreature it means I don't have to type cast it. I don't know if that's the best way of doing it or not?

    Alternatively I could type cast it when I need to use it:-

    eg.

    TCreature(Player).DoAction;

    as opposed to

    Player.DoAction;

    I really need to get to grips completely with pointers and pointers to types

    I have used pointers throughout the game so far with no problems, it's just this that's bugging me (litteraly)
    I see your point, is there a particular reason why you use TObjectList, you could always use TList which instead of resulting with TObject TList results the pointer and you can base your own class on this.. e.g.

    [pascal]
    type
    TCreature = Class
    // TCreature code
    public
    Name: String;
    procedure DoAction;
    end;

    TCreatures = Class(TList)
    private
    function GetCreature(Index: Integer): TCreature;
    public
    function Add(Name: String): Integer;
    property Items[Index: Integer]: TCreature read GetCreature; default;
    end;

    implementation

    //----< TCreature >----//
    procedure TCreature.DoAction;
    begin
    // Action
    end;

    //----< TCreatures >----//

    function TCreatures.GetCreature(Index: Integer): TCreature;
    begin
    Result := Nil;
    If (Index < 0) Or (Index > Count-1) Then Exit;
    Result := TCreature(inherited Items[Index]);
    end;

    function TCreatures.Add(Name: String): Integer;
    var
    nCreature: TCreature;
    begin
    nCreature := TCreature.Create;
    nCreature.Name := Name;
    Result := inherited Add(nCreature);
    end;
    [/pascal]

    and with this you can do what you want: Players[i].DoAction;

    I find using this is a lot easier to deal with

    hope it helps..
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  10. #10
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Another Pointer Question - TObjectList

    [pascal]Var
    P : PCreature;
    T : TCreature;
    begin
    T := TCreature(MyList[0]);
    P := @T;
    Edit1.Text:= IntToStr(P^.ID);
    End;[/pascal]

    I must say that I think you've missed the point of classes. In Delphi any variable of a class type is already a pointer. There is no reason to have a PType as a pointer to the class as you are effectivly creating a pointer to a pointer to the class instance. This PType structure basically is legacy from Turbo Pascal and should not be used anymore.

    Just change your Player to be of type TCreature and you will effectly have achieved everything you need to do.

    The only time you need PTypes in delphi is if you are using records and want to put them into something like an object list.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

Page 1 of 3 123 LastLast

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
  •