Crash after Scene Change

Hello, I need some more understanding how viewports work in combination with sprite.

I have this small piece of code which manually sets the viewport of a sprite after its creation:

class Sprite_Picture alias_method🙂evg_sp_initialize_pu1, :initialize) def initialize(*args) evg_sp_initialize_pu1(*args) if SceneManager.scene_is?(SceneMap) self.viewport = SceneManager.scene.spriteset.get_viewport_evg(1) end endendclass Scene_Map attr_reader :spritesetendclass Spriteset_Map def get_viewport_evg(id) case id when 1 @viewport1 when 2 @viewport2 end endendIf I now change the scene and go back to Scene Map the game just crashes without any error msg.

Someone can explain to me why this happens?
I'm not sure why it crashes, but you did forget the _ in Scene_Map in your condition.
That was just because i retyped it Here. In the editor it is Scene_Map. I just cant figure out why it crashes when i manually change the viewport ref, switch scene and switch back. I also tried to debug it with the hidden game.exe crash debugger but it has no clue either like it seems.
I tried that snippet, but it won't crash for me.

I changed scenes around 30 times, no crash.

This makes me think, that you got some compatibility issues with other scripts, maybe?

Just guessing.

I take back, it will crash if I got an active picture showing. o.o

Btw, why do you want to change their viewports?

You can do that with this too:

Code:
class Spriteset_Map  def update_pictures    $game_map.screen.pictures.each do |pic|      case pic.number      when 1..10        vp = @viewport1      when 11..20        vp = @viewport2      # and so on...      else        vp = @viewport3      end      @picture_sprites[pic.number] ||= Sprite_Picture.new(vp, pic) #@viewport2, pic)      @picture_sprites[pic.number].update    end  endend
This will change their viewports depending on their number defined with the Show Picture event command.
If you return to the map, a new spriteset has to be created. However, until the spriteset is completely initialized  SceneManager.scene.spriteset  refers to the previous spriteset that was disposed when you left the map.

You should see an error message instead if you return to the map by using an item/skill that invokes a common event for example.

In addition, while it does not really matter here, SceneManager.scene changes as soon as a new scene to go to is determined, it does not necessarily match the scene that is currently executed. For example, If you run this during a battle event, the picture sprite created in the battle spriteset would refer to the map spriteset viewport as well:
@>Show Picture: 20, 'Destiny', 'Upper Left' (0, 0), (100%, 100%), 255, Normal

@>Abort Battle

Original thread: https://forums.rpgmakerweb.com/threads/crash-after-scene-change.38360/