Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

help with eval

Hey, thanks for reading this :)
I have this little code, which logs the sent parameters(*args) whenever Window_something.new(*args) is called.
However, it doesn't seem to work- $window_parameters stays empty..
[rgss]class Window
 
  def self.inherited(subclass)
    $window_parameters = {} if $window_parameters.nil?
    code = "class #{subclass}" + "\n"
    code +=  "alias win_init initialize" + "\n"
    code +=  "def initialize(*args)" + "\n"
    code +=    "$window_parameters[#{subclass}] = *args" + "\n"
    code +=    "win_init(*args)" + "\n"
    code +=  "end" + "\n"
    code += "end" + "\n"
   
    eval(code)
 
  end
 
end
[/rgss]
 
Thanks,
The main problem seems to be that I used eval rather than
class_name.class_eval(code)

I'm still getting this really weird error:
If I type the code manually for every class, it works perfectly.
But when doing so in class_eval, whenever I call initialize I get 'wrong number of arguments'. I've printed the sent arguments to test it and they're fine, but rmxp reads them as an array- so rather than 4 arguments it gets '1 argument for 4'.
I'm currently stuck, as I can't figure out how to turn the array back to a pointer/ list of arguments x.x

Here's the code
[rgss]class Window
  # every sub-class of Window is saved in window_types.
  def self.inherited(subclass)
    $window_parameters = Hash.new if $window_parameters.nil?
    $window_types = [] if $window_types.nil?
    $window_types.push(subclass.to_s)
  end
end
 
#----------------------------------------------#
# this bit goes above main,
# it's called after all windows have been defined.
#----------------------------------------------#
def make_window_args
 for winclass in $window_types
    code =  "alias win_init initialize" + " ; "
    code +=  "def initialize(*args)" + " ; "
    code +=    "args = *args.clone" + " ; "
    #code +=      "p args" + " ; "
    code +=    "$window_parameters[self.class.name] = *args" + " ; "
    code +=    "win_init(*args)" + "; "
    code +=  "end"
    (eval(winclass)).class_eval(code)
    code = "def clone" + " ; "
    code +=   "args = $window_parameters[self.class.name]" + " ; "
    #code +=      "p args" + " ; "
    code +=   "new = (self.class).new(args)" + " ; "
    code +=   "return new" + " ; "
    code += "end"
 
    (eval(winclass)).class_eval(code)
   
  end
end
[/rgss]
 
I feel silly.. I only had to do
code += "new = (self.class).new(*args)" + " ; "

This is actually working now :)
I've been banging my head on the wall with this 'clone script' for a week now..
I can clone windows, next step- clone scenes, then profit :D
This is for my an auto-save/load script,which (hopefully) will be able to save in the middle of battle, etc.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top