Execute common events directly when called in a method?

....

@menu1.set_handler🙂task, method🙂command_task))
@menu1.set_handler🙂folmagi, method🙂command_folmagi))
@menu1.set_handler🙂folbruti, method🙂command_folbruti))
@menu1.set_handler🙂cancel, method🙂return_scene))
end

def freezefix
@menu1.active = true
end

def command_task
$game_temp.reserve_common_event(1)
freezefix
end


This is a cutout from me learning rgss3 scripting. I am currently learning how to script an menu.
Anyway, I am having  trouble understanding what controls the flow of when the command event I am calling is ran at.
This part:
 


def command_task
$game_temp.reserve_common_event(1)
freezefix
end


I want the common event to be run directly when that method is called. At the moment the common event is executed after i have exited my custom menu.
Does anyone know how I can get the common event to be executed directly so I do not have tp close my menu in game for it to run?


Sorry for bad english :/
Hi,


In order to execute event commands, you need a Game_Interpreter object:

Spoiler
interpreter = Game_Interpreter.new


Create a new interpreter object that can process event commands.


interpreter.setup( List  [ , Event-ID ] )


Load up the interpreter with a list of event commands to execute:


List: List of event commands, for example  $data_common_events[ID].list


Event-ID (optional): ID of the map event "This Event" and Self Switch commands will refer to.


interpreter.update


Advances the event execution by one frame.


interpreter.running?


Returns whether or not the set up command list is still in process.



Your menu may also need to update a Window_Message instance if your events contain message commands. Otherwise your message commands will never finish.


In practice, this could look like this:

Spoiler
  ...


 


  def start


    ...


    @message_window = Window_Message.new


    @interpreter = Game_Interpreter.new


  end


  def command_task


    @interpreter.setup($data_common_events[1].list)


  end


  def update


    ...


    if @interpreter.running?


      @interpreter.update


      freezefix  unless @interpreter.running?


    end


  end


By default, interpreters are used on the map (maintained by $game_map) and in battle (maintained by $game_troop). Reserving a common event like you did will only set a variable that is repeatedly checked by the above when a new event could be set up (to avoid aborting the currently running event).


Sorry that the post was a bit rushed.
Spoiler
  ...


 


  def start


    ...


    @message_window = Window_Message.new


    @interpreter = Game_Interpreter.new


  end


  def command_task


    @interpreter.setup($data_common_events[1].list)


  end


  def update


    ...


    if @interpreter.running?


      @interpreter.update


      freezefix  unless @interpreter.running?


    end


  end

Hi, This code just freezes everything, I need to end the program with windows task maanger.

Q1.
Also do I need to create a new class that inherits from Game_Interpreter?


Q2.
 My current script. I am feeling very lost. Sorry.

Spoiler
@menu1.set_handler🙂task, method🙂start))
@menu1.set_handler🙂folmagi, method🙂command_folmagi))
@menu1.set_handler🙂folbruti, method🙂command_folbruti))
@menu1.set_handler🙂cancel, method🙂return_scene))
end

def freezefix
@menu1.active = true
end

def start
@message_window = Window_Message.new
@interpreter = Game_Interpreter.new
command_task
update
end

def command_task
@interpreter.setup($data_common_events[1].list)
end

def update
if @interpreter.running?
@interpreter.update
freezefix unless @interpreter.running?
end
end

Q3. I am trying to wait for an key input a inside another command. but i keep getting the Error 
" can't yield from root fiber"
What does that mean? word for word?


here is the code

Spoiler
Code:
class Scene_FolioMagi < Scene_MenuBase
  def start
    super
    create_background
    create_book_window
    wait
  end
  
  def create_book_window
    @bookwindow = Book_Window.new(80,80,400,400)
    @bookwindow.draw_text(10,0,80,24,"Group 1")
    @bookwindow.draw_text(10,40,80,24,"Group 2")
    @bookwindow.draw_text(10,80,80,24,"Group 3")
    @bookwindow.draw_text(10,120,80,24,"Group 4")
    @bookwindow.draw_text(10,160,80,24,"Group 5")
  end
  
  def wait
    Fiber.yield unless Input.trigger?(:B)
  end
end

class Book_Window < Window_Base
  def initialize(x, y, width, height)
    super
  end
end
@rpgmaker20221


I think it might help if you go back a step and tell us what you want to achieve instead of how you want to do it.


