How to Trigger OnCreate()
Monkey Forums/Monkey Programming/How to Trigger OnCreate()
| ||
[monkeycode] Class testClass Method New() Print "New()" End Method OnCreate:Int() Print "OnCreate()" Return 0 End End Function Main:Int() Local test:testClass = New testClass Return 0 End [/monkeycode] The output is New() Why? How do I get OnCreate() to fire? |
| ||
Extend the App class. OnCreate isn't a Monkey language feature, it's a mojo "framework" feature. |
| ||
OnCreate is a method of the Mojo class App. If you want OnCreate to fire because testClass is your mojo application then you need it to extend the App class. If it is just a plain monkey class, you will need to call OnCreate yourself after you create it. test.OnCreate() |
| ||
Class testClass Method New() Print "New()" OnCreate() End Method OnCreate:Int() Print "OnCreate()" Return 0 End End |
| ||
Ahhhhhh! OK. So, then, how would I fix the below so that LoadString() works without making testClass Extend App? [monkeycode] Import mojo Class testClass Method New() ' this doesn't work here because requires App ' Local x:String = LoadString("test.txt") Print "New()" End Method OnCreate:Int() ' and this doesn't fire here Local x:String = LoadString("test.txt") Print "OnCreate()" Return 0 End End Function Main:Int() Local test:testClass = New testClass Return 0 End [/monkeycode] |
| ||
Import mojo Class testClass Field mystring:String Method New() Print "New()" mystring = LoadString("test.txt") End End Function Main:Int() Local test:testClass = New testClass Print test.mystring Return 0 End |
| ||
Difference, I get this error:Monkey Runtime Error :TypeError: Cannot call method 'LoadString' of null Does this code run for you?! |
| ||
c.k. what are you trying to do? Why dont you want to expend App? |
| ||
To use mojo functions you must have created an App instance. It doesn't need to be used or extended, just created to initialise everything. That's why you'll notice a number of test examples on the forum where Main() just does something like: [monkeycode] Function Main:Int() Local a:App = New App() ' test code that uses mojo or needs to run on a mojo target End [/monkeycode] |
| ||
It's just an object. It's not an App. I don't mind Extending App, if that's appropriate for "just objects." |
| ||
I've got a module and I'm writing a Main() function for testing purposes (so I can just run the module file). So, there's no App anywhere. I'll just use muddy_shoes' suggestion. Seems reasonable... :-) Of course, I might be going about testing all wrong... :-/ |
| ||
If you want your module to work without mojo, for instance using the C++ target to build components that can run on a remote server Monkey does allow you to develop without it. Without mojo, you can use the LoadString command from the OS module. |
| ||
If your module is for use with mojo, you may just as well make a test harness that extends App. But if you just want LoadString(), you are okay as skid points out. |