Undocumented language features + questions
Monkey Forums/Monkey Programming/Undocumented language features + questions
| ||
| So there are examples which declare variables ending with # or $. I believe this is shorthand for an int or string, is this what it means? It is not mentioned in the language spec anywhere. Also there is this line in the mojo graphics.monkey: Import "data/mojo_font.png" Does this cause it to copy this file into the target data directory? Next, I thought I read somewhere that strict is supposed to force you to use parenthesis on function calls, but it doesn't seem to care. Also poking around with module files, I am looking at the cpp translator, does trans run this directly? If I wanted to modify something, like make strings pass by const reference instead of by value, would I be able to modify it here? If I wanted to add a language feature, would it be possible to do this with trans? So I could do something like add threading for example? The first step I am taking is designing a UI library I can use, based roughly around xwidgets and the box sizers to manage layout. Without delegates, interfaces, or multiple inheritence I am finding it incredibly awkward to try to setup a callback mechanism, so my button click event can bubble up to it's own callback function. The easiest way I have thought of so far is to create a generic handleEvent function that exists on all ui classes and pass the event name back as a string, along with the control that originated it. Is there any easier way to do this? |
| ||
| Cherry-picking the easy question: Interfaces are supported in recent releases of Monkey and are documented in the language reference. Have you updated to the latest version? |
| ||
| Yep, the symbols are shortcuts to the primitives types... # = Float % = Int $ = String ? = Boolean With mojo_font.png is automatically added to your project data folder at compile time. Yeah Strict should force parenthesis - this is a bug... |
| ||
| So there are examples which declare variables ending with # or $. I believe this is shorthand for an int or string, is this what it means? It is not mentioned in the language spec anywhere. Most of these are brought across from BlitzMax. % = Int, # = Float, $ = String, ? = Bool. If I wanted to add a language feature, would it be possible to do this with trans? Maybe, if you wanted to implement it in all target languages, and if you wanted to merge your changes with every new Monkey release. So I could do something like add threading for example? Depends on the target language. I'd say no. Next, I thought I read somewhere that strict is supposed to force you to use parenthesis on function calls, but it doesn't seem to care. Nope, although it would be nice. Without delegates, interfaces, or multiple inheritence I am finding it incredibly awkward to try to setup a callback mechanism, so my button click event can bubble up to it's own callback function. As muddy_shoes pointed out, interfaces have been available for quite some time now. Also eww @ multiple inheritance... D: To be honest I'm really not sure why Mark hasn't added exception handling yet, since all the target languages support it. It's fairly trivial and I would have added to trans myself, but I don't want to have to constantly merge. Edit: Beat me to it therevills... damn you! ;) |
| ||
The are function pointers, but they have to be methods in classes. More or less, we could considere Callbacks have to be done using classes.Class CallBack
Method Action:Int(Parameter:Object)
End
EndJust add a CallBack field to any GUI element and the game using the GUI should just point this fields to the desired instances of "callbacks" to perform the actions. The same you would do in a regular function pointer. You can design it in a way that "the caller" is also passed in the parameters of the callback. Pseudocode, simple example: Class MyButton
Field clickCallback:CallBack
Method OnClick()
If clickCallback <> Null Then clickCallback.Action(Parameters)
End
End |
| ||
| Ok, I am still on V29, I just bought monkey a week ago. I guess I assumed they would keep the initial pro download link up to date. It also took me a while to even find the update section on the website. The monkey guys should add this info to the 'Download' section if you are a registered user, it might prevent noobs like me from clogging the tubes with our questions. I am glad they have interfaces, and even happier they have callbacks, this will make my job much easier. |
| ||
| Ok, I just checked out the language docs for V42b, still no reference to the %,#,$,? shorthand. Also, re-reading the callback thing, how exactly would you implement a callback with that syntax? You are basically just making an interface there. This is how I would like to program:
class SimpleWindow extends Form
field okButton:Button
field cancelButton:Button
method New()
okButton = new Button(self)
cancelButton = new Button(self)
' I want something like this, so onClick is an array of callbacks
' with an object and a function pointer for each item
okButton.onClick.Add(self, OnOkPressed)
cancelButton.onClick.Add(self, OnCancelPressed)
end method
method OnOkPressed()
Close()
end method
method OnCancelPressed()
Close()
end method
end class
The interface method would work, but it'd be very verbose by comparison, can you even create nested classes? I fear the interface method will look like this:
class SimpleWindowOkHandler implements ButtonClickHandler
field parent:SimpleWindow
method OnClick()
parent.OnOkPressed()
end method
method New(sw:SimpleWindow)
parent = sw
end method
end class
class SimpleWindowCancelHandler implements ButtonClickHandler
field parent:SimpleWindow
method OnClick()
parent.OnCancelPressed()
end method
method New(sw:SimpleWindow)
parent = sw
end method
end class
class SimpleWindow extends Form
field okButton:Button
field cancelButton:Button
method New()
okButton = new Button(self)
okButton.onClick.Add(new SimpleWindowOkHandler(self))
cancelButton = new Button(self)
cancelButton.onClick.Add(new SimpleWindowCancelHandler(self))
end method
method OnOkPressed()
end method
method OnCancelPressed()
end method
end class
|
| ||
| Monkey doesn't offer function pointers (unless you subscribe to ziggy's rather generous interpretation) or treat functions as objects, so your desired technique isn't going to work at the moment. On the other hand, your construction is possibly a little more verbose than it needs to be. There are any number of ways to skin this particular cat, but if you change your click interface to offer more data about the source of the click you can avoid creating a class for every clickable item. Example:
Interface ClickHandler
Method OnClick( source:Button )
End
Class MyWindow Extends Form Implements ClickHandler
Field okButton:Button
Field cancelButton:Button
Method New()
okButton = new Button(Self)
okButton.onClick.Add(Self)
cancelButton = new Button(Self)
cancelButton.onClick.Add(Self)
End
Method OnClick( source:Button )
If source = okButton
...
ElseIf source = cancelButton
...
End
End
End
|
| ||
| To answer the question about importing graphics files: it works exactly like you speculated. I did a quick test to see if it works for selecting different assets depending on target: #If TARGET = "flash" Import "flash/screen.jpg" #ElseIf TARGET = "html5" Import "html5/screen2.jpg" #EndIf Works fine. |