Wrapping code in target language

Monkey Forums/Monkey Programming/Wrapping code in target language

Foppy(Posted 2011) [#1]
I have a question about "wrapping" code that is not Monkey code.

Basically I am wondering how to do this.

For example, suppose I would like to use the Kongregate API in a game, which I am actually considering. My target in this case would be Flash. Now Kongregate give an example on their website about how to use the API. How can I add the necessary actionscript code to my game without having it overwritten the next time I build the Monkey program? I have seen people say "oh just wrap the code" but couldn't find an example of how to actually do that. :)

By the way, the Kongregate example is here: http://developers.kongregate.com/docs/api-overview/as3-api


AdamRedwoods(Posted 2011) [#2]
You'd make a Flash module.

So you'd make a monkey "front-end" module that had functions like:
Import "kongregate api"
Extern
  Function StartKongregate()
  Function GetKongregateUser()
  '' ..etc...
End


Then you'd have to write the Flash module, basically using example code they have and putting that into these functions above.
Function StartKongregate() {
  // Pull the API path from the FlashVars
  var paramObj:Object = LoaderInfo(root.loaderInfo).parameters;

  // The API path. The "shadow" API will load if testing locally. 
  var apiPath:String = paramObj.kongregate_api_path || 
  "http://www.kongregate.com/flash/API_AS3_Local.swf";

  // Allow the API access to this SWF
  Security.allowDomain(apiPath);

  // Load the API
  // etc....
}


Note how I don't really have any return values, and if you do keep them to Integers if possible. Easiest that way. If you need strings then you'd have to manage the string in Monkey and pass it to the function by Object (since as3 passes by value).

...although maybe you could return a string. Try it, not sure how the GC would handle it.


slenkar(Posted 2011) [#3]
you can test your game in html5 and MingW and then test in flash when you need the kongregate username etc.

#if TARGET="flash"
Import "kongregate api"
Extern
Function StartKongregate()
Function GetKongregateUser()
'' ..etc...
End
#end




Foppy(Posted 2011) [#4]
So I create a "front end" module in Monkey, explaining Monkey that these functions are "external", and their actual definitions are to be found in the Flash module (also written by me) that I import.

But wouldn't that mean I have to import an actionscript file (namely the Flash module)?

Edit: This post by Rob Pearmain about iOS integration seems to suggest as much, in this case a .cpp file is imported, in my case it would be an .as file:

http://monkeycoder.co.nz/Community/posts.php?topic=1736

So thanks guys, I will have a try!


Foppy(Posted 2011) [#5]
Thanks again Adam and slenkar, with your help I have now added Kongregate highscores to my game Monkey Pixels:

http://www.kongregate.com/games/Foppygames/monkey-pixels

I had to access the Flash root object and couldn't figure out how, since it has to be accessed through a DisplayObject (or Sprite, or any visible object of a related class). Then after having a look at the AS code generated by Monkey I realized that of course the "game" object (extended from Sprite) created by Monkey is what I needed! I tried to access game.root, and from then onward everything just worked!

I will add a post to the tutorials section in the coming days about this.

This is very cool as I am writing a racing game that I was also planning to put on Kongregate, and having highscores will make it much more fun.


slenkar(Posted 2011) [#6]
glad you got it working, I will probably use it in a few months when I am ready to release something :)