2,158
edits
m (Clarify when this can be used.) |
mNo edit summary |
||
(5 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
{{Modding Info}} | {{Modding Info}}{{Modding Topic Prerequisites | Modding:C Sharp Scripting}} | ||
== Add Code to Player on a New Game == | |||
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. | ||
Line 22: | Line 20: | ||
public void mutate(GameObject player) | public void mutate(GameObject player) | ||
{ | { | ||
// modify the player object | // 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:Script Modding]] |
edits