Creating objects without config

edited January 2017 in General discussions
Hi all, I'm currently learning Orx, and after some struggling I figured out how to create objects from code, without using any config.

I didn't see this documented anywhere. All the examples seem to be using orxObject_CreateFromConfig.

Here is an example of how to create an object purely from code:
orxOBJECT * createImageObject (char * filename, float pos_x,
                float pos_y, float scale_x, float scale_y)
    orxOBJECT * obj = orxObject_Create ();

    orxFRAME * frame = orxFrame_Create (orxFRAME_KU32_FLAG_NONE);
    orxObject_LinkStructure (obj, (orxSTRUCTURE*) frame);

    orxTEXTURE * tex = orxTexture_CreateFromFile (filename, orxTRUE);
    orxFLOAT image_x;
    orxFLOAT image_y;
    orxTexture_GetSize (tex, &image_x, &image_y);

    orxGRAPHIC * gfx = orxGraphic_Create ();
    orxGraphic_SetData (gfx, (orxSTRUCTURE*) tex);
    orxObject_LinkStructure (obj, (orxSTRUCTURE*)gfx);

    orxVECTOR v;

    v.fX = image_x;
    v.fY = image_y;
    v.fZ = 1.0;
    orxObject_SetSize (obj, &v);

    v.fX = pos_x;
    v.fY = pos_y;
    v.fZ = 0.0;
    orxObject_SetPosition (obj, &v);

    v.fX = scale_x;
    v.fY = scale_y;
    v.fZ = 1.0;
    orxObject_SetScale (obj, &v);

    return obj;

The important part here is creating a frame, and linking it to the object. Without that the object won't appear. Then a texture and a graphic can be created and linked to the object in that way. Finally the object's position, size and scale can be set. (Size comes from the image size in this example.)

I have only tested this on Android, but I guess it would work on other platforms too.

(Also note, that I think Orx's config system is quite good, but I needed completely dynamic objects for my project.)


  • edited January 2017
    Hi rgdev and welcome here!

    Don't hesitate to ask questions either here or on the chat ( if you find yourself stuck or struggling too much.

    In your example, you'll be in charge of doing the cleanup yourself: you're the owner of what you directly created, in this case the orxGRAPHIC, the orxTEXTURE and the orxFRAME.

    If you still want a dynamic approach but without having to trouble yourself with the bookkeeping, you can modify the config's content on the fly. Here's a quick example (I've written it directly on the forum so it might not fully work/compile):
    orxOBJECT * createImageObject (char * filename, float pos_x,
                    float pos_y, float scale_x, float scale_y)
        orxConfig_SetString("Graphic", "@");
        orxConfig_SetString("Texture", filename);
        orxOBJECT * obj = orxObject_CreateFromConfig("ImageObject");
            orxVECTOR v;
            orxVector_Set(&v, pos_x, pos_y, 0.0);
            orxObject_SetPosition(obj, &v);
            orxVector_Set(&v, scale_x, scale_y, 1.0);
            orxObject_SetScale(obj, &v);
        return obj;

    Of course both position and scale could also be passed via config, but in that case there's no ulterior bookkeeping to be made so I left them as is. You can of course modify any other relevant property or inherit from other sections as you see fit.
  • edited January 2017
    Hi iarwain, thanks for all the info.

    I guess that's the more standard way of creating objects dynamically in Orx. :)
  • edited January 2017
    Hi rgdev,

    My pleasure. Your approach definitely works so don't hesitate to use it if you prefer it. I'm just a bit lazy and I'd rather have the engine doing the cleanup instead of doing it myself, that's why I choose the second option. :)
  • edited January 2017
    Hi rgdev. Welcome.

    This might make a good small tutorial if you don't mind me pinching the code for the wiki.
  • edited February 2017
    Hi sausage,

    I think a tutorial about manually creating objects and cleaning them up would be a good idea. Sure, feel free to reuse any of this code.
  • edited September 2017
    Thanks for the advice.
  • edited June 2017
    Hi Trovetaira and welcome here!

    If you need more advices, have any questions or just want to chat, don't hesitate to use the chat as well: :)
Sign In or Register to comment.