Arrays + Calls

● ARCHIVED · READ-ONLY
Started by Helladen 12 posts View original ↗
  1. I'm having trouble putting calls into a multi-dimensional array. Does anyone know a solution to turn a string to a call, or something else that could resolve this problem?
  2. Thanks. :D
  3. Just be careful with eval. It's a very consuming method and you should avoid by all means to place it on a method updated every frame.
  4. I'm not sure what the question is.

    Why do you want to evaluate a string as a method call?



    Code:
    def test
       p 'hello'
    end
    
    def another
       p 'another hello'
    end
    
    arr = [method(:another), method(:test)]
    arr[0].call
    arr[1].call
    
    arr.each {|m| m.call}
  5. I guess I'll post it here. I have it designed out completely, but I ran into a problem. I plan on sharing it anyway, so it won't hurt anything.

    It is not finished yet. I'm trying to get the scripts for variables working. I originally made it only to patch YEA - System Options which I had working, but I wanted to take it to the next level and allow all switches/variables to be used to call scripts. The options menu was going to do it for you automatically, but other things would require you to do an event script call. I can do all of that easy though.

    Spoiler
    # ▼ Switch and Variable Script Calls 1.0
    # -- Last Updated: 2012.07.30
    # -- Author: Helladen
    #==============================================================================
    # This will allow a developer to call specific things using switches and/or
    # variables. This has support for YEA's System Options script.
    #==============================================================================

    #------------------------------------------------------------------------------
    # ■ Aliased methods (YEA's System Options)
    #------------------------------------------------------------------------------
    # change_custom_variables
    # change_custom_switch
    #------------------------------------------------------------------------------

    #==============================================================================
    # ■ Instructions
    #==============================================================================
    # It will automatically call the script for you if you change them in
    # YEA's System Options menu, but you will need to script call if it is not.
    #
    # You need to add each switch/variable you want and what it will call in the
    # configuration below. Switches work differently than variables, they only call
    # when they meet the requirements, where variables will always call and embed
    # that value into the script call. For example, if you have a argument in your
    # script call it will place the variable in the argument. This does not have
    # support for multiple arguments, so you can't have a script call that has more
    # than one.
    #==============================================================================

    #------------------------------------------------------------------------------
    # ■ Event Script Calls
    #------------------------------------------------------------------------------
    # These are used to call the script for a variable or switch.
    #
    # call_switch_script(ID)
    # call_variable_script(ID)
    #
    # ID is the given switch or variable index number.
    #------------------------------------------------------------------------------

    #==============================================================================
    # ■ Configuration
    #==============================================================================
    module SCRIPT_CALLS
    WINDOW_VARIABLE = 3

    VARIABLES ={
    # -------------------------------------------------------------------------
    # index => call,
    # -------------------------------------------------------------------------
    3 => "$game_system.windowskin = Window",
    }

    #SWITCHES ={
    # -------------------------------------------------------------------------
    # index => [on/off, call],
    # -------------------------------------------------------------------------
    #1 => [true, ]
    #}
    end

    #==============================================================================
    # ■ Script Import
    #==============================================================================
    $imported = {} if $imported == nil
    $imported["Switch and Variable Script Calls"] = true
    #==============================================================================

    # Compatibility for YEA's System Options
    if $imported["YEA-SystemOptions"]
    class Window_SystemOptions < Window_Command
    #------------------------------------------------------------------------
    # alias method: change_custom_variables
    #------------------------------------------------------------------------
    alias change_custom_variables_al change_custom_variables
    def change_custom_variables(direction)
    change_custom_variables_al(direction)
    variable_script(SCRIPT_CALLS::WINDOW_VARIABLE)
    end

    #------------------------------------------------------------------------
    # alias method: change_custom_switches
    #------------------------------------------------------------------------
    #alias change_custom_switch_al change_custom_switch
    #def change_custom_switch(direction)
    # change_custom_switch_al(direction)
    #end

    def variable_script(num)
    #if $game_variables[SCRIPT_CALLS::WINDOW_VARIABLE] == 0
    # $game_system.windowskin = "Window"
    #else
    # $game_system.windowskin = "Window" + $game_variables[SCRIPT_CALLS::WINDOW_VARIABLE].to_s
    #end
    eval(SCRIPT_CALLS::VARIABLES[num][0].to_s + $game_variables[num].to_s)
    end
    end
    end

    Code:
    #==============================================================================
  6. Code:
    module SCRIPT_CALLS
    WINDOW_VARIABLE = 3
    end
    
    module OptionsHelper
    OptsIndex = {
    3 => :set_windowskin
    }
    module_function
    def set_windowskin(idx)
    $game_system.windowskin = ('Window' + idx.to_s)
    end
    def callfunc(idx, *args)
    send(OptsIndex[idx], *args)
    end
    end
    
    $imported = {} unless $imported
    $imported["Switch and Variable Script Calls"] = true
    if $imported["YEA-SystemOptions"]
    class Window_SystemOptions < Window_Command
    
    alias change_custom_variables_al change_custom_variables
    def change_custom_variables(direction)
    change_custom_variables_al(direction)
    variable_script(SCRIPT_CALLS::WINDOW_VARIABLE)
    end
    
    def variable_script(cmd_id, *args)
    OptionsHelper.callfunc(cmd_id, *args)
    end
    end
    end
    You can do something like this.

    And we need Ruby BBCode....
  7. eval is evil and almost never necessary. Unless you are creating an interactive interpreter prefer function objects.
  8. This is the fixed version.

    Spoiler
    Code:
    #==============================================================================
    # ▼ Switch and Variable Script Calls 1.0
    # -- Last Updated: 2012.07.30
    # -- Author: Helladen, Cidiomar
    #==============================================================================
    # This will allow a developer to call specific things using switches and/or
    # variables. This requires YEA's System Options script. I may eventually add
    # support for things outside of this, but at the moment I am not skilled enough
    # to do so.
    #==============================================================================
    
    #------------------------------------------------------------------------------
    # ■ Aliased methods (YEA's System Options)
    #------------------------------------------------------------------------------
    # change_custom_variables
    # change_custom_switch
    #------------------------------------------------------------------------------
    
    #==============================================================================
    # ■ Instructions
    #==============================================================================
    # You must specify an ID of a variable or switch you want to check for, this
    # then allows you to call specific things after they have been changed in the
    # System Options menu.
    #==============================================================================
    
    #==============================================================================
    # ■ Installation
    #==============================================================================
    # Place below Yanfly Engine Ace - System Options.
    #==============================================================================
    
    #==============================================================================
    # ■ Configuration
    #==============================================================================
    module OptionsHelper
    VarOptsIndex = {
    	3 => :set_windowskin
    }
    
    SwitchOptsIndex = {
    
    }
    
    module_function
    def set_windowskin(idx)
    	if $game_variables[idx] == 0
    	 $game_system.windowskin = "Window"
    	else
    	 $game_system.windowskin = "Window" + $game_variables[idx].to_s
    	end
    end
    
    def call_script(idx, type)
    	if type
    	 return unless SwitchOptsIndex[idx] != nil
    	 send(SwitchOptsIndex[idx], idx)
    	else
    	 return unless VarOptsIndex[idx] != nil
    	 send(VarOptsIndex[idx], idx)
    	end
    end
    end
    
    #==============================================================================
    
    #==============================================================================
    # ■ Script Import
    #==============================================================================
    $imported = {} if $imported == nil
    $imported["Switch and Variable Script Calls"] = true
    #==============================================================================
    
    # Compatibility for YEA's System Options
    if $imported["YEA-SystemOptions"]
    	class Window_SystemOptions < Window_Command
    	 #------------------------------------------------------------------------
    	 # alias method: change_custom_variables
    	 #------------------------------------------------------------------------
    	 alias change_custom_variables_al change_custom_variables
    	 def change_custom_variables(direction)
    		change_custom_variables_al(direction)
    
    		ext = current_ext
    		var = YEA::SYSTEM::CUSTOM_VARIABLES[ext][0]
    
    		# Call the variable's script.
    		OptionsHelper::call_script(var, false)
    	 end
    
    	 #------------------------------------------------------------------------
    	 # alias method: change_custom_switches
    	 #------------------------------------------------------------------------
    	 alias change_custom_switch_al change_custom_switch
    	 def change_custom_switch(direction)
    		change_custom_switch_al(direction)
    
    		ext = current_ext
    		switch = YEA::SYSTEM::CUSTOM_SWITCHES[ext][0]
    
    		# Call the switch's script.
    		OptionsHelper::call_script(switch, true)
    	 end
    	end
    end
    Does anyone know of a way to check variables/switches changing? If not do I need to make my own function to do this?
  9. Look at Game_Switches and Game_Variables classes and how they are setting the values.

    It is already telling the map to refresh, so you just need to add to it.
  10. Thanks.
  11. This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.