Facepunch / sbox-issues

178 stars 12 forks source link

Make ActionGraph system accessible in code #5004

Open youarereadingthis opened 9 months ago

youarereadingthis commented 9 months ago

For?

S&Box

What can't you do?

Generate ActionGraphs at runtime using existing and custom nodes, basically.

How would you like it to work?

Here is a flawed but hopefully decent enough example of how it might look.

var ag = new ActionGraph("Custom Graph");

var allNodes = ActionGraphNode.All;

if (allNodes.TryGetValue("S&box Issue", out var nodeInfo))
{
  var properties = new Dictionary<string, object>()
  {
    ["Request"] = SboxIssueRequest.Accepted
  };

  var node = nodeInfo.Create() { Properties = properties };
  node.Properties["OnClosed"] = new ActionGraphNode.Find("Play Sound").Create().Run;

  ag.AddNode(node);

  Components.GetOrCreate<StartActionComponent>().Action = ag.Compile();
}

What have you tried?

Decompiling classes and realizing that it's not public.

Additional context

I am working on a level editor and have my own logic system, and while it is simpler to use it is also much more limited than ActionGraph.

I thought about remaking ActionGraph, but.. why? It's already there just out of reach.

Metapyziks commented 9 months ago

Ideally you could just use ActionGraph and its editor inside your level editor, without needing this. Could you talk a bit more about your use case?

youarereadingthis commented 9 months ago

Ideally you could just use ActionGraph and its editor inside your level editor, without needing this. Could you talk a bit more about your use case?

My level editor is custom. It's a thing that you can use in the release version of the game. It allows players to make their own levels using tools, widgets, and a logic system without having the project files. Here is an image. image

As such, I cannot use the UI unique to the editor version of the S&box, and there is no way(at least that I can find) to implement ActionGraph in code so that I can make my own UI/system for it and generate ActionGraphs at runtime.

Metapyziks commented 9 months ago

Thanks for talking through your setup. This'll have to be quite low priority for now because we'll need to be careful about what we expose, to avoid people getting around our access control. However, if you're targeting a standalone release I think we're planning to skip access control entirely, so you could directly reference Facepunch.ActionGraphs.

For now, if the graphs you would generate are relatively simple, you could directly generate the JSON representing an ActionGraph. I think you'd then be able to parse it with:

var parsedDelegate = Sandbox.Json.Deserialize<ExampleDelegate>( actionGraphJson );

I'm sure you'll be able to figure out the format by looking at how graphs you build in our editor serialize, but feel free to ask if you decide to try this and have any questions.

youarereadingthis commented 9 months ago

Due to there being access control concerns, the low priority of my request, and my lack of plans for standalone/commercial release I've decided that if I'm going to implement a system like ActionGraph in my game(s) I might as well just make it from scratch with ActionGraph as a reference.

A simplified version is not prohibitively difficult for me, and anyone dedicated enough to do the same thing probably feels the same way, so feel free to close the issue if you see not enough point in doing this.