官术网_书友最值得收藏!

Time for action – Math!

  1. As an example, take a look at this code.
    var float Float1, Float2;
    var int Int1;
    
    function PostBeginPlay()
    {
        Float2 = Int1 / Float1;
        'log("Float2:" @ Float2);
    }
    
    defaultproperties
    {
        Int1=5
        Float1=2.0
    }
  2. We can divide an int by a float or vice versa, and we get the result we expect:
    [0008.10] ScriptLog: Float2: 2.5000

    However, if we divide an int by an int and assign it to a float, what would we expect the result to be?

  3. Let's take a look at this code:
    var float Float1;
    var int Int1, Int2;
    
    function PostBeginPlay()
    {
        Float1 = Int1 / Int2;
        'log("Float1:" @ Float1);
    }
    
    defaultproperties
    {
        Int1=5
        Int2=2
    }

    With that it looks like we'd expect the same result. Let's take a look at the log:

    [0007.66] ScriptLog: Float1: 2.0000

    When dividing ints, the truncating happens before assigning the result, even if it's a float. Depending on what we're doing this may be what we want, but it's good to keep that in mind.

  4. Two other operators that can be used for simple math are increment ( ++ ) and decrement ( ).
    Int1 = 5;
    Int1++;
    'log("Int1" @ Int1);

    This would give us 6 in the log.

  5. For vectors and rotators, the arithmetic works with each element of the struct individually. For example, with the following code:
    var vector Vect1, Vect2, VectResult;
    
    function PostBeginPlay()
    {
        VectResult = Vect1 + Vect2;
        'log("VectResult:" @ VectResult);
    }
    
    defaultproperties
    {
        Vect1=(X=1.0,Y=4.5,Z=12.0)
        Vect2=(X=2.0,Y=4.0,Z=8.0)
    }

    We get the following result in the log:

    [0007.74] ScriptLog: VectResult: 3.00,8.5,20.00

    As we can see, each individual element has been worked with separately. X added to X, Y to Y, and Z to Z.

  6. Vectors can also be multiplied or divided by floats and ints. This has the effect of changing the vector's VSize while keeping the direction the same.

What just happened?

The basic arithmetic operators are simple stuff, but when working with different types of variables it's important to remember how they'll respond to the operators.

Modulo

Modulo ( % ) returns the remainder after division. It's a pretty obscure and not commonly used operator, but when needed it can save many lines of code.

主站蜘蛛池模板: 安吉县| 三明市| 绵阳市| 隆回县| 隆昌县| 历史| 宣化县| 北辰区| 凤翔县| 阿拉善盟| 郧西县| 莲花县| 凌海市| 博野县| 瑞丽市| 钟山县| 盖州市| 聂拉木县| 始兴县| 万州区| 平原县| 东辽县| 南宁市| 津南区| 赣榆县| 迭部县| 汉川市| 于田县| 微山县| 读书| 新晃| 海城市| 鹤山市| 六盘水市| 平乐县| 阿克苏市| 伊春市| 博爱县| 青海省| 榆中县| 库尔勒市|