Cannot use LoadImage()

Monkey Forums/Monkey Programming/Cannot use LoadImage()

V. Lehtinen(Posted 2014) [#1]
Hi!

I bumped into a really ridiculous "feature"(?) while coding my game engine...
I had some test code in my Engine, which extends mojo.App, and it worked just fine.
So I thought I'd move that code to a separate class (TestScene, which extends from my Scene class).
Well, now I can't load any images without this M.A.V....

(Code seen above is from "mojo/graphics.monkey")
So, why there isn't a device when I try to load images somewhere else than inside App??
Or am I missing something... Please help! I'm crying! :'((


V. Lehtinen(Posted 2014) [#2]
Here's the code where it all goes wrong:
Strict
Import mge
Import mojo

Import "test.data/tikku.png" ' The image I'm trying to load.

Function Main:Int()
    MGE.engine = New Engine() ' The engine is extended from App, and it is the one that runs the stuff
    MGE.scene = New TestScene()
    
    Return 0
End

Class TestScene Extends scene.Scene
    
    Method OnCreate:Void()
        Local list:= New List<Entity>
        For Local i:Int = 0 To 359
            Local x:Float = 400 + monkey.math.Cos(i) * 300
            Local y:Float = 300 - monkey.math.Sin(i) * 200
            Local e:Entity = New Entity(x, y, LoadImage("tikku.png")) ' This here. Can't load an image. THERE'S NO DEVICE TO LOAD IT WITH?! :<
            e.angle = i * 3.6
            list.AddLast(e)
        Next
        
        AddList(list)
        list.Clear()
    End
End



Gerry Quinn(Posted 2014) [#3]
I'd be lying if I told you I understood the inner workings of the App class, but I imagine that stuff happens asynchronously to get graphics devices etc. started up, and it is not complete when App.New() returns. Once App.OnCreate() has been called you are good to go, so put your initialisation in that..


dawlane(Posted 2014) [#4]
Try changing
Import "test.data/tikku.png"
to
Import "data/tikku.png"
The application will be looking for the tikku.png in it's data directory.

Edit:
See the graphics.monkey file in the mojo module on how it does it.


V. Lehtinen(Posted 2014) [#5]
Edit:
See the graphics.monkey file in the mojo module on how it does it.


Yeah, I tried that, but changed it to "test.data" because:

I'd be lying if I told you I understood the inner workings of the App class, but I imagine that stuff happens asynchronously to get graphics devices etc. started up, and it is not complete when App.New() returns. Once App.OnCreate() has been called you are good to go, so put your initialisation in that..

So I'm not able to load images, unless it happens when App.OnCreate() is called? Hmm... :/
Guess I'll need to go deeper and change things around...

EDIT: Yeah, it's exactly like that.. I changed the code, so Engine calls current scene's OnCreate from it's OnCreate and everything works. Just want to have a little bit better structure with these... At least now I know how things work, thanks! :)