Your DesktopX Documentation Resource Guide

Documentation Home

Quick Start

User's Guide

Developer's Guide

Resources

Tutorials


Dynamically create and clone objects

  • Start downloading the demo object here and import it as objects inside your theme.

  • You'll see two buttons and another textured object with two text children inside it.
  • The green button adds a new object (including its children) and stacks it below it.
  • The red button removes the last added clone.
  • This shows the preferred method to clone or "create" object. It can be seen as "creating" in that you are basically providing a "template" objects+children you want to create at runtime. This is a "template" in that it can well be hidden (Visible = No in Relation panel), but is used as prototype to create the actual object. It is much simplier and faster than actually create each object, children and all its state properties at runtime via code (and it is not currently possible).
  • The prototype object is the first one in the picture and it is left visible for simplicity.

Cloning the prototype composition

The interesting part is inside the green button script:

Sub Object_OnLButtonUp(x,y,b)
If b = False Then
  Dim i, strname
  i=0
  Do
    i = i+1
    strname = "clone_base" & i
  Loop until DesktopX.IsObject(strname)=False

  DesktopX.object("clone_base0").Clone strname, DesktopX.object("clone_base0").left, _
        DesktopX.object("clone_base"&(i-1)).bottom+10
  DesktopX.object("clone_child_A").Clone "clone_child_A" & i, _
        DesktopX.object("clone_child_A").left, DesktopX.object("clone_child_A").top
  DesktopX.Object("clone_child_A" & i).Parent = DesktopX.Object("clone_base" & i)
  DesktopX.object("clone_child_B").Clone "clone_child_B" & i, _
        DesktopX.object("clone_child_B").left, DesktopX.object("clone_child_B").top
  DesktopX.Object("clone_child_B" & i).Parent = DesktopX.Object("clone_base" & i)
End If
End Sub

The first piece of the script searches for an available "name", it just calls DesktopX.IsObject to know what existing clones already exist and find a "free" name.

Then, the steps are basically:

  • Clone the parent
  • For each children
    • Clone the children
    • Assign the newly created parent to the newly created children

Hopefully, the next DesktopX versions will provide handier methods for cloning complex compositions in one shoot, however this is the clean way to do it now.

Destroying clones

The code for removing a clone is similar and is inside the red object:

Sub Object_OnLButtonUp(x,y,b)
If b = False Then
  Dim i, strname
  i=0
  Do
    i = i+1
    strname = "clone_base" & i
  Loop until DesktopX.IsObject(strname)=False


  If > 1 Then
    DesktopX.object("clone_child_A" & (i-1)).delete
    DesktopX.object("clone_child_B" & (i-1)).delete
    DesktopX.object("clone_base" & (i-1)).delete
  End If
End If
End Sub

Again, it is first searching for the last created clone.

The second part of the code deletes it:

  • First it deletes each child, then it deletes the parent.
  • If objects are nested in a more complicated way, the script should first delete the most inner children, to avoid that one remains unparented in the process.

 


7/29 

SkinStudio 6.2 Released

7/25 

A God Has Fallen - New Demigod Trailer Released

7/24 

Sins of a Solar Empire v1.1 Beta has Arrived

7/23 

Stardock Releases New WindowBlinds 6.2 Update

7/22 

Stardock Releases The Political Machine v1.04 with New Characters

7/22 

Stardock Releases MyColors 2.5

7/17 

DesktopX 3.5 Officially Released

7/11 

Corel WinDVD 9 and Painter X Now Available on Impulse!