Sections of native code (Extern?)

Monkey Forums/Monkey Programming/Sections of native code (Extern?)

Raz(Posted 2012) [#1]
Hi everyone,

as far as I understand, I can use Extern to write non-monkey code but I am not actually sure how I go about doing this. I've looked through the docs but I am unable to make sense of it.

Is anyone able to post a quick example of Monkey code calling a function that has been written in native code?

Thanks :)


bruZard(Posted 2012) [#2]
Example:

JavaScript:
function Hello(){ alert("Hello World"); };


Monkey:
Import "myJavaScript.js"

Extern
Function HelloWorld:Void() = "Hello"



Raz(Posted 2012) [#3]
Cheers Bruzard :)

So calling HelloWorld() from within the monkey code, will run Hello() from the js code?


bruZard(Posted 2012) [#4]
Your Monkey function is called "HelloWorld()" and calls the js function "Hello()"


Beaker(Posted 2012) [#5]
On a similar note: has anyone found a way to store a native object in/as a monkey object?


skid(Posted 2012) [#6]
For native objects I usually mimic the mojo model and create monkey class extensions of native objects.


xzess(Posted 2012) [#7]
hi beaker,

im currently working on this also.
in current progress, (i am wrapping cocoa elements and need to access them inside monkey)
i tried it very similar like in the mojo module.

Class iMButton for example holds the pointers from UIButton and handles it, by wrapping all needed functions to monkey.. for example iMButton::SetCaption(string caption) will set the private class var pointer UIButton *mybutton;

with mybutton.text = caption.ToNSString();


So something like

#import <UIKit/UIKit.h>

Class iMButton {
Private
UIButton *button

Public
void SetCaption(string caption)

}

iMButton::SetCaption(string caption)
{
button.text = caption.ToNSString();
}



and in monkey i would then wrap the SetCaption Void.


Gerry Quinn(Posted 2012) [#8]
I did the same - in my case it was to load a text file in Flash. (My code is probably rubbish as I know nothing about ActionScript, but it does work.)

Monkey code:


Flash code:



By the way, does anyone know a clean way to write text in Flash? I modified mojo in order to use setClipboard() inside the mouse event handler, thus obeying Adobe's rule that it can only be called from a user-initiated event handler. There's probably a way to make an invisible button in Flash without messing with mojo, though.