srcnalt / OpenAI-Unity

An unofficial OpenAI Unity Package that aims to help you use OpenAI API directly in Unity Game engine.
MIT License
672 stars 150 forks source link

Invalid expression term on Import #7

Closed Neraull closed 1 year ago

Neraull commented 1 year ago

hi, i've just imported the package into unity (2019.4.38f1c1 LTS) on windows and it immediately gives me the following error:

Library\PackageCache\com.srcnalt.openai-unity@f06833f4ff\Runtime\OpenAIApi.cs(19,64): error CS1525: Invalid expression term '='

it seems to refer to the line: private Configuration Configuration => configuration ??= new Configuration();

Would you happen to know what the problem could be?

Thanks!

srcnalt commented 1 year ago

Hi @Neraull, this is a null-coalescing operator with the inline property. I use a newer C# compiler I suppose that must be the issue. Will make sure I am writing older and more common version. You can replace this line with

private Configuration Configuration
{
    get
    {
        if (configuration == null)
        {
            configuration = new Configuration();
        }
        return configuration;
    }
}
Neraull commented 1 year ago

Hi Srcnalt, thanks for the quick response!

i replaced the line with the block of code above, and there are cascading errors that pop up in other classes. lol

it first showed me that it couldn't make line 5 in IRsponse a public: public ApiError Error { get; set; }

then after fixing that, it now tells me: Assets\Samples\OpenAI Unity\0.1.2\DallE\DallE.cs(35,23): error CS1003: Syntax error, '(' expected

I'm assuming it's an issue on my end with an older compiler?

Thanks for the help.

Neraull commented 1 year ago

attaching images of what i'm seeing.

image

image

srcnalt commented 1 year ago

So seems like the issues are:

I suppose I should reduce my C# version and have another pass on the whole code.

Neraull commented 1 year ago

ahh ok. so it's more different compiler versions causing these issues. Should i just wait for your next version then?

srcnalt commented 1 year ago

Updated the code to be C# 6 compatible and made a release #8 v0.1.3 should be working fine. Could you update the package in Unity Package Manager and let me know if all works fine?

Neraull commented 1 year ago

Heya,

I reimported the package again and it seems fine until i import the DallE sample.

then the following error pops up:

image

image

srcnalt commented 1 year ago

Sorry forgot this one, you can wrap the content of the if block in there in a using block.

using(var request = new UnityWebRequest(response.Data[0].Url))
{
  request.downloadHandler = new DownloadHandlerBuffer();
  request.SendWebRequest();

  while (!request.isDone) await Task.Yield();

  Texture2D texture = new Texture2D(2, 2);
  texture.LoadImage(request.downloadHandler.data);
  var sprite = Sprite.Create(texture, new Rect(0, 0, 256, 256), Vector2.zero, 1f);
  image.sprite = sprite;
}
Neraull commented 1 year ago

awesome! completely works now for testing and base functions. Thank you so much, you're awesome for doing this.