Download

HELP with VaRest Plugin and getting a json responce

PROBLEM SOLVED SEE LAST POST IF YOU ARE INTERESTED IN THE ANSWER . THANKS

k i deleted my other thread because i think i didn’t explain myself clearly and people were getting confused when they were trying to answer me.

so what i am trying to do is query my statusCheck database, which contains all the information stored about my servers and then send the result of this query back to unreal via json so that i can then use this data to drive my blueprint logic and then also use this data to populate a UMG widget that will show the clients all the server information

this is my database at the moment it is very basic for testing.
a6874a36619f057daae325049d012f228f660ccb.png

So for this example i want to query the database and return the status of just 1 server (Lobby) and see if it is online or not. so i wrote my php script like this


<?php

// Create connection
$link = new mysqli ($servername, $username, $password, $dbname);

// Check connection
if ($mysqli->connect_error) 
{
    echo(json_encode(array('status'=>"Connection failed: " . $mysqli->connect_error)));
	die;
} 

// select Lobby Status from the statusCheck database 
$query = "SELECT ServerStatus FROM statusCheck WHERE ServerName ='Lobby'";  

if ($stmt = mysqli_prepare($link,$query))
{
	//execute statment
	
	mysqli_stmt_execute($stmt);
	
	//bind variables
	
	mysqli_stmt_bind_result($stmt, $ServerStatus);
	
	//fetch values


	while (mysqli_stmt_fetch($stmt))
		
		{
			echo json_encode(array('ServerStatus'=>$ServerStatus));
		}
}
		
  /* close statement */
    mysqli_stmt_close($stmt);


/* close connection */
mysqli_close($link);
		
?>

now this works perfectly , when i run the script i get the following json

{“ServerStatus”:“offline”}

so i know that in this case the lobby server is offline because i specifically selected the Lobby server in the query.

inside unreal i setup a test to see if it works like so

0acd2357eada4b34b47f72bca6753cd5.png

and as expected the result is

b1b1b397a9a5497fbec30a12e57bb796.png

now the problem is when i alter my query to try and return **ALL **the results from the database. it doesn’t work.

php code is now


<?php

// Create connection
$link = new mysqli ($servername, $username, $password, $dbname);

// Check connection
if ($mysqli->connect_error) 
{
    echo(json_encode(array('status'=>"Connection failed: " . $mysqli->connect_error)));
	die;
} 

// select All ServerNames && ServerStatus from the statusCheck database and sort them by ID order 
$query = "SELECT ServerName, ServerStatus FROM statusCheck ORDER by ID";  

if ($stmt = mysqli_prepare($link,$query))
{
	//execute statment
	
	mysqli_stmt_execute($stmt);
	
	//bind variables
	
	mysqli_stmt_bind_result($stmt, $ServerName, $ServerStatus);
	
	//fetch values


	while (mysqli_stmt_fetch($stmt))
		
		{
			echo json_encode(array('ServerName'=>$ServerName, 'ServerStatus'=>$ServerStatus));
		}
}
		
  /* close statement */
    mysqli_stmt_close($stmt);


/* close connection */
mysqli_close($link);
		
?>

which outputs the following json

{“ServerName”:“Lobby”,“ServerStatus”:“offline”}{“ServerName”:“Server1”,“ServerStatus”:“online”}

but it is not valid json and so wont work in unreal.

so does anyone know how i would solve this problem so that i can do logic like this

d3b7a087acf85d2c45b1b6b94ea6d440c9231561.png

so here you can see the logic im trying to do is

if (serverName == Lobby && ServerStatus == offline)
{
// call custom function to do stuff
}

of course i would like to do this for all of my servers.

So please if anyone can help on how to do this i would be very happy.

at the moment the work around i have is to have a separate PHP script for every single server and just query them one at a time, which is not too bad as the database is only little but obviously this would soon become a nightmare if i had loads of servers.

Im sure it is possible i just need a little help figuring out how .

I think I need to put the results of the query into an array and then echo the array

but not sure how to then put each result into a field so that i can still use the field nodes in unreal as these seem to only take a single value??

So I end up with something like this

