Disposing Sprites and Windows Efficiently

Discussion in 'Learning Ruby and RGSSx' started by Rikifive, Mar 21, 2016.

  1. Rikifive

    Rikifive Bringer of Happiness Veteran

    Messages:
    1,442
    Likes Received:
    676
    Location:
    Poland
    First Language:
    Polish
    Primarily Uses:
    Other
    Hello everybody!


    This time I'd like to ask a totally noob question.


    Let's say I'm creating multiple sprites and windows in a scene and stuff ~ everything works nice, but we'll have to dispose them when leaving the scene and such.


    For example, let's create some stuff:

    class Scene_Some_Scene < Scene_Base
    def start
    super
    SceneManager.clear
    Graphics.freeze
    initialize_scene
    end

    def initialize_scene
    @background = Sprite.new
    @background.bitmap = Cache.abc("Background")
    @background.z = -6

    @player_cursor = Sprite.new
    @player_cursor.bitmap = Cache.abc("cursor")
    @player_cursor.x = 300
    @player_cursor.y = 240
    @player_cursor.z = 100

    @player2_cursor = Sprite.new
    @player2_cursor.bitmap = Cache.abc("cursor2")
    @player2_cursor.x = 340
    @player2_cursor.y = 240
    @player2_cursor.z = 100


    @first_window = Window_First.new
    @second_window = Window_Second.new
    end

    (to be continued...)


    Everything works great, now let's get rid of this:

    (continuation of the code above)

    def terminate
    super # or even awesome ( ͡° ͜ʖ ͡°)
    dispose_sprites
    dispose_windows
    end

    def dispose_sprites
    @background.bitmap.dispose
    @background.dispose
    @player_cursor.bitmap.dispose
    @player_cursor.dispose
    @player2_cursor.bitmap.dispose
    @player2_cursor.dispose
    end

    def dispose_windows
    @first_window.dispose
    @first_window = nil
    @second_window.dispose
    @second_window = nil
    end
    end


    It all works, but is it completely okay without any side effects like stealing memory and stuff?


    Also what if I'll forget about one sprite or window?


    I know, that it stays on screen, but it disappears after a while - is that eating the memory all the time?


    I'm doing stuff and it works, but I don't have any 'theory' experience, because I was learning stuff by myself (by that I mean no school and stuff) ~ I don't know some basics about Ruby, so that's why I'm asking.


    Also what about changing the bitmap?


    Is using codes like this correct?


    @background.bitmap = Cache.abc("New Background")



    and

    Code:
    @background.bitmap.dispose
    @background.dispose
    Do I need to put both of these lines to completely dispose it? ~ or would the second one only work?


    Is there anything else I should know about this?


    ~ Thank you
     
    #1

Share This Page