Some issues ...

Monkey Forums/Monkey Beginners/Some issues ...

Sebastien(Posted 2014) [#1]
Hi everyone, actually I'm new on Monkey, and I still have some issues...

If I create a class that extends App, that I call in a Main function, everything works fine, and I load my files using "monkey://...". But since I'll use many more classes, I created a Game class where is my main loop of the game. Since I can't extend the App more than one time, I call the .OnUpdate and .OnRender manually in my main loop (what is a good thing since I can decide or not to call the functions). The OnCreate method is altered into New (), but my issue is that in this case "monkey://..." seems to not work anymore. I also ask myself if DrawImage will work in this case, or if I should do something like .Render(outputScreen). Thanks a lot for your help.


Paul - Taiphoz(Posted 2014) [#2]
Does your game class have its own render and update methods?


Xaron(Posted 2014) [#3]
No no no, do NOT call OnUpdate and OnRender by yourself.

You don't have a main loop anymore. You just have OnUpdate and OnRender which is handled by the app itself. So when you have a game class you should call your main method from OnUpdate and/or OnRender.

Example:

Game Class
Class Game
  Method New()
  End Method

  Method update:Void()
    'do your update logic here
  End Method

  Method draw:Void()
    'do all painting here
  End Method
End Class


Then you have your app itself and a global variable holding that game class.

So you do something like
gGame = new Game()


in OnCreate and just do:
Method OnUpdate:Int()
  gGame.update()
End Method

Method OnRender:Int()
  gGame.draw()
End Method



Sebastien(Posted 2014) [#4]
Yes they have, and works fine when extends App. But I need to add other classes so there comes the issues.


Paul - Taiphoz(Posted 2014) [#5]


Strict

Import mojo

Global Hello:Something = New Something()

Function Main:int()
	New MyGame
	Return 0
End

Class MyGame Extends App
	
	Method OnCreate:Int()
			SetUpdateRate 30
			Return 0
	End
	
	Method OnUpdate:int()
		Hello.Update()
		Return 0
	End 
	
	Method OnRender:Int()
		Cls
		Hello.Render()
		Return 0
	End
	
End Class

Class Something
	Field text:String
	Field x:Int
	Field y:Int
	
	Method New()
		Self.text = "Hello World"
		Self.x=Rnd(100,200)
		Self.y=Rnd(100,200)
	End
	
	Method Render:Void()
		DrawText(Self.text,Self.x,Self.y)
	End
	
	Method Update:Void()
	
	End 
End Class


[Edit ... NVM Xaron posted before me]


Xaron(Posted 2014) [#6]
I did Paul, but your example looks better! *g*


Sebastien(Posted 2014) [#7]
Ok, so OnCreate, OnUpdate and OnRender are only created in the main class, I thought each class had them. The only thing is the path where I stored the data doesn't work anymore (monkey://data/)... Thanks for your quick answers ;)


chimaera(Posted 2014) [#8]
Can you try to remove the "Monkey://Data" from your path and test it. Since all local loading comes from your appname.data-folder I never use the "Monkey" part of the path. I simply write LoadImage("myImage.png").

It might depend what you are loading. So what are you loading using "Monkey://Data..."


Sebastien(Posted 2014) [#9]
The method New() of my class isn't executed, despite the Import (I've put each class in a separate file). That's why I have a memory access violation, the picture isn't loaded. Someone had this issue ? Thanks ! :)


chimaera(Posted 2014) [#10]
Can we see your class-code, please.


Sebastien(Posted 2014) [#11]
Sorry it was a stupid thing. I loaded files into it and you can't do everything in a constructor. I've simply made a separate method. The only thing with Monkey is the debugger doesn't really help to find where is the issue from. Thank you very much for your help !


chimaera(Posted 2014) [#12]
The New() method is always called when you initiate a class like this "myLittleClass:c_myClass = New c_myClass". If you get a MAV-crash, then this could have to do with at least 2 things. 1) You are not loading the external asset correctly 2) You are loading the asset to soon (this has happened to me). Try to create a method called initiate() where you load the same asset (remove the asset loading from New()). Call the initiate()-method after the New()-method and see if it works.


chimaera(Posted 2014) [#13]
Ah! hehe. I added my answer without seeing that you already found the issue.


Sebastien(Posted 2014) [#14]
Thanks a lot everything works fine now ! I'll post some news about my project soon.


Gerry Quinn(Posted 2014) [#15]
OnCreate, OnUpdate, and OnRender are empty method in the App class that are called at appropriate times (if you look at the mojo.app code you will see calls to _app.OnRender etc., and that App.OnRender does nothing).

They are designed to be overridden, and since the super class version is empty you don't need to worry about having to call it. They could have been made abstract, but then you would be forced to make these functions in any extended app, whereas with overrides of empty functions you don't have to make them unless you need them.

By default the only method a class has is an automatically generated New() method if no New() is defined.

Incidentally, you can extend the App more than once if you want, in the sense that you could have MyFramework Extends App, and MyGame Extends MyFramework. I don't *recommend* this, but it is feasible. But there can only ever be one App-based class instance, so your program has a single chain of command. [When I say I don't recommend it, I mean I don't recommend it for your game class. I actually do this with a framework app that sets up some standard things and is then extended a second time to make an app-based class for each project, but the game logic is best put in its own class.]