• Torque2D MIT 学习笔记(4) 脚本语法(2)


    数字

    TorqueScript支持的数字类型有四种:

    123        // 整型
    1.23       // 浮点
    123e-4  // 科学计数
    0xabcd  // 十六进制
     

    字符串

    文本,比如名字或者短语词组都可以作为字符串存储.

    数字也可以以字符串格式存储.

    标准的字符串存储在双引号标记区域内.如:

    "123abc"


    标记字符串存储在单引号标记区域内,如:

    '123abc'


    在TorqueScript中,标记字符串会被特殊对待,他们不仅有自己的字符串数据还有一个与之相关联的特殊数字标记.

    标记字符串通常被用在网络数据发送中,不管实际发送多少次字符串,其字符串数据只会被发送一次,其他的时间都是发送标记值.

    在使用的时候需要通过detag命令来解析内容,通常我们用不到标记字符串,除非有很多字符串要通过网络传递,比如聊天系统等等.如下:

    $a = 'This is a tagged string';
    echo("  Tagged string: ", $a);
    echo("Detagged string: ", detag('$a'));
    

    输出结果为:

       Tagged string: SOH1
    Detagged string:

    第二行为空值,除非字符串是通过网络传递到客户端的.

    字符串运算符

    基本语法: "string1" operation "string2"

    如同使用数学运算符一样,TorqueScript提供了四种操作符供程序猿使用.

    @     连接两个字符串
    TAB   使用制表符连接两个字符串
    SPC   使用空格连接两个字符串
    NL    新行
    
    比如:
    
    echo("Hello" @ "World");
    echo("Hello" TAB "World");
    echo("Hello" SPC "World");
    echo("Hello" NL "World");
    
    OUTPUT:
    HelloWorld
    Hello   World
    Hello World
    Hello
    World

    布尔型

    true (1)

    false (0)

    和其他许多编程语言一样,0和false被认为假,true和非零被认为真,常常被用来作为条件判断的开关,如下:

    $lightsOn = true;
    
    if($lightsOn)
      echo("Lights are turned on");
    

    数组

    数组结构被用来存储连贯的相同类型的数据,比如:

    $TestArray[n]   (一维数组)
    $TestArray[m,n] (二维数组)
    $TestArray[m_n] (二维数组)
    

    PS:数组下标从0开始.

    比如:

    $userNames[0] = "Heather";
    $userNames[1] = "Nikki";
    $userNames[2] = "Mich";
    
    echo($userNames[0]);
    echo($userNames[1]);
    echo($userNames[2]);
    

    向量

    在TorqueScript中"向量"(组合数据)是一种非常重要的类型,比如SceneObject就有许多的变量时2或3个数据的组合类型,比如位置,向量,颜色等.

    一次性赋值的时候以字符串的方式,单个数据之间用空格分隔,比如:

    %position = "25 32";
    %firstColor = "100 100 100 1.0";
    echo(%firstColor);
    
    


    脚本变量的组合方式:

    %red = 128;
    %blue = 255;
    %green = 64;
    %alpha = 1.0;
    
    %secondColor = %red SPC %blue SPC %green SPC %alpha;
    echo(%secondColor);
    

    运算符

    算数运算符:

    Operator Name Example Explanation
    * multiplication $a * $b Multiply $a and $b.
    / division $a / $b Divide $a by $b.
    % modulo $a % $b Remainder of $a divided by $b.
    + addition $a + $b Add $a and $b.
    - subtraction $a - $b Subtract $b from $a.
    ++ auto-increment (post-fix only) $a++ Increment $a. The value of $a++ is that of the incremented variable: auto-increment is post-fix in syntax, but pre-increment in sematics (the variable is incremented, before the return value is calculated). This behavior is unlike that of C and C++.
    -- auto-decrement (post-fix only) $b-- Decrement $b. The value of $a-- is that of the decremented variable: auto-decrement is post-fix in syntax, but pre-decrement in sematics (the variable is decremented, before the return value is calculated). This behavior is unlike that of C and C++.

     

    关系运算符:

    Operator Name Example Explanation
    `< Less than $a < $b 1 if $a is less than % b (0 otherwise.)
    `> More than $a > $b 1 if $a is greater than % b (0 otherwise.)
    `<= Less than or Equal to $a <= $b 1 if $a is less than or equal to % b (0 otherwise.)
    `>= More than or Equal to $a >= $b 1 if $a is greater than or equal to % b (0 otherwise.)
    `== Equal to $a == $b 1 if $a is equal to % b (0 otherwise.)
    `!= Not equal to $a != $b 1 if $a is not equal to % b (0 otherwise.)
    `! Logical NOT !$a 1 if $a is 0 (0 otherwise.)
    `&& Logical AND $a && $b 1 if $a and $b are both non-zero (0 otherwise.)
    $= String equal to $c $= $d 1 if $c equal to $d .
    !$= String not equal to $c !$= $d 1 if $c not equal to $d.

    还有一个额外的逻辑或操作, ||, 比如:

    $a || $b
    

    两者有一个非零,结果就为1,否则为0;

     

    位操作

    Operator Name Example Explanation
    ~ Bitwise complement ~$a flip bits 1 to 0 and 0 to 1. (i.e. ~10b == 01b)
    & Bitwise AND $a & $b composite of elements where bits in same position are 1. (i.e. 1b & 1b == 1b)
    ^ Bitwise XOR $a ^ $b composite of elements where bits in same position are opposite. (i.e. 100b & 101b == 001b)
    << Left Shift $a << 3 element shifted left by 3 and padded with zeros. (i.e. 11b << 3d == 11000b)
    >> Right Shift $a >> 3 element shifted right by 3 and padded with zeros. (i.e. 11010b >> 3d == 00011b)

    还有一个额外的位或操作, |, 比如:

    $a | $b


    赋值

    Operator Name Example Explanation
    Assignment $a = $b; Assign value of $b to $a. Note: the value of an assignment is the value being assigned, so $a = $b = $c is legal.
    op= Assignment Operators $a op= $b; Equivalent to $a = $a op $b, where op can be any of:
    * / % + - & ^ << >>  

  • 相关阅读:
    模块移植-加宏选择性编译
    模块-各个模块的路径所在
    ubuntu-系统卡慢解决(转载)
    meld文件的脚本
    artTemplate模板引擎
    前端完全分离和前端不完全分离
    px em rem的区别
    原型和原型链
    阻止默认事件
    document.ready和onload的区别
  • 原文地址:https://www.cnblogs.com/KevinYuen/p/2940357.html
Copyright © 2020-2023  润新知