Board index FlightGear Support Interfacing

How to set http values through interfacing with unity3d?

Connecting two computers, using generic protocol, connecting with Matlab?

How to set http values through interfacing with unity3d?

Postby starvi » Mon Jun 14, 2021 12:44 pm

hey everyone,

by following this youtube tutorial, i am able to get values from flightgear through http interfacing. however, while following the unity document to set, the values does not update in flightgear.
the webrequest returns success and no exception caught but the value remains the same in flightgear.

the command i'm trying is "position/altitude-ft". i tried setting the value on the browser and it immediately reflects in flightgear, thus i'm certain that the variable can be set but i'm not sure why the set is not working.

will be great if anyone can let me know if its the wrong command or if there is something missing from my code.

any help appreciated.

Code: Select all
public async void Set<T>(string command, T val)
{
        using UnityWebRequest webRequest = UnityWebRequest.Post("localhost:5050/json/" + command,  val.ToString());

        // request and wait for to connect to the server
        UnityWebRequestAsyncOperation operation = webRequest.SendWebRequest();
        while (!operation.isDone)
        {
            await Task.Yield();
        }

        // if fail
        if (webRequest.result != UnityWebRequest.Result.Success)
            this.Log($"Error: {webRequest.error}", Color.red, false);
}


edit: added the url for connecting
Last edited by starvi on Tue Jun 15, 2021 3:41 am, edited 1 time in total.
starvi
 
Posts: 12
Joined: Fri May 21, 2021 5:13 am

Re: How to set http values through interfacing with unity3d?

Postby Hooray » Mon Jun 14, 2021 8:26 pm

you might want to share with us what exactly you're trying to do here.
The background being that you cannot simply modify certain property values via external means, since they're internally managed/updated (rewritten).
For instance, /position will be typically updated by the FDM.

Thus, if you're trying to reposition the aircraft, you will have to use a different protocol/mechanism (presets and/or fgcommands), or use an external FDM mechanism instead (which entails disabling the internal one).

Thus, please share with us what exactly you are trying to do, or we will have to do lots of guesswork here :D
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: How to set http values through interfacing with unity3d?

Postby starvi » Tue Jun 15, 2021 4:07 am

hey hooray,

i'm trying to interface with flightgear through the webserver (http) such that i am able to create a simulation while controlling flightgear through unity3d.

i am able to get the values through the json as follow:
Code: Select all
public async Task<T> Get<T>(string command)
{
   using UnityWebRequest webRequest = UnityWebRequest.Get("localhost:5000/json/" + command);

   // request and wait for to connect to the server
   UnityWebRequestAsyncOperation operation = webRequest.SendWebRequest();
   while (!operation.isDone)
   {
      await Task.Yield();
   }

   // if fail
   if (webRequest.result != UnityWebRequest.Result.Success)
   {
      this.Log($"Error: {webRequest.error}", Color.red);
      return default;
   }
   
   JObject result = JObject.Parse(webRequest.downloadHandler.text);
   return result["value"].ToObject<T>();
}


but when i try to do the opposite, which is to set the values, nothing happens through my code.
the setting of position is just a test, though i believe it should be allowed as i can set the position through the web browser

edit: just came across this
A simple web service provides read-only access to the property tree. This web service can be reached at the URL http://localhost:port/json
, from here so i am certain the setter previously will not work.

i tried using
Code: Select all
using UnityWebRequest webRequest = UnityWebRequest.Put("localhost:5000/#Simulator/Properties/sim/yokes-visible", System.Text.Encoding.UTF8.GetBytes("false");

but it returns "Error: HTTP/1.1 401 Unauthorized" instead.
starvi
 
Posts: 12
Joined: Fri May 21, 2021 5:13 am


Return to Interfacing

Who is online

Users browsing this forum: No registered users and 2 guests