beeglebug / behave-flow

a ui for behave-graph using react-flow
MIT License
76 stars 10 forks source link

Support for variables #15

Open bhouston opened 2 years ago

bhouston commented 2 years ago

I've added explicit support for creating typed variables with default values. Right now variables are discarded when loaded into behave-flow.

There are a number of examples here: https://github.com/bhouston/behave-graph/tree/main/src/graphs/core/variables

beeglebug commented 2 years ago

image

I've made a start on this, but have a question. In the image above, there is a float variable, but the "variable" input sockets on the Set and Get nodes both have a valueType of "id", which is causing an issue.

At the moment, sockets can only be connected when valueTypes match (float -> float, flow -> flow etc), from a user perspective, it would make sense for a float variable to only be able to connect to matching nodes, but the data for that decision is not self contained on the socket spec.

I could potentially add special logic which, when it encounters an "id" variable type, it looks at the other non-flow sockets on the node for a valueType, in this case it would find "float".

beeglebug commented 2 years ago

Thinking about this some more, i think its right that "id" is its own valueType so that variables can only be connected to variable slots (similar to flow sockets), perhaps I just need to add some custom logic at the point a connection is attempted to check the variable type also matches.

This still leaves the question of how to determine the type, at the moment I think its only get/set nodes which have variable sockets, in which case the correct valueType is whatever the value of the sole non-flow non-variable socket is, but is this universally true?

bhouston commented 1 year ago

I came up with a different solution by looking at Unreal Engine's blueprints. What they do is that after you have "declared" variables by adding variable entries to their list of variables, it create new nodes that include basically the name of the variable in their name.

Basically if you declare a "count" variable in a blueprint, you can now create nodes of type "get count" and "set count" in which the value socket is of the same value type of "count."

I've implemented this pattern in the behave-graph library recently. So we can mimic it.

The node names in the JSON would be "variable/set/${variable.id}" and "variable/get/${variable.id}".

I've made these two functions that dynamically create these nodes:

https://github.com/bhouston/behave-graph/blob/main/src/lib/Profiles/Core/Variables/VariableSet.ts#L9 https://github.com/bhouston/behave-graph/blob/main/src/lib/Profiles/Core/Variables/VariableGet.ts#L8