banner
YZ

周周的Wiki

种一棵树最好的时间是十年前,其次是现在。
zhihu
github
csdn

UE5 Blueprint Practical Guide: VaRest Plugin Application - From JSON File to Infinite List

In this article, I will demonstrate how to use the VaRest plugin in UE5 to read JSON files and display the data in an infinite scrolling list. VaRest is a powerful open-source plugin that makes HTTP requests and JSON data processing in UE5 simple and intuitive.

Installing and Enabling the VaRest Plugin#

First, you need to search for the VaRest keyword in the Unreal Marketplace, find the plugin, click the "Free" button, then switch to the Library, and install VaRest to the required project engine from the vault.

Finally, you need to enable the use of this plugin in the engine by clicking Edit -- Plugins -- Search and check VaRest -- Restart the engine in the menu bar.

image.png

Creating a JSON File and Building an Infinite List#

  1. Use an online JSON editor to simulate JSON data

Create a new Data folder in the project's Content directory, and create a text file named Info.json. Copy and paste the simulated data from the online JSON editor into Info.json.
image.png

  1. Build the Infinite List UI
    We need to create a UI to display the JSON data.

First, create an infinite list to display items: name it WBP_StudentPanel, add a ListView control, and click the + sign under the list record bar to create a new EntryWidgetClass blueprint for displaying individual student information, naming it WBP_StudentItem. As shown below:

image.png
image.png

Blueprint Node Connection#

Defining Data Structures#

Here, we also need to define two basic data structures for data parsing and display. Create a new structure blueprint named S_StudentInfo, defining four variables that match the previous JSON data: name, number, height, and weight for constructing the data structure after parsing the JSON data; create a blueprint class that inherits from the Object class, named O_StudentInfo. Define a variable of the type of the structure just defined: S_StudentInfo to display the data structure in the list UI. As shown in the two basic blueprint classes below:
image.png

Improving the Test Blueprint Logic#

Initial Display of Infinite List UI#

In a camera control blueprint class, connect the BeginPlay node to the Create Widget node, selecting WBP_StudentPanel for the Class pin, and connect the execution pin to Add to Viewport. As shown below:
image.png

Pressing the Number 0 Key to Parse JSON and Display it in the List#

Here, we will use the API provided by the VaRest plugin, so make sure the VaRest plugin is enabled beforehand.

  1. Blueprint Logic for Parsing JSON

First, use the Load Json from File function to parse the JSON file relative to the Content directory, so fill in the Path pin with Data/Info.json. Then use the Get Array Field method to parse the data under the data node, constructing a Student Data JSON, as shown below:
image.png

Then, loop through this JSON array with a for loop, parsing each element individually. Here, use the Get String Field method to parse name, number, height, and weight separately, then construct S_StudentInfo and add it to the StudentInfoList array. Finally, define an event dispatcher ParseStudentInfoCompleted, which will be called after parsing is complete to pass the StudentInfoList out. As shown below:
image.png

  1. Binding the Event Dispatcher ParseStudentInfoCompleted

In WBP_StudentPanel, pull a wire from Event Construct to get a reference to the camera controller, bind the event dispatcher, and then use the Create Event node to create an event trigger function ParseStudentInfoCompletedCallBack.
image.png

Improve the function ParseStudentInfoCompletedCallBack:

Define a variable StudentInfos, of type O_StudentInfo. Iterate through the passed structure array, constructing O_StudentInfo for each individual element. Use the Add item node to add to StudentListView (which is an infinite list ListView control), and after completion, set the ListView's items using SetListItems (this is very important, as it will trigger subsequent update interface events):
image.png

  1. Displaying Data in Individual List Elements

A key step here is to implement an interface in the WBP_StudentItem blueprint: User Object List Entry

Then, simply implement the On List Item Object Set event, converting the passed Object object to O_StudentInfo and obtaining a reference to the structure S_StudentInfo, finally assigning values to the Text components in sequence.
image.png

Running Test Results#

Click Play to run, press the keyboard number key 0, and the infinite list successfully displays the JSON data:
image.png

This ListView is really useful for massive data.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.