Problem accessing App class member variables

Monkey Forums/Monkey Programming/Problem accessing App class member variables

chimaera(Posted 2011) [#1]
I am (like a lot of other people) converting a little Bmax program To Monkey, To learn more about Monkey itself. But hi run into a problem that I guess have To do with scoping Or access permissions (public/private maybe).
Import mojo

Function Main()
	New myApp
End

Class myApp Extends App
	Field myVariable:Int=1
	Field player:c_player
		
	Method OnCreate()
		player = New c_player
	End
	
	Method OnUpdate()
		player.update()
	End
	
	Method OnRender()
	End
	
	Function returnMyVariable()
		Return myVariable
	End
	
End

Class c_player
	Method update()
		Print myApp.myVariable 'Trying to access the variable directly
		Print myApp.returnMyVariable() 'Trying to access via member function
	End
End


I have implemented 2 ways that I have tried to access myApp's variables, in the c_player.update. Both lines give me a "Field 'myVariable' cannot be accessed from here" error.

Can anyone help me out?


therevills(Posted 2011) [#2]
Because you havent assigned myApp to a global variable:

Import mojo

Global myApp:MyApp

Function Main()
	myApp = New MyApp
End

Class MyApp Extends App
	Field myVariable:Int=1
	Field player:c_player
		
	Method OnCreate()
		SetUpdateRate(30)
		player = New c_player
	End
	
	Method OnUpdate()
		player.update()
	End
	
	Method OnRender()
	End
	
	Function returnMyVariable()
		Return myApp.myVariable
	End
	
End

Class c_player
	Method update()
		Print myApp.myVariable 'Trying to access the variable directly
		Print myApp.returnMyVariable() 'Trying to access via member function
	End
End




chimaera(Posted 2011) [#3]
Ahhh. I wasn't sure that you could use globals like this in Monkey...Also took my a while to see that you changed the extended App class to "MyApp". Couldn't get it to compile it before that.

Thanks for the help.


therevills(Posted 2011) [#4]
Opps should have said I did that... I prefer to name my classes in PascalCase (Capital letter first).

Also you didnt have a SetUpdateRate in your OnCreate and you need that to run anything in OnUpdate.