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

Time for action – All classes are created equally

What does this mean for us in practical terms? Let's take a look at our friend AwesomeActor to see if he can help demonstrate.

  1. Write the following code in our AwesomeActor class:
    class AwesomeActor extends Actor
        placeable;
    
    var() int MyInt;
    
    function PostBeginPlay()
    {
        `log(self @ MyInt);
    }
    
    defaultproperties
    {
        MyInt=4
    
        Begin Object Class=SpriteComponent Name=Sprite
            Sprite=Texture2D'EditorResources.S_NavP'
            HiddenGame=True
        End Object
        Components.Add(Sprite)
    }

    We'll use an editable MyInt variable to see class behavior.

  2. Compile the class and open up the editor. Next to our existing AwesomeActor, place another one.
    Time for action – All classes are created equally
  3. Save the map and run it from the editor, then close out the game and editor.
  4. Now let's take a look at our Launch2.log file:
    [0010.61] ScriptLog: AwesomeActor_0 4
    [0010.61] ScriptLog: AwesomeActor_1 4

    We can see that class instances are created with the class name and then a number added to the end. Remembering that in programming everything starts with 0, in our case we have AwesomeActor_0 and AwesomeActor_1. Since we haven't changed anything about them yet, both have the default value of MyInt, 4.

  5. How do we know which one's which? A handy bar at the bottom of the editor can help us. Reopen the editor and click on one of the AwesomeActors to select it. At the bottom of the editor we can see that instance's name.
    Time for action – All classes are created equally
  6. From our log we can see that each instance of AwesomeActor has been created in the same manner. Now let's change one of them. Double-click on one to open up its properties, and change MyInt to 23.
    Time for action – All classes are created equally
  7. Save the map and run the game, then exit and take a look at Launch2.log.
    [0007.76] ScriptLog: AwesomeActor_0 4
    [0007.76] ScriptLog: AwesomeActor_1 23

What just happened?

We can see that even though both instances were created from the same class blueprint, we can change them after they have been created. This is an important principle of object-oriented programming. Having classes lets us quickly create objects of the same type without having to have a separate file or code for each instance we're going to use. An object is created from the class with all of that class' properties, and then we can change them after they have been created.

主站蜘蛛池模板: 米林县| 巴中市| 庆云县| 裕民县| 岳普湖县| 民丰县| 平罗县| 崇信县| 荃湾区| 余庆县| 驻马店市| 四川省| 汤阴县| 嘉定区| 鸡泽县| 永仁县| 阳信县| 侯马市| 鹿泉市| 洞口县| 蓬安县| 安平县| 金沙县| 托克逊县| 达拉特旗| 宜章县| 榆林市| 东海县| 灵山县| 庆城县| 根河市| 新邵县| 雷波县| 宁夏| 江华| 武胜县| 澄城县| 尼玛县| 万山特区| 中超| 玉门市|