`
web3d
  • 浏览: 82898 次
  • 性别: Icon_minigender_1
  • 来自: 南昌
社区版块
存档分类
最新评论

Virtools脚本语言(VSL)教程 - 值、类型与变量

 
阅读更多
<h3>值</h3>
<p>VSL能识别以下类型的值:</p>
<blockquote>
<li>数字,如123、3.14159、'v'或0xe0d02090</li>
<li>逻辑布尔值:true或false</li>
<li>字符,如"Virtools,The Behavior Company"</li>
<li>特殊关键字<em>null</em>等于</li>
</blockquote>
<h3>
<a name="TOC-2"></a>类型</h3>
<p>VSL是一种类型化的语言,这意味着如我们所知的在编译时(compilation time)每个变量和每个表达式都带有类型。类型决定了运算的意义;强类型可以帮助在编译时检查错误。</p>
<p>VSL类型划分为3类:</p>
<blockquote>
<li>基本类型</li>
<li>VSL结构类型</li>
<li>C++ 类/结构类型</li>
</blockquote>
<p></p>
<p>基本类型即布尔型(bool)与数字类型。数字类型为整型(<em>char,int</em>)及浮点型(<em>float</em>)。</p>
<p></p>
<table border="1" cellspacing="0" cellpadding="1"><tbody>
<tr>
<td valign="top"><strong>类型</strong></td>
<td valign="top"><strong>内容</strong></td>
<td valign="top"><strong>大小</strong></td>
<td valign="top">
<strong>取值范围</strong><br>
</td>
</tr>
<tr>
<td valign="top">bool</td>
<td valign="top">bool类型是一种整数类型。它用两个可能的值(用true或false来表示)代表逻辑数</td>
<td valign="top">4 Bytes</td>
<td valign="top">true 或false (0 or 1)</td>
</tr>
<tr>
<td valign="top">char</td>
<td valign="top">char类型也是一种整数类型,通常包含了ASCII字符集成员的执行。</td>
<td valign="top">4 Bytes</td>
<td valign="top">-128 to 127</td>
</tr>
<tr>
<td valign="top">int</td>
<td valign="top">int类型也是一种整数类型。等价于C/C++中的无符号的int类型。</td>
<td valign="top">4 Bytes</td>
<td valign="top">?147483647? to 2147483647</td>
</tr>
<tr>
<td valign="top">float</td>
<td valign="top">float类型是一种浮点数类型。等价于C/C++中的float类型。</td>
<td valign="top">4 Bytes</td>
<td valign="top">-3.402823466e+38F to 3.402823466e+38F</td>
</tr>
<tr>
<td valign="top">str</td>
<td valign="top">str类型等价于C/C+中的char*类型。</td>
<td valign="top">4 Bytes</td>
<td valign="top">-</td>
</tr>
</tbody></table>
<p></p>
<p>正如你所见,所有VSL基本类型都是以4个字节实现的,在内存中是字组对齐方式,不受它们是否需要更多空间的情况约束。例如,char类型仅需一个字节来表示ASCII码表中的成员,但在VSL中仍用了4个字节。</p>
<p></p>
<p>VSL结构是在VSL中创建自定义数据类型的一种方式。VSL结构可以有零个或多个成员,这些成员可以是任何VSL中的已知类型。VSL结构和C的结构非常相似。<br><br>结构声明实例:</p>
<pre>struct Car<br>{<br> Entity3D entity;<br> Vector direction;<br> float gaz;<br>};
<p></p>
</pre>
<li>VSL可以操作C/C++类型。在VSL中使用类(class)之前,你必须绑定它。</li>
<p>VSL来源于很多Virtools SDK中的约束类(bound classes)。大多数情况下,类的名称从Virtools SDK到VSL会发生改变。对于Virtools SDK用户来说,CK3dEntity是熟知的一个类,但到了VSL它的名字就变为"Entity3D"了。</p>
<h3>
<a name="TOC-3"></a>变量</h3>
<p>变量是一个存储单元,且带有一个相关联的类型,可能是基本类型、VSL结构或C/C++类。变量总是包含一个和它的类型相兼容的所赋的值。变量值通过赋值或前缀式或后缀式 ++ / --运算符来进行改变。</p>
<p>这里有些在VSL中进行变量声明的实例:</p>
<pre><span>// Declare an integer variable named "i"</span>
int i;<br><span>// Declare an array of 123 float named "table"</span>
float table[123];<br><span>// Declare a Vector variable named "speed", initialized with values 1.0, 2.0 and 3.0</span>
Vector speed(1.0,2.0,3.0);<br><span>// Declare a variable referencing an Entity3D</span>
Entity3D bird;

<h3>
<a name="TOC-4"></a>变量范围</h3>
<p>VSL里变量名仅能在程序的特定区域中使用。这个区域叫做变量名的适用范围。适用范围觉得累变量名(指非静态或非共享变量)的生命周期。适用范围也决定了在调用类的构造器或析构器时变量名的可见性,及什么时候进行局部变量的初始化。</p>
<pre>void foo(int a) <span>// start of the function foo scope</span>
{
if (a) <span>// start of if scope</span>
{

} <span>// end of if scope</span>
} <span>// end of the function foo scope</span>

<p>让我们通过一个简单的实例来考察一下变量的生命周期:</p>
<pre>float angle; <span>// Declaring a global variable</span>

void foo()
{
angle = 0.01; <span>// ok</span>
}

void foo2(int a)
{
angle = a*180/P; <span>// error, P is not yet declared</span>
float P = 3.14159;

if (a) <span>// ok</span>
{
float angle2 = 360-angle; <span>// ok</span>
}
bc.OutputToConsole(angle2); <span>// it doesn't exist outside of the scope of the enclosing braces {...}<br></span>
}

<p>当你在一个函数外声明了一个变量时,它会以全局变量方式被调用,因为在当前VSL脚本的各个地方都是有用的。</p>
<h3>
<a name="TOC-5"></a>静态与共享全局变量</h3>
<p>我们的<strong>全局</strong>变量可以是共享式的或静态的。</p>
</pre></pre></pre>
<ul>
<li>静态全局变量具有在某个脚本的两次执行中保持它的值的特性。</li>
<li>共享全局变量具有保持它的值静态不变的能力,但所有对该变量进行了声明的脚本间都可以共享该变量。</li>
</ul>
<p><br><strong>怎样声明静态或共享全局变量</strong></p>
<pre>shared int a; <span>// a is a shared global variable</span>
static float b; <span>// b is a static global variable</span>

void main()
{
shared int i;<span>// i is not a shared variable (shared keyword is ignored)</span>
static float j;<span>// j is not a static variable (static keyword is ignored)</span>
<span>// ...</span>
}

<p>在声明静态或共享全局变量时,你无法初始化或调用那种没有默认<strong>构造器</strong>环境的变量。</p>
<pre>shared int a = 0; <span>// error</span>
static Vector v(1.0,2.0,3.0); <span>// error</span>

void main()
{
static int b = 0; <span>// Not an error because static keyword is ignored</span>
shared Vector w(-1,0,0); <span>// Not an error because shared keyword is ignored</span>
<span>// ...</span>
}

<p><strong>静态全局变量</strong></p>
<p>实例:</p>
<pre><span>// ...</span>
static int a;

void main()
{
if (iIn1) { <span>// iIn1 is a boolean global variable</span>
a = 0;
} else {
++a;
}
}

<p>如果在我们第一次执行脚本的时候,iIn1为true,a等于0。<br>然后,为了下一次脚本执行时a的值增加,我们将iIn1设为false,所以到时候第二次执行会将a设为1,而第三次则会将a设为2,等等。<strong><br></strong></p>
<p><strong>共享全局变量</strong></p>
<p>实例:</p>
<pre><span>// First script</span>
shared Vector pos;

void main()
{
pos.Set(0,1,0.5);
}

<pre><span>// Second script</span>
shared Vector pos;<span> // the second script also declares pos as a shared Vector</span>

void main()
{
Vector test(0,1,0.5)
if (pos == test) {
<span>// ...</span>
}
}

<p>如果我们在第二个脚本执行了第一个脚本,pos的值是 {0,1,0.5} ,所以在第二个脚本中条件 pos == test 为true。</p>
<h3>
<a name="TOC-6"></a>变量名</h3>
<p>在VSL中,一些特定的名字是不能作为变量名使用的,如类/结构名及以下保留字:</p>
</pre></pre></pre></pre></pre>
<table style="width: 276px;" border="0" cellpadding="10"><tbody><tr valign="top">
<td width="8%">main<br>int<br>short<br>float<br>char<br>while<br>do<br>bool</td>
<td width="12%">if<br>for<br>then<br>else<br>true<br>false<br>TRUE<br>FALSE</td>
<td width="14%" valign="top">NULL<br>null<br>struct<br>class<br>enum<br>extern<br>shared<br>static<br>
</td>
<td width="66%" valign="top">
<p>switch<br>case<br>default<br>break<br>continue<br>return</p>
</td>
</tr></tbody></table>
<p>编译器按以下顺序搜索名字,当找到了名字时就停止:</p>
<blockquote>
<li>从当前范围到全局范围搜索</li>
<li>搜索看名字是否为枚举值</li>
<li>搜索看名字是否为定义的常量</li>
</blockquote>
<pre>void foo(int a)<br>{<br> bc.OutputToConsole(pi); <span>// print 3.14159 (pi is a constant in VSL)</span>

float pi = 1.0;

if (a)
{
float pi = 0.5;
bc.OutputToConsole(pi); <span>// print 0.5</span>
}

bc.OutputToConsole(pi); <span>// print 1.0</span>
}

<pre>enum Const<br>{<br> pi,<br> epsilon,<br> maxint,<br>};<br><br>
void foo(int a)
{
bc.OutputToConsole(pi); <span>// print 0</span>

float pi = 1.0;

if (a)
{
float pi = 0.5;
bc.OutputToConsole(pi); <span>// print 0.5</span>
}

bc.OutputToConsole(pi); <span>// print 1.0</span>
}
</pre></pre>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics