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

Time for action – Using structs

Going back to our basket of kittens example, what if there were other things in the basket besides kittens? How would we represent them?

  1. Let's create a struct at the top of our AwesomeActor class and put a few things in it.
    struct Basket
    {
        var string BasketColor;
        var int NumberOfKittens, BallsOfString;
        var float BasketSize;
    };

    Now we have two types of items in the basket as well as some variables to describe the basket itself.

  2. Now we need a variable of that struct so we can use it:
    var Basket MyBasket;
  3. Now we can change the values in our PostBeginPlay function.
    function PostBeginPlay()
    {
        MyBasket.NumberOfKittens = 4;
        MyBasket.BasketColor = "Yellow";
        MyBasket.BasketSize = 12.0;
        MyBasket.BallsOfString = 2;}

    That seems easy enough to handle. Let's try something a bit more complex.

  4. I heard you like structs, so we'll Inception-ize it by adding a struct inside a struct.
    struct SmallBox
    {
        var int Chocolates;
        var int Cookies;
    };
  5. Now let's put a box inside our basket struct.
    struct Basket
    {
        var SmallBox TheBox;
        var string BasketColor;
        var int NumberOfKittens, BallsOfString;
        var float BasketSize;
    };

    Now our class should look like this:

    class AwesomeActor extends Actor
        placeable;
    
    struct SmallBox
    {
        var int Chocolates;
        var int Cookies;
    };
    
    struct Basket
    {
        var SmallBox TheBox;
        var string BasketColor;
        var int NumberOfKittens, BallsOfString;
        var float BasketSize;
    };
    
    var Basket MyBasket;
    
    function PostBeginPlay()
    {
        MyBasket.NumberOfKittens = 4;
        MyBasket.BasketColor = "Yellow";
        MyBasket.BasketSize = 12.0;
        MyBasket.BallsOfString = 2;
    }
    
    defaultproperties
    {
        Begin Object Class=SpriteComponent Name=Sprite
            Sprite=Texture2D'EditorResources.S_NavP'
            HiddenGame=True
        End Object
        Components.Add(Sprite)
    }
  6. Now we're getting somewhere. How would we access the struct inside a struct? Using the same method as before, we would access the box like this:
    MyBasket.TheBox

    So accessing variables inside the box struct would look like this:

    MyBasket.TheBox.Chocolates = 2;
  7. Easy enough. It would work the same way with arrays. Let's say we added a static array of fortune cookie messages to our basket. Our new struct would look something like this:
    struct Basket
    {
        var string Fortunes[4];
        var SmallBox TheBox;
        var string BasketColor;
        var int NumberOfKittens, BallsOfString;
        var float BasketSize;
    };
  8. To assign a value to one of them, we would use the same method as before:
    MyBasket.Fortunes[2] = "Now you're programming!";

What just happened?

Structs are a powerful tool in UnrealScript, and they're used extensively in the source code. A lot of the code you'll make will involve structs. As an example, think about the one thing that every actor in the game has: A location. How is an actor's location stored? A variable appropriately called Location, which is declared as a 3D position using a Vector variable. If we look in Actor.uc, we can see how a Vector is defined:

struct immutable Vector
{
   var() float X, Y, Z;
};

This leads nicely into our next topic...

Vectors

Simply put, a vector is a 3D coordinate. It may be used to represent an actor's location, velocity, or even the angle of a surface. They're used a lot in UnrealScript, so even though they're just a struct, there are a lot of things we can do with them on their own. Let's take a look.

主站蜘蛛池模板: 高陵县| 特克斯县| 广河县| 曲靖市| 涞水县| 凤庆县| 保定市| 苏尼特右旗| 浏阳市| 咸宁市| 水城县| 乐业县| 名山县| 图木舒克市| 泽州县| 黑河市| 黄大仙区| 历史| 三原县| 璧山县| 高密市| 乐平市| 绍兴市| 大庆市| 塘沽区| 嵩明县| 泌阳县| 洛扎县| 眉山市| 莒南县| 新源县| 太和县| 当雄县| 彩票| 唐河县| 石门县| 石城县| 雅安市| 广平县| 浙江省| 吉首市|