how to import something in a different folder?

Monkey Forums/Monkey Programming/how to import something in a different folder?

Grey Alien(Posted 2011) [#1]
If I use James Boyd's autofit code by putting it in the same folder as my main project, Import autofit works fine. But if I want to put the code into a shared folder and I do this:

Import "../include/autofit.monkey"

Monk complains that SetVirtualDisplay, one of the functions in autofit, isn't working properly. Where am I going wrong please? Thx.


therevills(Posted 2011) [#2]
Try this:

Import include.autofit


BTW Diddy has Jame's code in it too, by just calling SetScreenSize(640,480, True), the boolean sets autofit on/off.


Grey Alien(Posted 2011) [#3]
Oh I see. Problem is include is up a level from my game folder. That's why I used ..

Ah, well I'll probably use that SetScreenSize then, but do I need to also call something in OnRender before CLS?


therevills(Posted 2011) [#4]
Problem is include is up a level from my game folder. That's why I used ..


Opps, didnt see that...

but do I need to also call something in OnRender before CLS?


Nope... something like this is all you need to do:

Strict

Import diddy

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

Global gameScreen:GameScreen

Class MyGame Extends DiddyApp
	Method OnCreate:Int()
		Super.OnCreate()
		
		images.Load("background.png", "", False)
		SetScreenSize(480, 320)
		
		gameScreen = new GameScreen
		gameScreen.PreStart()
		
		Return 0
	End
End

Class GameScreen Extends Screen
	Field backgroundImg:GameImage
	
	Method New()
		name = "Game"
	End

	Method Start:Void()
		game.screenFade.Start(50, false)
		backgroundImg = game.images.Find("background")
	End
	
	Method Render:Void()
		Cls
		backgroundImg.Draw(0, 0)
	End
	
	Method Update:Void()
		If KeyHit(KEY_ESCAPE)
			game.screenFade.Start(50, true)
			game.nextScreen = game.exitScreen
		End
	End
End



Grey Alien(Posted 2011) [#5]
Thanks. I'm not actually using TDiddyApp at the moment in my minigame so SetScreenSize doesn't work (I assume it's a method of TDiddyApp). But if I do, I'll be sure to use SetScreenSize.

So back to my question, does anyone know how to include a file up one level from the game source so that I can have an include folder that spans all games? I did this for my BMax stuff, or should I just put autofit into a folder inside modules?


DruggedBunny(Posted 2011) [#6]
I don't think you can do this; Import takes a Monkey-style identifier, rather than a straight file system string. Since each part has to be separated by dots (like accessing the fields of an object, eg mainfolder.module) you can't represent 'parent directory' with two dots. (This might possibly relate to the fact that some platforms don't have a file system as such... not sure.)

I don't think you're meant to be able to stick it in quotes, either (that's like putting a variable name in quotes), but I'm surprised Monkey doesn't complain about not finding the import.

Another way to do this is to create a folder called autofit in MonkeyPro/modules and stick autofit.monkey in there. Then you can just do...

Import autofit


... in any project.


therevills(Posted 2011) [#7]
I'm not actually using TDiddyApp


You can of course rip out the code and stick it in yours, James did a brilliant job with it :)

Also its time to drop the T prefix... your in Monkey land now, Types are Classes... so it should be a C, or nothing at all ;)

(And since Monkey is case-sensitive, I really suggest you stick with a coding standard and dont mix them up (eg PascalCase for Classes/Methods/Functions and camelCase for variable names))


Grey Alien(Posted 2011) [#8]
Haha, I used T for 9 years in Delphi before I began making games so it's an OLD habit now.

Yep I do use PascalCase and camelCase and CONSTS_LIKE_THIS

Yeah I'll try that module idea thanks.


therevills(Posted 2011) [#9]
Yep I do use PascalCase and camelCase and CONSTS_LIKE_THIS


Not all the time your don't, remember I've got GAF and I've seen your code - LOL ;)

Its actually a job I've been putting off for ages, is to actually go thru GAF and fix up the coding style :D


Grey Alien(Posted 2011) [#10]
Oh yeah I didn't use it for GAF, but I do now since working at BFG on a team. I started GAF in 2006 after using BlitzPlus (and Delphi) before that, and it grew sorta organically over time and my approach to how to do things changed (got better). I even began to refactor some of the classes to mark the private fields with _ and tidy things up but never finished. I actually made a mini game engine in Flash at BFG (they own it) and that was nice and clean, as is stuff I write in Monkey now.