Row 1 in database query result is
Field ServerName = Lobby
Field ServerStatus = offline

Row 2 in database query result is
Field ServerName = Server1
Field ServerStatus = offline

Etc
thankyou

up untill last week i had never used php or json or varest before so all of this stuff was a massive headache for me

After days of frustration, reading documentation, code api’s, following every single php tutorial i could find, reading every varest thread i could find and non stop experimenting, i finally figured it out.

FIRST problem was getting a Valid Json response to work with inside unreal.

here is my script i have commented it to help others if they need it

what i wrote here just consists of general programming knowledge eg variables, arrays etc and then the php mysqli api.
Which can be found here

http://php.net/manual/en/book.mysqli.php

as i have said i have only just started learning this stuff so if anyone spots any mistakes or knows a better way to do things or any comments or suggestions on how i can improve the script then please let me know.




<?php

// Create a connection to the database
$link = new mysqli ($servername, $username, $password, $dbname);

// Check connection to database
if ($mysqli->connect_error) 
{
    echo(json_encode(array('status'=>"Connection failed: " . $mysqli->connect_error)));
	die;
} 

/* here is where i am performing the query against the database to find the data i want 
select All ServerNames && ServerStatus from the statusCheck database and sort them by ID order*/ 

$query = "SELECT ServerName, ServerStatus FROM statusCheck ORDER by ID";  

//prepare the query statement
if ($stmt = mysqli_prepare($link,$query))
{

	//execute statment
	
	mysqli_stmt_execute($stmt);
	
	//bind the results of the query into variables
	
	mysqli_stmt_bind_result($stmt, $ServerName, $ServerStatus);
	
	//fetch the result values

        // here i am looping through the results as there is more than one row
	while (mysqli_stmt_fetch($stmt))
		
		{
				
                        // here i am creating an array to hold all the results of the query which is an array itself 
			$serverStatusArray] =array('serverName'=>$ServerName, 'serverStatus'=>$ServerStatus);
		
		}

                // finally we send the $serverStatusArray back to unreal and encode it as json
		echo json_encode(array('ServerQueryResults'=>$serverStatusArray));
}
		
  /* close the statement */
    mysqli_stmt_close($stmt);


/* close the database connection */
mysqli_close($link);
		
?>

finally i ran the script and got the following response

1ca92f7753ce9cc88f4c17d7ee1f0de4eccef9f6.png

so problem one is solved i now have valid json to work with

the trouble is i had absolutly no idea what to do with it once inside unreal. All the examples i could find in the threads and even the varest documentation were very basic.

in the end i did it like this

as we were returning an array of json objects i used the get object array field node and put my array name as the Field name . like so

786bfffdac642043b89595089cdce87cb42bd8b3.png

now the next problem is i have an array of json objects but i dont need them all grouped together so i simply used the get nodes to break them apart from the array into their own seperate json objects. (of course you can use a loop or other method)
as i knew that the array only contained 2 rows of data i just used get 0, and get 1 like so

bcfed78434375b803787298faf2f9d077b6633e1.png

now that i had the seperate json objects to work with i could then do what i had originally intended and use the data to branch out into some logic

if (serverName == Lobby && ServerStatus == offline)
{
// print string
}

3853123eedb94f978e5046b585c38e3c.png

and finally the result is as expected

293e7fea5872412b81b0be1eae84bd93.png

YAY :-p

Hello how would you do this with a foreachloop for each json object?

Man you made my day. I was doing exactly the same. Thank you very much.

Hey!

I want to do similar stuff but I fail at getting a Va Rest Request JSON object that I could read for more detail.

I use a node js server that communicates with the unreal project in udp packages, and I use the On Received Bytes (UDP) node to get the packages.

The only problem is that it gives me a byte array, which I don’t know how to convert to a valid format to be able to work with. The warning says that it gets a SIOJson Object which is always faild to convert to Va Rest Request JSON.

I see that you use a custom event (result) to get your JSON object. How could I do the same? I’m completely new to UE and Blueprints and all that stuff.

Thank you very much for your help in advance. I hope you still check this thread even after 3 years.