Some help for code structure

Monkey Forums/Monkey Beginners/Some help for code structure

hub(Posted 2015) [#1]
Hello.

As i developp for android target, i use a log text file to trace the loading errors. (it's a nightmare to debug on android without an message box to display an message).

All the loading operations are inside the OnCreate method(). So i've a black screen during this operation as I write a lot of debug text informations.



How to adapt my code to juste write something like 'Please wait loading...' during the OnCreate loading.

At this time, i play a sound 'loading please wait !' i've put it into the Oncreate() !

Many thanks monkey guru !


Steve Ancell(Posted 2015) [#2]
If you want something like a loading bar or loading screen then you would need to handle it inside OnUpdate() somehow, something along the lines of this.

You would need to create the loaded variable, UpdateLoader(), and DrawLoader() functions yourself of course.


Method OnUpdate()
While Not loaded
UpdateLoader()

EndIf
End


Method OnRender()
While Not loaded
DrawLoader()

EndIf
End


hub(Posted 2015) [#3]
This : ?




hub(Posted 2015) [#4]
i've always the black screen without anything. As OnCreate is the first code executed...


hub(Posted 2015) [#5]
OnLoading ? How to include this...


hub(Posted 2015) [#6]



hub(Posted 2015) [#7]


always the black screen...


Steve Ancell(Posted 2015) [#8]
You do realise you need to put that inside of a class that inherits from Mojo.App?, is that your actual code?


hub(Posted 2015) [#9]
Could you post some example code. ? On android target OnLoading do nothing. So have a downloading screen seems to be impossible to code.


Steve Ancell(Posted 2015) [#10]
Bear with me, I'll see what I can do.


Steve Ancell(Posted 2015) [#11]
Here's my quick 'n' dirty solution. Please note that the C_Loader class is fake, it designed just to prove the theory and does not actually load anything. A loader class is quite simple enough to make, but feel free to ask for help if you're not sure how.


Strict
Import mojo


Global loader:C_FakeLoader




Function Main:Int()
	Local game:C_Game = New C_Game()
	
	Return 0
	
End




Class C_Game extends App
	Method OnCreate:Int()
		loader = C_FakeLoader("MyFile.dat")
		
		Return 0
		
	End
	
	
	Method OnUpdate:Int()
		Local currentByte:Int
		
		
		If loader.bytesRemaining > 0
			currentByte = loader.ReadByte()
			
		EndIf
		
		Return 0
		
	End
	
	
	Method OnRender:Int()
		Cls
		
		If loader.bytesRemaining > 0
			DrawText("Loading!", DeviceWidth() / 2, (DeviceHeight() / 2) - 20)
			DrawText(loader.bytesRemaining, DeviceWidth() / 2, DeviceHeight() / 2)
			
		EndIf
		
		Return 0
		
	End
	
End




Class C_FakeLoader
	Field bytesRemaining:Int
	
	
	Method New(filePath:String)
		Self.bytesRemaining = 1000
		
	End
	
	
	Method ReadByte:Int()
		Local currentByte:Int
		
		
		currentByte = Self.bytesRemaining
		bytesRemaining = (bytesRemaining - 1)
		
		Return currentByte
		
	End
	
End



hub(Posted 2015) [#12]
i need to increase my monkey code knowledge to understand your code ! Many thanks for this.


Steve Ancell(Posted 2015) [#13]
I don't mind helping beginners, I've done you a simple program skeleton to help explain it better.


Strict
Import mojo




'Anything global is declared here and can be defined, accessed or changed anywhere in the program.
Global i_am_a_global_integer:Int
Global i_am_a_global_float:Float
Global i_am_a_global_string:String



'All Monkey-X programs need an entry point function named Main, any function that needs to return a value must have its return type declared just before the parentheses().

Function Main:Int()
	Local game:C_Game = New C_Game()
	
	
	'Globals can be defined inside functions, like this.
	i_am_a_global_integer = 123
	i_am_a_global_float = 123.45
	i_am_a_global_string = "Hello!"
	
	Return 0
	
End




'Mojo programs require a class that inherits from the Mojo App class, then an instance of this class needs to be created inside the Main:Int() function.
'Classes can contain methods, these are similar to functions.
'But, unlike functions and methods, variables are declared inside a class by using the Field keyword and can be defined in any of its methods; I have put an example of a field inside the class below.

Class C_Game extends App
	'Declaration of variable inside a class.
	Field classVariable:Int
	
	
	Method OnCreate:Int()
		'Initiation stuff goes in here.
		
		'Fields are accessed like this.
		Self.classVariable = 123
		
		
		'Globals can also be defined inside methods, like this.
		i_am_a_global_integer = 123
		i_am_a_global_float = 123.45
		i_am_a_global_string = "Hello!"
		
		
		Return 0
		
	End
	
	
	Method OnUpdate:Int()
		'Update stuff goes in here.
		
		'Globals can also be defined, accessed or changed here.
		'This example will increase this variable by 1 every time OnUpdate() is called by the Mojo App.
		i_am_a_global_integer = (i_am_a_global_integer + 1)
		
		Return 0
		
	End
	
	
	Method OnRender:Int()
		'Clear the screen each frame.
		Cls
		
		'Drawing stuff goes in here.
		
		'And finally, this will spam stuff onto the screen.
		DrawText(i_am_a_global_integer, 0, 0)
		DrawText(i_am_a_global_float, 0, 20)
		DrawText(i_am_a_global_string, 0, 40)
		
		Return 0
		
	End
	
End




MikeHart(Posted 2015) [#14]
Hi folks,

you can actually render images and draw text in the OnLoading event of mojo. This event will be called during OnCreate if a step takes to long. I use it to render a spinning wheel so the user knows thta the app is actually doing something.


hub(Posted 2015) [#15]
@MikeHart. Have you tested OnLoading with android ? Could you post an example with OnLoading ?
i use mojo2.

@Steve. Thanks you for all these code explanations !


Gerry Quinn(Posted 2015) [#16]
Unless you're doing something native, Monkey works pretty much the same on all devices. Why not do most of your debugging on a more suitable target? If you run on desktop with MSVC you get access to a real debugger, but anyway anything is better than Android! Just debug on Android for purely Android-specific stuff.

Quite possibly you have good reasons, but if it's just a vague mistrust of debug results from a different target (I know I felt that way at first) you will be pleasantly surprised at Monkey's cross-target consistency. Real bugs in Monkey code will generally look the same across all targets.


Midimaster(Posted 2015) [#17]
there are "async"-commands, which enable you to continue with OnRender() before all loading is completed. See the module mojo.asyncloaders and the command LoadImageAsync and OnloadImageComplete.

A sample should look like this:



Class Game Extends App

	Field  Check:Image, ExitKnopf:Image,....


	Method OnCreate%()
		SetUpdateRate 60
		Loader= New LoaderClass
		.....
	End



	Method ReceivedImage:Void(bild:Image,FileName$)
		Print "LOADER received "+ FileName + " " + BilderZahl + " " + Millisecs()
		Select FileName
			Case "Check.png"
				Check=bild			
				Loader.Load("ExitKnopf.png")
			Case "ExitKnopf.png"
			....
		End
		BilderZahl=BilderZahl+1
	End



	Method OnUpdate%()
		If KeyHit(KEY_ESCAPE) Self.OnBack() 
		.....
		UpdateAsyncEvents
	End



	Method OnRender%()
		If LadeStufe=0
			Loader.Load("Check.png")
			SplashScreen
			LadeStufe=1			
			Return 0
		Endif
		.....
	End
End


Class LoaderClass Implements  IOnLoadImageComplete

	Method Load:Void(FileName$,Frames%=1)
		If ExtractExt(FileName)="png"
			LoadImageAsync  FileName,Frames,,Self
		Endif
	End



	Method OnLoadImageComplete:Void ( bild:Image, path:String, source:IAsyncEventSource )
		MyGame.ReceivedImage bild,path
	End
End



Steve Ancell(Posted 2015) [#18]
MikeHart:
you can actually render images and draw text in the OnLoading event of mojo


I never new that, I never tried. LOL
Thanks for the tip Mike.


hub(Posted 2015) [#19]
When my app starts a log file is created to trace errors. So it take time to check or write informations. At this step i don't load images or sounds. So onLoading not fired in my case.
Finally i only use a boolean and put nothing inside the OnCreate(). On android the display message stay on screen while the program perform LoadMyData().




Shinkiro1(Posted 2015) [#20]
By the way: appli is not a common abbreviation for application. App or application are the 'correct' terms.
I just mention this because if other people read your code they might not know what you mean.

PS: Acronyms Seriously Suck


hub(Posted 2015) [#21]
Yes of course, In fact Appli is the french term. My native language. I use french into my code for this project.