How to use custom Json data in Visual Script?

Hi, I’m new to Flax, previously using Unity. I’m playing around a bit with custom Json Assets in Visual Script and trying to use them similar to Unity Scriptable Objects.

I tried to replicate this code (something similar) from the ThirdPersonShooter sample in Visual Script, but it seems that this cannot be done in Visual Script.

How to use data from custom Json Asset in Visual Script?

Hello,
never used unity, but with flax engine you can use these two nodes: CreateInstance (from JsonAsset) and CastValue (from Tools).

Here are a couple of images

1 Like

A couple of notes while I was testing it, in case any flax devs can provide some information

  • Example working with Flax compiled from source today, but flax installed 2 o 3 days ago from laucher i get some native assertion failed error in CastValue node
  • The node Identity (in Quaternion) produces an error
[Error] Missing field 'Identity' in type 'FlaxEngine.Quaternion'
[Error] Visual Script stack trace:
[Error] at VS_Weapon:e5573c724bd4efa4a69262b8a15df185 in node 1048583
    at VS_Weapon:e5573c724bd4efa4a69262b8a15df185 in node 262170
    at VS_Weapon:e5573c724bd4efa4a69262b8a15df185 in node FlaxEngine.Actor.set_Transform()
    at VS_Weapon:e5573c724bd4efa4a69262b8a15df185 in node 1114116
    at VS_Weapon:e5573c724bd4efa4a69262b8a15df185 in node Set _timeToFire
    at VS_Weapon:e5573c724bd4efa4a69262b8a15df185 in node 1114113
    at VS_Weapon:e5573c724bd4efa4a69262b8a15df185 in node FlaxEngine.Input.GetAction()
    at VS_Weapon:e5573c724bd4efa4a69262b8a15df185 in node OnUpdate()

It’s very intresting. I have a question.

How do I set the _data parameter to null?
When I created a _data parameter with type JsonAsset, it was not possible to connect the output from Cast Value node to Set _data node.

I tried again, but set the _data parameter to WeaponData type and let the data from get _data + something data node from json asset to debug log node be written out. When I turned on Play mode, it said “Invalid ManagedHandle” in the console. When I turned off Play Mode, I got a Popup window with Error, Assertion Failed and the editor crashed. I am using version 1.6.6344


02

I tried writing some data from a Json Asset to Debug.Log in C# and it works without problems. This crash is only on Visual Script side.

I will do my best to try to answer as well as possible, if someone sees something is incorrect please say so.

About setting _data to null, use Set _data without passing any value to it.
image

In this example, _data must be of type WeaponData and have another reference to the json file, then deserialize the json to the data type needed.
The deserialization part is done by the CreateInstance node, in the c# part it is _data = WeaponSettings.CreateInstance<WeaponData>(), but in c# it is specified to return an object of type WeaponData, in Visual Script it returns T and does not know what type it is, so after CreateInstance it is necessary to perform a cast.

If you want to use debug log you can get the content directly from the json or serialize the object to get the string and pass it to debug log. I’m not sure if the error occurs in this part or not, if you can attach some image to see it it would be helpful.

About the error, something similar happened to me when using the Flax 1.6 option from the laucher with the CastValue node. I solved it by compiling flax from source, but it also works with the last option master branch (daily) from launcher.

I fixed this here: Fix missing C# static fields in native scripting api · FlaxEngine/FlaxEngine@0083ebd · GitHub (will be in then next master (daily) build)

1 Like

I used this Visual Script to check if the values from the _data parameter are correct and it also shows the same errors mentioned above and when I turn off play mode the editor also crashes.

I also added a public Weapon string to the WeaponData C# script and tried setting the _data->Weapon parameter directly in the ClassMembers->Parameters tab and had that string output to the console to see if it would pass and got the same result, same errors and editor crash. I used this visual script.

I also tested this scenario via C# and it works.

With the two Visual Script examples the error you are talking about occurs with the option 1.6 installed from launcher.

Installed Flax with the master branch option (daily) you should not have the same problem. If you can, I suggest to install it from master branch (daily) and check that the master branch version is set by default.

If you are already using a recent version of flax, compiled from source or master branch (daily) from launcher, then maybe someone from flax staff can check the error you are reporting.

Maybe, if you have installed flax with the master branch (daily) some time ago, if you can try to uninstall and install again.

I’ll try it via the master-branch. Thanks for answers, good luck :slight_smile:

For some reason I can’t install the master-branch version in the Flax launcher window, I ran Flax as administrator and nothing, it won’t even download. I do not know why. What’s happened here?

I could not tell you what the problem is, I can only comment what sometimes happens to me, maybe it can be related.

The download progress bar is always stuck at 0
image

But in the background it is downloading. Depending on the connection speed, I understand that the download time will be longer or shorter.

In my case it take less than a minut to download, after that it start the installation process
image

I finally managed to install Master-branch via Launcher. The problem was probably a slow internet connection.

I tested all the above scenarios there and everything works without problems, no editor crashes. Thanks for help :slight_smile: