LoadSound/LoadSprite return value

Just starting out? Need help? Post your questions and find answers here.
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

LoadSound/LoadSprite return value

Post by MrTAToad »

LoadSound/Sprite is supposed to return a numeric value (which is based on the index value). However, it appears that when not using #PB_Any, it just returns [object Object], and has a tendency to do weird things to the program...
Fred
Site Admin
Posts: 1821
Joined: Mon Feb 24, 2014 10:51 am

Re: LoadSound/LoadSprite return value

Post by Fred »

Could you elaborate why it is a problem ? All #PB_Any returns an object and it should be no issue with it.
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

Re: LoadSound/LoadSprite return value

Post by MrTAToad »

I presume these commands can fail, and as such its always a good idea to check to see if any problem has (or will occur) before it is used or loaded
Fred
Site Admin
Posts: 1821
Joined: Mon Feb 24, 2014 10:51 am

Re: LoadSound/LoadSprite return value

Post by Fred »

Yes, but could you provide a sample where it is a problem to return an object instead of a number ? If the command fail, it always returns 0.
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

Re: LoadSound/LoadSprite return value

Post by MrTAToad »

Here is a quick test program :

Code: Select all

;
; ------------------------------------------------------------
;
;   SpiderBasic - Sprite example file
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;

Define result

Debug "use arrow keys to move"

OpenScreen(800, 600, 32, "400 sprites")

Procedure RenderFrame()
  Static x, y
  
  ClearScreen(RGB(0, 0, 0))

  If ExamineKeyboard()
    
    If KeyboardPushed(#PB_Key_Left)
      x-1
    ElseIf KeyboardPushed(#PB_Key_Right)
      x+1
    EndIf
    
    If KeyboardPushed(#PB_Key_Up)
      y-1
    ElseIf KeyboardPushed(#PB_Key_Down)
      y+1
    EndIf
    
    ; Reduce the sprite size before display
    ;
    ZoomSprite(0, 60, 60)
    
    ; Display 100 sprites !
    ;
    For xDelta = 0 To 9
      For yDelta = 0 To 9 
        DisplayTransparentSprite(0, x+xDelta*60, y+yDelta*60, 128) ; half transparency
      Next
    Next
    
    ; and another 100 sprites !
    ;
    For xDelta = 0 To 9
      For yDelta = 0 To 9
        DisplaySprite(0, x*2+xDelta*60, y*2+yDelta*60)
      Next
    Next
    
  EndIf
  
  FlipBuffers() ; continue the rendering
EndProcedure


Procedure Loading(Type, Filename$)
  Static NbLoadedElements
  
  NbLoadedElements+1
  If NbLoadedElements = 1 ; Finished the loading of all images and sounds, we can start the applications
    FlipBuffers() ; start the rendering
  EndIf
EndProcedure


Procedure LoadingError(Type, Filename$)
  Debug Filename$ + ": loading error"
EndProcedure

; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loading())
BindEvent(#PB_Event_LoadingError, @LoadingError())
BindEvent(#PB_Event_RenderFrame, @RenderFrame())

Debug "Result of loading actual file : "+Str(LoadSprite(0, "Data/Spider.png"))
Debug "Result of loading invalid file : "+Str(LoadSprite(0, "Data/Spider1.png"))
result=LoadSprite(#PB_Any, "Data/Spider.png")
Debug "Sprite ID for valid sprite : "+Str(result)
If IsSprite(result)
  Debug "Sprite exists!"
  Debug "Size : "+Str(SpriteWidth(result))+" x "+Str(SpriteHeight(result))
EndIf
result=LoadSprite(#PB_Any, "Data/Spider1.png")
Debug "Sprite ID for invalid sprite : "+Str(result)
If IsSprite(result)
  Debug "Sprite exists!"
  Debug "Size : "+Str(SpriteWidth(result))+" x "+Str(SpriteHeight(result))
EndIf

and the output
Result of loading actual file : [object Object]
Result of loading invalid file : [object Object]
Sprite ID for valid sprite : 100000
Sprite exists!
Size : undefined x undefined
Sprite ID for invalid sprite : 100001
Sprite exists!
Size : undefined x undefined
Data/Spider1.png: loading error
Data/Spider1.png: loading error
As you can see the result when using an actual id number is "object Object", rather than, as per the instructions, 0 if there was a problem, or another value if everything is okay. Would actually be handy if you can get a value denoting the file was cached too...
Fred
Site Admin
Posts: 1821
Joined: Mon Feb 24, 2014 10:51 am

Re: LoadSound/LoadSprite return value

Post by Fred »

As the loading are asynchronous, LoadXXX() functions will always success :).
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

Re: LoadSound/LoadSprite return value

Post by MrTAToad »

I thought that was the case!
Fred
Site Admin
Posts: 1821
Joined: Mon Feb 24, 2014 10:51 am

Re: LoadSound/LoadSprite return value

Post by Fred »

I will put a note in the doc to make it clear.
Post Reply