The only thing you told us so far is that you want to start a common event directly from inside a script instead of reserving it and allow the engine to start it - that is how you want to do "something", but it does not explain what that "something" is - what should the effect on the game for the player should be?


Because basically, doing that doesn't make any sense at all for me - common events are preset by yourself, so if you want to do something outside the gameloop, you should place that code directly into the script instead of having that code in a common event. If there is something you don't know how to do in script but know how to do in common events, then simply ask for the script equivalent of that - it will be easier than trying to make the common events run elsewhere.


Because the main advantage of the common event against direct scripting is the fact that the common events are already included in the main game loop and handled there with all security and safety features - messing with the main game loop is very bad unless you know exactly what you're doing, which is another reason not to try to redirect the common events to be run outside that loop.


And if you tell us what you want to achieve, then there is a good chance we might be able to tell you an alternative way instead of messing up the game loop (which will have a lot of side effects and reduce compatibility with other scripts).
Spoiler
—





Common Events and Custom Commands are managed straight from the module and are integrated into the main menu with ease. Here, you can set what display name will show up for the command, whether or not it can be disabled (and if it can be, disabling will work through a Switch), and the Event ID or Handler Method used.

I wanted to have a common event inside a command from the main menu. So when I push it. Like in the Yanfly Script (follow the link or look at the spoiler above) https://yanflychannel.wordpress.com/rmvxa/menu-scripts/ace-menu-engine/


This is how I want a command from my menu to work.
1. Calls a common event
2. Execute the event --> (this is inside the common event)


3. Shows the message I want.


4. returns to the main menu

help1.png
If I remember correctly, yanfly's menu ends and executes the common event on the map - which is exactly what will happen if you only reserve the common event instead of executing it directly.


There are two possible ways to do what you want, the easiest would be to make your 3 and 4 part of the common event - reserve the event, and at the end of the common event use a show text to display your message and then a script command to call the menu again.


Done correctly, the player might not even realise that he was redirected to the map for a short while.


If there is some reason why that is not possible, then search for tsukihime's blog and her scripts - she once wrote a menu interpreter script to allow common events to be executed on the menu. That might be a better solution than yanfly's menu script, or you can check her code to see how she handled this.
Thank you! I will try that! 🙂
I haven't looked at Yanfly's menu script you posted yet, so I couldn't take it into account. Andar already posted a solution, but I hope this post at least offers some explanation for the experienced issues:


Q2:


For short, you need to include a  super  call in your update method:


Each scene is processed by calling their "start" method to set them up, calling "update" in a loop while the scene is active, and then calling "terminate" to finish them (see Scene_Base, line 11ff.). All of these methods have a basic implementation in Scene_Base, which should usually be included in their child classes implementations.


The update method in particular handles frame timing, update of all pressed keys and update of your scenes Window objects, so usually update is called once per frame. In your case, skipping this turns your update process into an infinite loop.


@menu1.set_handler🙂task, method🙂start))


Calling start again after the scene has already been set up is usually a bad idea. The method is used to set up windows and other graphic related objects that should be disposed once they are no longer needed (as usually done in the terminate method), and recreating those may leave undisposed windows/viewports behind that may cause problems later.


If you want to recreate specific objects or reset something, you should do that in a separate method.


Q3:


Fibers are used to run code in several steps separated by Fiber.yield. For example:

Spoiler
fiber = Fiber.new {


  print "A"


  Fiber.yield


  print "B"


  print "C"


  Fiber.yield


  SceneManager.goto(Scene_Gameover)


}


fiber.resume


fiber.resume


fiber.resume


The first time you call  fiber.resume afterwards, "A" would be printed to the console. The second time "B" and "C" would be printed. The third time you call fiber.resume , the game would go to the gameover-screen.

While an interpreter internally uses a fiber to break parts of the event execution into steps, the main process (where Scenes are usually executed) is not handled as a fiber, and thus Fiber.yield does not work here.


Wait periods in a scene are usually achieved by just doing nothing for a certain amount of update cycles. Scenes like Scene_Battle achieve this by directly calling update_basic, which - like update - advances your game by one frame, but does not execute anything else you put into your update method.


If you want to end a scene, you can usually do that by going to or calling another scene or returning to the previous scene where this was called from (for ending a menu screen):
SceneManager.goto( Scene_XXX )


SceneManager.call( Scene_XXX )


return_scene


Calling a new scene allows you to later return to your previous scene by using return_scene (used for menus and for temporary leaving the map screen) whereas goto does not remember the old scene and is used to transfer to a successing scene without the intention to go back (like the transition to the gameover- or title screen).


