mestiez / ppg-snippets

a number of useful code snippets that should help users write People Playground mods
https://www.studiominus.nl/ppg-modding/
53 stars 113 forks source link

Custom Right Click option #25

Open An4r3w opened 2 years ago

An4r3w commented 2 years ago

Hello sorry for posting here again a question >.< but I really can't understand how to make a custom right click option for only my custom humans. Can someone help me?

mestiez commented 2 years ago

a PhysicalBehaviour has a list of custom context menu buttons. The following code shows how to add buttons. Do note that it only works on objects with a PhysicalBehaviour. The human root object doesn't have one so you'd need to run this script on every limb you want that button to be on.


//assuming Instance is the GameObject, this code will add a "Blow up" button 

var phys = Instance.GetComponent<PhysicalBehaviour>();

var button = new ContextMenuButton("uniqueIdentifierPleaseForGodSake", "Blow up", "Blow up the selection", () => 
{
   ExplosionCreator.Explode(Instance.transform.position, 8);
});

phys.ContextMenuOptions.Buttons.Add(button);

just for clarity, there are quite a lot of context menu button constructors:

    public ContextMenuButton(string identity, string label, string desc, params UnityAction[] actions)l

    public ContextMenuButton(string identity, Func<string> label, string desc, params UnityAction[] actions)l

    public ContextMenuButton(Func<bool> condition, string identity, Func<string> label, string desc, params UnityAction[] actions)l

    public ContextMenuButton(Func<bool> condition, string identity, string label, string desc, params UnityAction[] actions)l
An4r3w commented 2 years ago

Hello, thank you! I just do not understand how to run the script on every limb.