Disposing Sprites and Windows Efficiently

Rikifive

Bringer of Happiness
Veteran
Joined
Jun 21, 2015
Messages
1,442
Reaction score
679
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
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

If you had some musical requests in line with me.. feel free to re post! Corona has killed me and have recently got my internet back <3
I'm now working on procedural generated World Maps for my own project.
Is there anyone who's interested in having this Plugin for his own game?
I have a dream where RMer are not blinded and too dependant on published script / plugins...
RM MV ACE? 2020? Super? Legends? Sword and Shield? 2.5D? RM Pixel? RM 2: Revenge of Squared RTP tiles? Don't mind me, I'm just wondering about what will be the name of the next engine.


So I came up with a new look for the paths, and along the way realized health bars are so overrated, so now a crazy red light trail follows her and gets shorter as her life decreases. :LZSexcite:

Forum statistics

Threads
98,210
Messages
950,357
Members
129,506
Latest member
TakuPlayz
Top