Import Global scope

Monkey Forums/Monkey Programming/Import Global scope

Shagwana(Posted 2011) [#1]
Given the following code...

'Filename: Scope.Monkey
Strict
Import GlobalScopeInc
Global iTesting:Int
Function Main:Int()

	iTesting=99
	Print(iTesting)			'Good and works as expected

	Local temp:OtherFileClass = new OtherFileClass
	temp.Do()
End Function

'Filename: GlobalScopeInc.Monkey
Import Scope
Class OtherFileClass
	Method Do:Void()
		Print(iTesting)   'Does not work
	End Method
End Class


Problem: The 2nd print does not show the global value from the first

However If its all in one file like so...
Strict
'Import GlobalScopeInc
Global iTesting:Int
Function Main:Int()

  iTesting=99
	Print(iTesting)			'Good and works as expected

	Local temp:OtherFileClass = new OtherFileClass
	temp.Do()
End Function

Class OtherFileClass
	Method Do:Void()
		Print(iTesting)   'Now does work
	End Method
End Class

It will work.


Any ideas what I can do to fix the first issue, using a global in a import file?


Beaker(Posted 2011) [#2]
Don't use globals? :)

Or use a class specifically for storing global data?


Shagwana(Posted 2011) [#3]
The reason is for the main class that holds all the OnCreate(), OnUpdate() functions to be accessable from the includes, like so..

Global cGame:SublimeGameClass = Null          'Main game engine

Function Main:Int()
	cGame = New SublimeGameClass 
	Return 0
End 'Function

Class SublimeGameClass Extends App

	Field iData:Int

	Method OnCreate:Int()
		iData=99
		Return 0
	End 'Method

	Method OnUpdate:Int()
		Return 0
	End 'Method

	Method OnRender:Int()
		Return 0
	End 'Method
End 


The above I want to access the data held in the main working program from an included(imported) class. As my little example above shows, I am having a little problem with that.


maverick69(Posted 2011) [#4]
I think what you trying to achieve is broken by design.

A better way would be to use a singleton class which holds the data, then import it when you need access to it from other classes or main source file.


Shagwana(Posted 2011) [#5]
I solved it by using 'self' (as a param) from the main class and thus cascade it to those classes that need the information.