DXScript Tutorial - Stardock



DXScript Tutorial .2

By Ed Britton

Nostrildamus@

2/14/02

This is a quick & dirty tutorial for scripting in DesktopX using the DXScript module. I use VBScript as the language in this tutorial, but if you prefer javascript most of it is very easy to reapply. It assumes you have a small amount of knowledge of DesktopX already, namely creating objects and setting some of their properties.

Table of Contents

1. VBScript and JavaScript Basics

2. Hello, World! - DXScript Basics

3. Object States - Using states

4. Object Properties - Local and non local properties

5. Timers - Creating and setting timers

6. Controls - An example

7. The Debugger

A1. Useful Functions

A2. References

A3. Extra Examples

1. VBScript and JavaScript Basics

Before you get started with scripting in DesktopX, you must learn the basics of a scripting language. If you feel that you already have a working knowledge of VBScript or JavaScript you can go ahead and skip this chapter.

Now is a good time to choose what language you will use. VBScript and JavaScript are both good choices, as they come with your operating system. Other languages such as Perl may be used if you feel that you are more comfortable with them. This tutorial however only covers these two languages, and at the moment all examples are written in VBScript, as it is the easier of the two.

How do you decide upon which language to use? As I stated above, VBScript is easier as it uses a subset of the Basic syntax, but if you’re more familiar with a C++ environment JavaScript might be more what you’re used to. They both do a fine job of scripting in DX.

General

Every command line in JS must have a ; at the end, whereas you do not need them in VBS. Everything that has a beginning and end in JS uses the braces {} to start and end, while VBS uses special end names for every type. Both languages are case sensitive, so be sure to always make sure your case is right.

Ex.

JavaScript:

function Yay()

{

X = 1;

if(x == 1)

{

Script.MsgBox(“Whee!”);

}

}

VBScript:

Function Yay()

X = 1

If X = 1 Then

Script.MsgBox(“Whee!”)

End If

End Function

Comments

Comments are lines that won’t be read as commands, so you can put comments throughout your code. In VBScript anything after Rem is a comment, and in JavaScript anything after // is a comment.

Ex.

Rem This adds 1 to X

X = X + 1

// This adds 1 to X

X++;

Variables

Variables are used to store information of some type, such as a number or text. Variables are declared the same way in both VBS and JS. Simply type the name of the variable, an = sign, and what you want to set it to. Numbers have nothing around them but you must put text in quotes.

Ex.

Name = “Ted”

Age = 25

After creating the variables, you can reassign them any time with an equal sign. The data stored in the variable can now be used in place of words or numbers by putting in the name of the variable, for instance:

X = 5

Y = 2

Z = X + Y + 4

In this instance Z would be equal to 11.

Operators

Operators are symbols that perform calculations, such as adding or checking to see if one number is the same as another. Both languages have the math operators +-*/%. / is for division and % is for modulus. In JS you can add or subtract one from a variable by using ++ and -- after the variable name. To compare variables, both have the operators != (Not equal to), >, = (Greater or equal to), ScreenX Then xshift = -2

If < 0 Then yshift = 2

If Object.Bottom > ScreenY Then yshift = -2

Object.Left = Object.Left + xshift

= + yshift

End Sub

Object: Orbit

Rem Find the X pos in a circle

Function CircleX(Center,Radius,Angle)

CircleX = Cos(Angle)*Radius + Center

End Function

Rem Find the Y pos in a circle

Function CircleY(Center,Radius,Angle)

Rem Return Sin(Angle)*Radius + Center

CircleY = Sin(Angle)*Radius + Center

End Function

Rem Globals

Angle = 0

SizeX = Object.Right - Object.Left

SizeY = Object.Bottom -

Rem Options

Rem The radius of the orbit

Radius = 150

Rem the Object you want to orbit

Obj = "Ball"

Rem the speed in millisecs of each update

Speed = 30

Rem the smoothness of the angle increment

AngInc = .05

Rem Startup

Sub Object_OnScriptEnter

Rem Set timer 11 to the speed

Object.SetTimer 11,Speed

End Sub

Rem Main code

Sub Object_OnTimer11

Rem Inc the angle

Angle = Angle + AngInc

Rem restart the angle if needed

If Angle > 360 Then Angle = 0

Rem Find the horiz center of the object

CenX = ((DesktopX.ScriptObject(Obj).Object.Right - DesktopX.ScriptObject(Obj).Object.Left) / 2) + DesktopX.ScriptObject(Obj).Object.Left

Rem Find the vertical center of the object

CenY = ((DesktopX.ScriptObject(Obj).Object.Bottom - DesktopX.ScriptObject(Obj).) / 2) + DesktopX.ScriptObject(Obj).

Rem Set the X pos

X = CircleX(CenX,Radius,Angle)

Rem Set the Y pos

Y = CircleY(CenY,Radius,Angle)

Rem Reposition the orbiter

Object.Left = X - SizeX/2

= Y - SizeY/2

End Sub

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download