Modding:Adding Code to the Player: Difference between revisions

m
no edit summary
mNo edit summary
 
(6 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Modding Info}}
{{Modding Info}}{{Modding Topic Prerequisites | Modding:C Sharp Scripting}}


{{ambox
== Add Code to Player on a New Game ==
|type=This article presupposes some knowledge of [[Modding/C Sharp Scripting|C# scripting]].
|format=tiny}}


You can use the <code>[PlayerMutator]</code> attribute and <code>IPlayerMutator</code> interface to modify the player object before the game begins, immediately after the player <code>GameObject</code> is first created.
You can use the <code>[PlayerMutator]</code> attribute and <code>IPlayerMutator</code> interface to modify the player object before the game begins, immediately after the player <code>GameObject</code> is first created.
'''This method works only when the player starts a New Game.''' If a player loads your mod on an existing save, <code>[PlayerMutator]</code> code is never called.


This structure allows you to run custom code on the player object. For example, you might add custom parts to the player from your mod, which is otherwise very difficult to do.
This structure allows you to run custom code on the player object. For example, you might add custom parts to the player from your mod, which is otherwise very difficult to do.
Line 20: Line 20:
       public void mutate(GameObject player)
       public void mutate(GameObject player)
       {
       {
           // modify the player object before the game starts
           // modify the player object when a New Game begins
          // for example, add a custom part to the player:
          player.AddPart<MyCustomPart>();
       }
       }
   }
   }
</syntaxhighlight>
== Add Code to Player on a Save Load ==
You can use the <code>[HasCallAfterGameLoadedAttribute]</code> and <code>[CallAfterGameLoadedAttribute]</code> attributes to modify the player object whenever a save game is loaded. This method can be useful if you want your mod changes to apply even to existing save games (rather than just new games).
Note that this method works '''only''' on loaded saves - if you want your code to also run on a new game, you must combine this method with the PlayerMutator method described above.
<syntaxhighlight lang="csharp">
using XRL; // for HasCallAfterGameLoadedAttribute and CallAfterGameLoadedAttribute
using XRL.Core; // for XRLCore
using XRL.World; // for GameObject
 
[HasCallAfterGameLoadedAttribute]
public class MyLoadGameHandler
{
    [CallAfterGameLoadedAttribute]
    public static void MyLoadGameCallback()
    {
        // Called whenever loading a save game
        GameObject player = XRLCore.Core?.Game?.Player?.Body;
        if (player != null)
        {
            player.RequirePart<MyCustomPart>(); //RequirePart will add the part only if the player doesn't already have it. This ensures your part only gets added once, even after multiple save loads.
        }
    }
}
</syntaxhighlight>
</syntaxhighlight>


{{Modding Navbox}}
{{Modding Navbox}}


[[Category:Modding]][[Category:Guides]]
[[Category:Script Modding]]
2,158

edits