Page 1 of 1

Events and SDLDotNet

PostPosted: Tue Mar 18, 2008 10:09 am
by Kaerigan
Hello there,

first I want to say that I think Cobra looks like a fantastic language, as it combines all my favourite features from other languages.

Now, onto the problem:

I'm trying to create a small game, Tic Tac Toe to be exact, and for this I have chosen SdlDotNet. In C# I've managed to make a window pop up, which you can close (I know, extraordinarily cool, huh?), but I can't seem to get this to work in Cobra. I do manage to get a window to pop up, but I can't close it or use any other events. Here's my code:

Code: Select all
        use SdlDotNet.Core
        use SdlDotNet.Graphics
   class App
      def main is shared
         App().go
      
      def init
         Video.setVideoMode(640, 480)
         Video.windowCaption = "Hello from SdlDotNet and Cobra"
         
      def go
         listen Events.quit, ref .quit
         Events.run
         
      def quit(sender as Object, args as EventArgs)
         Events.quitApplication


Which doesn't work, I get the error 'Cannot access instance member "quit" on type "Events". Make the member "shared" or create an instance of the type.'

Here's my C# code (which works):

Code: Select all
using SdlDotNet.Core;
using SdlDotNet.Graphics;

public class Application {
   
      [System.STAThread]
      public static void Main() {
         (new Application()).Go();
      }
      
      public Application() {
         Video.SetVideoMode(640, 480);
         Video.WindowCaption = "Tic Tac Toe";
      }
      
      public void Go() {
         Events.Quit += new System.EventHandler<QuitEventArgs>(this.Quit);
         Events.Run();
      }
      
      private void Quit(object sender, QuitEventArgs e) {
         Events.QuitApplication();
      }
      
   }


Now, I've tried creating an instance of Events as the error instructed me to, but that does not work. I don't understand what it means by "making it shared", if it means making it static, isn't it already? I haven't created any instance in the C# example? Any tips on what I should do? I don't even know if I'm using the "listen" correctly.

Re: Events and SDLDotNet

PostPosted: Tue Mar 18, 2008 10:17 am
by Charles
Can you try with the latest source instead of the last release?

http://cobra-language.com/source/

The source is easy to build. You don't have to be familiar with the internals and no third party build tool is required.

Re: Events and SDLDotNet

PostPosted: Tue Mar 18, 2008 10:21 am
by Kaerigan
Oh, I'm sorry, I probably should've told you, I am indeed using the latest source, I figured I should try it instead of the release before I wrote here, but the error is identical in both versions. And yes, the source was indeed easy to build.

Re: Events and SDLDotNet

PostPosted: Tue Mar 18, 2008 11:41 am
by Charles
In that case my guess is that Cobra is not picking up on the static/shared of events when it reads DLLs. But I haven't looked yet and it would be something else. In the meantime, try using the sharp escape hatch:
Code: Select all
sharp'Events.Quit += new System.EventHandler<QuitEventArgs>(this.Quit)'
# I think you could also try this:
sharp'Events.Quit += Quit'


Basically anywhere you have a Cobra expression (which includes statements), you can embed C# code with sharp'blah blah' or sharp"blah blah". Cobra won't understand that code, but will pass it down to the C# backend.

That should work and then you can proceed using Cobra. Of course, if many of your events are static, this will get annoying. I'll look into it today, but I'm tied up right now.

Re: Events and SDLDotNet

PostPosted: Tue Mar 18, 2008 12:18 pm
by Kaerigan
Oh, I didn't know you could do that. I'll try it out now. Thank you.

EDIT: Yup, it worked all right. It's not a pretty solution (although it isn't really ugly either), but the language is still young, or there is something wrong with the .dlls I guess. Thanks again.

Re: Events and SDLDotNet

PostPosted: Wed Mar 19, 2008 4:10 pm
by Charles
Fixed in development. The Cobra compiler was not paying attention to the fact that events could be static.

Let me know if you have further problems.

Re: Events and SDLDotNet

PostPosted: Thu Mar 20, 2008 3:24 am
by Kaerigan
Thanks, it works perfectly now. Now, I just thought I'd tell you this: when I try to view Cobra's help (cobra -h), something goes wrong. It shows information up to "-number[:decimal|float|float32|float64] default is decimal", then it crashes or something, with the following error (had to translate it from Swedish):

Unhandled exception: System.Collections.Generic.KeyNotFoundException: <something about a key not existing in some kind of list>
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at CommandLine.DoHelp() i c:\Build\Cobra\Source\CommandLine.cobra:line 824
at CommandLine.Run(List`1 args) i c:\Build\Cobra\Source\CommandLine.cobra:line 375
at CommandLine.Run() i c:\Build\Cobra\Source\CommandLine.cobra:line 330
at CobraMain.Main() i c:\Build\Cobra\Source\cobra.cobra:line 13


Which is kind of strange since Cobra currently resides in my C:\Program\Cobra folder. Everything else works.

Re: Events and SDLDotNet

PostPosted: Thu Mar 20, 2008 4:09 am
by Charles
Fixed.

The option spec for '-number:TYPE' was missing a "description". There was actually a test case for invoking cobra.exe's help, but it failed to check that the exit code was zero and/or that "Unhandled Exception" did not appear in the output. That test case now checks for both and I've added the description.

Thanks for the report.