How to Trigger OnCreate()

Monkey Forums/Monkey Programming/How to Trigger OnCreate()

c.k.(Posted 2012) [#1]
[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?


muddy_shoes(Posted 2012) [#2]
Extend the App class. OnCreate isn't a Monkey language feature, it's a mojo "framework" feature.


skid(Posted 2012) [#3]
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()



Difference(Posted 2012) [#4]
Class testClass
        Method New()
                Print "New()"
                OnCreate()
        End
        Method OnCreate:Int()
                Print "OnCreate()"
                Return 0
        End
End



c.k.(Posted 2012) [#5]
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]


Difference(Posted 2012) [#6]
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




c.k.(Posted 2012) [#7]
Difference, I get this error:

Monkey Runtime Error :TypeError: Cannot call method 'LoadString' of null


Does this code run for you?!


therevills(Posted 2012) [#8]
c.k. what are you trying to do? Why dont you want to expend App?


muddy_shoes(Posted 2012) [#9]
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]


c.k.(Posted 2012) [#10]
It's just an object. It's not an App.

I don't mind Extending App, if that's appropriate for "just objects."


c.k.(Posted 2012) [#11]
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... :-/


skid(Posted 2012) [#12]
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.


Gerry Quinn(Posted 2012) [#13]
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.