What am I doing wrong?
Monkey Forums/Monkey Programming/What am I doing wrong?
| ||
I have a class that has an image that I want to load once, because it's Global.Class Foo Global foo_image:Image = LoadImage("foo.png",1,Image.MidHandle) End Class When I try to access the foo_image within the class it throws a null surface error (which means it didn't load). But if I do this: Global foo_image:Image Class Foo Method New() foo_image = LoadImage("foo.png",1,Image.MidHandle) End Method End Class it works fine and I can access it. I just want to be sure I'm not loading a LOT of instances of this image and, that even though I'm loading it with each new Foo that there's only one global image. Is this valid? |
| ||
Off the top of my head, maybe the class gets initialised before the gfx gubbins (technical term). Try declaring the global but not loading the image until OnLoad()*? *Or equivalent -- not Monkeyed for a bit! |
| ||
you're loading the image before the graphics context exist. You should not use any Mojo command before the OnCreate has ben called. |
| ||
Yeah, I seen this and come to the conclusion that Monkey doesnt like to set variables in the declaration. |
| ||
It's explained in the documentation. Monkey does like initialization of fields and globals in clases, but you have to understand that the order in wich this is done can have side effects. A graphics context is required before anything is thrown to it. This has nothing to do with the language itself. |
| ||
I just want to be sure I'm not loading a LOT of instances of this image.. if you initialize it in the new method, the loadImage will be executed every time you create an instance of the Class and you will be filling memory likewise. I know that for sure. I am not sure if it will be loaded only once if you initialize it in the class declaration but probably will be loaded only once. |
| ||
You can check in the new() method if the image is loaded before loading it again. But the best way is to initialize the loading of images when the grahpics context has been created. That's what I am doing. |
| ||
Unless you have a ton of graphics, it's best to load them all in at a fixed point, sometime during App.OnCreate(). I tend to have my main game window own all the graphics, and it gets instantiated during App.OnCreate(), so GameWindow.New() does the loading. |
| ||
I tend to create an init function within classes for global stuffClass Thing Global dog:Image Function Init:Void() dog = LoadImage("woop.png") End End and call Thing.Init() in OnCreate() |