DesktopX: Scripts that create and clone objects
Feel the power.
Wednesday, March 23, 2005 by Frogboy | Discussion: DesktopX Tutorials
Alberto Riccio, lead developer of DesktopX, has written this tutorial. DesktopX 3 has the ability to create and clone objects.
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,![]()
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,![]()
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.
Reply #2 Friday, March 25, 2005 2:57 AM
Here is what the code would look like in JScript
The add script
function Object_OnLButtonUp(x,y,
{
if (b == false)
{
i=0;
do
{
i++;
strname = "clone_base" + i;
} while (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);
}
}
The remove script
function Object_OnLButtonUp(x,y,
{
if (b == false)
{
i=0;
do
{
i++;
strname = "clone_base" + i;
} while (DesktopX.IsObject(strname) != false);
if (i > 1)
{
DesktopX.Object("clone_child_A" + (i-1)).Delete();
DesktopX.Object("clone_child_B" + (i-1)).Delete();
DesktopX.Object("clone_base" + (i-1)).Delete();
}
}
}
I can try to do this for some of the other examples.
One big thing that seems to be missing is the MsgBox function.
I created my own function by using a an activeXObject
function MsgBox(message)
{
WshShell = new ActiveXObject( "WScript.Shell" );
n = WshShell.Popup( message, 0, "", 0 );
}
Please login to comment and/or vote for this skin.
Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:
- Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
- Access to a great community, with a massive database of many, many areas of interest.
- Access to contests & subscription offers like exclusive emails.
- It's simple, and FREE!







Reply #1 Wednesday, March 23, 2005 11:25 PM