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.
21b436efb52d4253807c81410c1ee96a.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

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

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

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

111f35f2cab6499990a3e618bf0d4152.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

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
}

and finally the result is as expected

293e7fea5872412b81b0be1eae84bd93.png

YAY :-p

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.

1 Like

this topic is still relevant, please help

don’t thank me

<?php
include("db_vars.php");

// Create a connection to the database
$link = new mysqli ($dbhost, $dbusername, $dbpassword, $dbname);

// Check connection to database
if ($mysqli->connect_error) 
{
    echo(json_encode(array('status'=>"Connection failed: " . $mysqli->connect_error)));
	die;
} 
$resultset = mysqli_query($link, "SELECT ServerName, ServerStatus FROM statusCheck ORDER by ID");

while($row = mysqli_fetch_assoc($resultset)){

	$json_array[] = $row;

}
		

	echo json_encode(array('ServerQueryResults'=>$json_array));
  /* close the statement */
    mysqli_stmt_close($stmt);


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