Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: handle of object?

  1. #1

    handle of object?

    is there any way to convert an object into an integer handle (or pointer)
    and then convert it back?

    I want to be able to serialize an object like this

    Code:
    type 
    warrior=class(Tobject)
    enemy:warrior
    so I need to know.. when I save the warrior to a file, who his enemy is, so I need to convert the enemy to an integer handle, so I can serialize it
    ( I know you cant save handles to a file and then convert them back into objects when you load a new game, but they are useful to stop cyclic references when loading or saving a game)
    Last edited by slenkar; 01-12-2010 at 08:09 PM.

  2. #2
    I'd declare a base class with methods to save/load an instance to a stream or a file. Or take a look at the TPersistent class.

  3. #3
    do you have a little example of saving an object with Tpersistent?

    I tried running the example xmlstream in the lazarus folder but it says
    streamasxmldemo.lpr(7,3) Fatal: Can't find unit Interfaces used by StreamAsXMLDemo
    Last edited by slenkar; 01-12-2010 at 09:11 PM.

  4. #4
    Do you have latest Lazarus daily snapshot or SVN? I just added Interfaces in my uses path and it compiled fine.

  5. #5
    I managed to get the example running but the code is very long and uncommented

    I put together a little program based on online examples:
    Code:
    program pascalreflect;
    
    {$mode objfpc}{$H+}
    
    uses
    classes,typinfo;
     type
    
     warrior =class(Tobject)
    
     property
     name:String;
     hp:integer;
     end;
    var
    TypeInfo: PTypeInfo;
    TypeData: PTypeData;
    PropList: PPropList;
    classref:Tclass;
    war:warrior;
    war2:warrior;
    listy:array of string;
    iter:integer;
    {$IFDEF WINDOWS}{$R pascalreflect.rc}{$ENDIF}
    
    begin
    war:=warrior.create;
    
    
    ClassRef := warrior.ClassType;
    
    war2:=classref.newinstance as warrior;
    //classname
    writeln (classref.ClassName);
    //typeinfo
    TypeInfo := warrior.ClassInfo;
    if typeinfo=nil then
    begin
    writeln('typeinfo is nil');
    end
    else
    begin
    TypeData := GetTypeData(TypeInfo);
    end;
    
    
    
    
    end.
    typeinfo is null though

    why is that?

  6. #6
    I have 1 guess; you are using .ClassInfo on type instead of war or war2 instances.
    You can't do warrior.name:='test';

    The next line seems also wrong:
    lassRef := warrior.ClassType;
    should be lassRef := war.ClassType;

    For clarity i recommend using capital T in classnames, such as TWarrior. This makes it harder to mix classes with variable names.

  7. #7
    ok got a bit closer to getting the features I need to save a game
    Code:
    uses
      rttiutils,typinfo,classes;
    
    
    type
      warrior =class(Tpersistent)
      public
      hp:integer;
      warname:string;
      published
    
       property Php:integer read hp write hp;
       property Pwarname:string read warname write warname;
        end;
    
    
    Var
      O : warrior;
      PT : PTypeData;
      PI : PTypeInfo;
      I,J : Longint;
      PP : PPropList;
      prI : PPropInfo;
    
    begin
      O:=warrior.Create;
      PI:=O.ClassInfo;
      PT:=GetTypeData(PI);
      Writeln('Property Count : ',PT^.PropCount);
      GetMem (PP,PT^.PropCount*SizeOf(Pointer));
      GetPropInfos(PI,PP);
      For I:=0 to PT^.PropCount-1 do
        begin
        With PP^[i]^ do
          begin
          Write('Property ',i+1:3,': ',name:30);
          writeln('  Type: ',typinfo.PropType(O,Name));
          end;
        end;
      FreeMem(PP);
      O.Free;
    end.

  8. #8
    ok now i can get and set strings and integers without having to know the names of the fields.
    getting closer to having a serialization library for pascal

    Code:
    Program pasreflect;
    
    { This program demonstrates the GetPropList function }
    
    uses
      rttiutils,typinfo,classes,sysutils;
    
    
    type
      warrior =class(Tpersistent)
      public
      hp:integer;
      warname:string;
      published
    
       property Php:integer read hp write hp;
       property Pwarname:string read warname write warname;
        end;
    
    
    Var
      O : warrior;
      PT : PTypeData;
      PI : PTypeInfo;
      I,J : Longint;
      PP : PPropList;
      prI : PPropInfo;
    
    begin
      O:=warrior.Create;
      PI:=O.ClassInfo;
      PT:=GetTypeData(PI);
      Writeln('Property Count : ',PT^.PropCount);
      GetMem (PP,PT^.PropCount*SizeOf(Pointer));
      GetPropInfos(PI,PP);
      For I:=0 to PT^.PropCount-1 do
        begin
        With PP^[i]^ do
          begin
          Write('Property ',i+1,': ',name);
          PrI:=GetPropInfo(O,name);
          if typinfo.PropType(O,Name)=tkinteger  then
          begin
          SetOrdProp(O,PrI,31);
          writeln('value'+ inttostr(GetOrdProp(O,PrI)));
          end;
          if typinfo.PropType(O,Name)=tksstring  then
          begin
          SetstrProp(O,PrI,'bill');
          writeln('value'+ (GetstrProp(O,PrI)));
          end;
    
          writeln('  Type: ',typinfo.PropType(O,Name));
          end;
        end;
      FreeMem(PP);
    
    
      writeln (inttostr(O.hp));
    
      O.Free;
    end.

  9. #9
    I need a unique ID number for each object

    I tried using the pointer address but the compiler said it wasnt portable
    how else can I get a unique ID number for each object?

    it has to be compatible with Tobject, as the serialization doesnt know the real object type.

    I tried looking at the JSON library in lazarus.. is it cross platform?
    Last edited by slenkar; 04-12-2010 at 08:21 PM.

  10. #10
    Below is a link you may find interesting. It's in Polish though. Try Google Translate or if you don't really need to understand the article's text, just focus on the code itself.
    http://delphi.dathox.com/2009/12/ser...cz2-i-cz3.html

Page 1 of 2 12 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
  •