What am I doing wrong here?

Hey guys. I'm trying to create a method that simply creates a very basic event at some co-ordinates in the current map. Here's what I have so far.

  1.  def VoodooDoll.create_clone(x,y)
  2.     clone = RPG::Event.new(x,y)
  3.     clone.name = "LEON_VOODOO_CLONE"
  4.     clone.id = $game_map.events.size + 1
  5.     clone.pages[0] = RPG::Event::Page.new
  6.     clone.pages[0].graphic.character_name = "Leon"
  7.     $game_map.events[clone.id] = Game_Event.new($game_map.map_id, clone)
  8.   end
Expand to see the code.


Now I have done this all while keeping an eye on the RPG reference manual (you know, the one you get when you press 'help'), and it seems that the initialize method of RPG::Event's subclasses (ie; RPG::Event::Page, RPG::Event::Page::Condition, RPG::Event::Page::Graphic and RPG::EventCommand) take care of themselves, in that they create very basic versions of themselves, hence why they are not really addressed in my method.

Also, I have created attribute accessors for @map_id and @events.

Problem is that just nothing happens when the script is run. No errors are thrown, just nothing happens. But at the co-ordinates given when I call the script, I can't walk over that tile. It's like an event is there, just transparent. I tried also putting "Graphics/Characters/Leon.PNG" but that didn't work either. Any help? I'm not sure what I'm doing wrong, here.
So I looked into it, and the problem is quite simple. You were creating an event, but the event sprites are created when you load Scene_Map. That means a bit of a workaround is required to get it to work properly. That said, I have a demo script that makes it work. If you look at what I have below, this is what you need to do to add it to the map. I also removed an unnecessary line of code, and corrected the event id. (Size returns 1 larger than the last id in an array or array-like hash, so you don't need to add one when you use it).

  1. #==============================================================================
  2. # ** Spriteset_Map
  3. #------------------------------------------------------------------------------
  4. #  This class brings together map screen sprites, tilemaps, etc.
  5. #  It's used within the Scene_Map class.
  6. #==============================================================================
  7.  
  8. class Spriteset_Map
  9.   #--------------------------------------------------------------------------
  10.   # * Public Instance Variables
  11.   #--------------------------------------------------------------------------
  12.   attr_accessor :character_sprites        # character sprites
  13.   attr_accessor :viewport1                # character sprites
  14. end
  15.  
  16. #==============================================================================
  17. # ** Scene_Map
  18. #------------------------------------------------------------------------------
  19. #  This class performs map screen processing.
  20. #==============================================================================
  21.  
  22. class Scene_Map
  23.   #--------------------------------------------------------------------------
  24.   # * Public Instance Variables
  25.   #--------------------------------------------------------------------------
  26.   attr_accessor :spriteset                # map sprites
  27. end
  28.  
  29. #==============================================================================
  30. # ** Interpreter (part 1)
  31. #------------------------------------------------------------------------------
  32. #  This interpreter runs event commands. This class is used within the
  33. #  Game_System class and the Game_Event class.
  34. #==============================================================================
  35.  
  36. class Interpreter
  37.   def create_clone(x,y)
  38.     clone = RPG::Event.new(x,y)
  39.     clone.name = "LEON_VOODOO_CLONE"
  40.     clone.id = $game_map.events.size
  41.     clone.pages[0].graphic.character_name = "001-Fighter01"
  42.     $game_map.events[clone.id] = Game_Event.new($game_map.map_id, clone)
  43.     if $scene.is_a?(Scene_Map)
  44.       viewport = $scene.spriteset.viewport1
  45.       new_sprite = Sprite_Character.new(viewport, $game_map.events[clone.id])
  46.       $scene.spriteset.character_sprites << new_sprite
  47.     end
  48.   end
  49. end
Expand to see the code.
Oh, cool. That works. Though, size+1 is necessary because event ID's actually start counting from 1, not 0 like we are always taught was the practice. Silly RPGMaker... Anyway, it still all works. Thanks, man!

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=76214