Summarizing:


- You should add  super  at the start of your update-method.


- Instead of  Fiber.yield , you can use update_basic to wait for one frame in a Scene object. In non-scene update methods, you have to use if-statements to skip unwanted code.


- You should not call the start method of a scene without terminate-ing the scene first. And even then there probably is a better solution.


As far as event commands go, the scene may need to support some of them. Message commands require the scene to maintain a Window_Message object that can process the messages set up by the event. Also, using map related commands like "Set Move Route" won't have an immediate effect since the map processing is probably paused in your menu, so waiting for it's completion would take forever if you chose that option.


However, apart from "Wait for XY to finish", some script commands that require you to be on the map and the fact that you have to take into account your scene might get terminated after any frame you updated the interpreter in due to possible "Call Menu" event commands, event execution in your menu should work fine in VXAce if I'm not mistaken.
I haven't looked at Yanfly's menu script you posted yet, so I couldn't take it into account. Andar already posted a solution, but I hope this post at least offers some explanation for the experienced issues:


Q2:


For short, you need to include a  super  call in your update method:


Each scene is processed by calling their "start" method to set them up, calling "update" in a loop while the scene is active, and then calling "terminate" to finish them (see Scene_Base, line 11ff.). All of these methods have a basic implementation in Scene_Base, which should usually be included in their child classes implementations.


The update method in particular handles frame timing, update of all pressed keys and update of your scenes Window objects, so usually update is called once per frame. In your case, skipping this turns your update process into an infinite loop.



@menu1.set_handler🙂task, method🙂start))


Calling start again after the scene has already been set up is usually a bad idea. The method is used to set up windows and other graphic related objects that should be disposed once they are no longer needed (as usually done in the terminate method), and recreating those may leave undisposed windows/viewports behind that may cause problems later.


If you want to recreate specific objects or reset something, you should do that in a separate method.


Q3:


Fibers are used to run code in several steps separated by Fiber.yield. For example:

Spoiler
fiber = Fiber.new {


  print "A"


  Fiber.yield


  print "B"


  print "C"


  Fiber.yield


  SceneManager.goto(Scene_Gameover)


}


fiber.resume


fiber.resume


fiber.resume


The first time you call  fiber.resume afterwards, "A" would be printed to the console. The second time "B" and "C" would be printed. The third time you call fiber.resume , the game would go to the gameover-screen.

While an interpreter internally uses a fiber to break parts of the event execution into steps, the main process (where Scenes are usually executed) is not handled as a fiber, and thus Fiber.yield does not work here.


Wait periods in a scene are usually achieved by just doing nothing for a certain amount of update cycles. Scenes like Scene_Battle achieve this by directly calling update_basic, which - like update - advances your game by one frame, but does not execute anything else you put into your update method.


If you want to end a scene, you can usually do that by going to or calling another scene or returning to the previous scene where this was called from (for ending a menu screen):
SceneManager.goto( Scene_XXX )


SceneManager.call( Scene_XXX )


return_scene


Calling a new scene allows you to later return to your previous scene by using return_scene (used for menus and for temporary leaving the map screen) whereas goto does not remember the old scene and is used to transfer to a successing scene without the intention to go back (like the transition to the gameover- or title screen).


Summarizing:


- You should add  super  at the start of your update-method.


- Instead of  Fiber.yield , you can use update_basic to wait for one frame in a Scene object. In non-scene update methods, you have to use if-statements to skip unwanted code.


- You should not call the start method of a scene without terminate-ing the scene first. And even then there probably is a better solution.


As far as event commands go, the scene may need to support some of them. Message commands require the scene to maintain a Window_Message object that can process the messages set up by the event. Also, using map related commands like "Set Move Route" won't have an immediate effect since the map processing is probably paused in your menu, so waiting for it's completion would take forever if you chose that option.


However, apart from "Wait for XY to finish", some script commands that require you to be on the map and the fact that you have to take into account your scene might get terminated after any frame you updated the interpreter in due to possible "Call Menu" event commands, event execution in your menu should work fine in VXAce if I'm not mistaken.

Thank you all for giving me so much help and information 🙂


and special thank you to  Another Fen 🙂  


This topic can now be closed 🙂

Original thread: https://forums.rpgmakerweb.com/threads/execute-common-events-directly-when-called-in-a-method.72997/