| Home Page | Recent Changes | Preferences

Unrealscript Reflection

Reflection is the ability of a computer language to examine properties of itself. (See also Wiki logo OnReflection.)

This feature is part of languages such as Java and .NET (C#, VB and possibly C++ and other languages of the .NET family).

It is not possible in UnrealScript alone, but apparently possible using C++ (don't know if you can make [native function]?s for the purpose).

(thanks to El Muerte TDS for answering this question in the BeyondUnreal forums).

Background

As part of my efforts to dump Unreal objects to XML, I've been looking for a way to enumerate the properties of objects without knowing what those properties are in advance.

Something like this (caution: air code, probably not valid UnrealScript)

// Given an object, enumerate all of its properties in an XML fragment.
/** 
    WARNING: This is not likely to be valid UnrealScript - just a sort of pseudocode

    I'm assuming here that:
    - the properties are accessable via a linked list of "property" objects in the class,
    - each property has a a way of determining its type and value.
    - a few other things I may not realize yet
*/
function DumpObjectToXML(Object someObject)
{
    local UProperty aProperty;      // a property of a Class
    local string aPropertyValue;    // value of a Class, casted to a string
    local UPropertyType itsType;    // i.e. string, int, etc.  It's probably really an enum if it exists
    
    aProperty = someObject.ClassList;
  
    while aProperty != None {
        // do something interesting with the property value, like add it as an Xml node?
        AddXmlNoteToTree(string(itsType), aPropertyValue, someXmlNodeParent, NT_ATTRIBUTE);
        aProperty = aProperty.NextProperty;
    }

}

Poor man's property enumerator.

function DumpProperties( Object O )
{
    local Property P;
    foreach AllObjects( class'Property', P )
        if( P.outer == O.class  )
            log( "[TYPE]" @ P.class.name @ "[NAME]" @ P.name @ "[VALUE]" @ O.GetPropertyText( string(P.name) ) );
}

Is it possible?

Not with UnrealScript, according to El Muerte TDS. The articles suggest you can do this in C++ (native functions, possibly?).

You might achive a "poor man's" reflection using UnCodeX with a specialized template to build a class file, though I've not tried this. Might be sufficient for many purpoeses though.

For example, you could probably generate a function to dump a whole object to XML by doing something interesting with an UncodeX template and a combination of your own classes.

More on this later, if I can get it to work! :)

Notes, open questions

  • There's some interesting functions for getting object metadata. Are there more?
    • GetPropertyText()
    • SetPropertyText()
    • GetEnum
    • (various means of casting names to strings, classes to strings, etc)
  • SaveConfig suggests a way of saving objects to INI files..
    • Can you save an entire object in one INI file?
    • How about looping through all loaded objects and doing a SaveConfig on them, could this be useful?
  • There's a couple articles (from the man himself, Tim Sweeney!) that hint at doing this, but don't describe how:

Foxpaw: I'm not really clear on what this "reflection" is. Is it just like when an IDE picks out the variables and functions and junk and.. well, I'll assume someone finds that sort of thing useful. Anyways, I'm not really sure what "reflection" is.

Xhiris: It's almost exactly like what an IDE would do, but you can do it within your code. Think of it as the ability to ask questions about your code from your code. A more detailed explanation is given at the OnReflection link Tarquin put at the top of this page.

Tarquin: I have no idea what it is either, but heck, if you want to discuss it here, go for it :)

Foxpaw: I went to that link but I don't really understand what it's all about. Why would your code need to know about itself, and what can be accomplished by doing so?

dataangel: Now that ut2k4 has proper save ability, can't it serialize objects? If not individual ones, it'd still be theoretically possible by exploring the save file.

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

FAQs

Help Desk

Mapping Topics

Mapping Lessons

UnrealEd Interface

UnrealScript Topics

UnrealScript Lessons

Making Mods

Class Tree

Modeling Topics

Chongqing Page

Log In