Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting component's properties as TStrings 1

Status
Not open for further replies.

Griffyn

Programmer
Jul 11, 2002
1,077
AU
Hi all,

Still working on this RPG game - all chuffing along nicely. I've got a database (Access) with tables of Events (such as what happens when you wear a magic item). The table contains names of properties for the TEntity object (which holds all personal information for a game hero).

The problem is that in my code that's reading these events I have such ugliness as:

Code:
  if AField = 'Gold' then
    AEntity.Gold := ....
  else if AField = 'Gems' then
    AEntity.Gems := ....
  else if AField = 'HitPoints' then
    AEntity.HitPoints := ...
  etc.
  etc.

where AField is the text retrieved from the database table indicating which property of the TEntity object to adjust.

I have quite a few different classes like this - the TEntity one has over 40 properties that need to be mapped, which is not very nice code. It works, but I would appreciate any advice on restructuring or shortcuts to doing it.

Many thanks!
 
A case for Run time type info (RTTI). This only works for published properties. You might need to adjust your TEntity acordingly.

Code:
uses TypInfo;  {All versions need this}

SetPropValue(AEntity, AField, [value]);

If all your values are integers, the following will be faster.

Code:
SetOrdProp(AEntity, AField, [integer]);

Above will error if AField is not a valid published property name. If you want to trap out invalid property names use following version:

Code:
var
  LPropInfo: PPropInfo;
begin
  LPropInfo:= GetPropInfo(AEntity, AField);

  if (LPropInfo<> nil) then 
    SetOrdProp(AEntity, LPropInfo, [integer]);
end;

Have fun
Simon

 
ahhh. I stumbled across RTTI in the help, but it didn't seem to go anywhere. The help on the TypInfo unit seems to be missing the majority of the unit's routines. At least I've got the source to help me.

Thanks Simon!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top