$data_weapons[$data_weapons.size] = $data_weapons[id].dup
$data_weapons[$data_weapons.size - 1].id = $data_weapons.size - 1
This is how I am trying to create a new weapon but it is still affecting the original.
Am I missing something?
I do not have rpg maker in front of me but,
try this:
new_weapon = $data_weapons[id].dup
new_weapon.id = $data_weapons.size
$data_weapons[$data_weapons.size] = new_weapon
or this(appending to the array):
new_weapon = $data_weapons[id].dup
new_weapon.id = $data_weapons.size
$data_weapons << new_weapon
hope this helps 😀
I figured it out already (and a little shorter too.) Thank you though.
Yes, I did know that about the reference variable I just forgot the .dup here
params = base.params.dupBTW This won't work, I can tell just looking at it.
prefix = new_weap(id)prefix = create_prefix(prefix)suffix = new_weap(id)suffix = create_suffix(suffix)You are calling this twice
new_weap(id)which is creating a new weapon every time it is called.
Also you do not need the () brackets when using push they're optional
I am having problems combining the features though.
Here is the method I'm using
I was looking to far into it. I didn't need to add them together Rpg Maker does that for me, here is the correct way to add the features.
#----------------------------------------------------------------------------- # * Create Features #----------------------------------------------------------------------------- def create_feats(base, prefix, suffix) feats = base.features.dup pre = $data_weapons[prefix].features.dup unless prefix == 0 suf = $data_weapons[suffix].features.dup unless suffix == 0 pre.each { |pf| feats.push pf } unless prefix == 0 suf.each { |sf| feats.push sf } unless suffix == 0 return feats endThe method above it is checking if the codes match. if so it adds the values. What it does not currently do is add the new code to the feats array if the code does not exist.
How can I do this correctly?
I didn't know that about push XD
Learn new things every day XD
"Learn new things every day XD" Me too. Thanks again for the help.
I'm not sure what you mean by the correct way to add features.
You should avoid duping mutable objects unless you set some rules.
The purpose of instance items is exactly to change them. Or at least, that's what I think they are for anyways.
By providing a deep copy, you can guarantee that if you grab an instance, it is its own entity and has nothing to do with any other object in the game.
If you're just attaching things to your instances, like a prefix or a suffix, that's ok.
However, once you modify any of the values, you're setting yourself up for bugs.
For example, let's say you have your Hand Ax and you duped its name. Strings are expensive; an empty string takes up 10 bytes when serialized and an extra byte for each ASCII character! Descriptions are long and therefore can get pretty expensive once you have 100 copies of the weapon.
Now you go and take your second Hand Ax and modify its name to Hand Ax+1.
You've suddenly changed the first ax's name as well to Hand Ax+1 because they both point to the same string.
This is not a problem if, as mentioned, you just "attach" things to your instance. Or stick with integers and symbols for ID purposes.
For example, instead of changing the second axe's name, you instead define a method that will build the name whenever it's requested
def get_name return "%s +%d" %[@name, @level]endYou do not modify the name. You simply read it and return a new value.But you will need to make sure that this is consistently followed by anyone that wishes to write an add-on using your script as a base.
The trade-off here is time vs space. You save memory and storage space (in the case of save files) by duping references, at the expense of having to generate the name at run-time everytime you need it. It is up to you which is more important.
Would you suggest that I do this in the base RPG modules.
like so
#==============================================================================# ** RPG::EquipItem#------------------------------------------------------------------------------# A superclass of weapons and armor.#==============================================================================class RPG::EquipItem < RPG::BaseItem #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_accessor 😛refix attr_accessor :suffix #-------------------------------------------------------------------------- # * Aliasing Method: Initialize Proccessing #-------------------------------------------------------------------------- alias :rpgeii416516161 :initialize #-------------------------------------------------------------------------- # * Initialize Proccessing #-------------------------------------------------------------------------- def initialize super @prefix = 0 @suffix = 0 end endI have never used them before so I'm not to sure about the "Hidden Classes"
Looking now. That's quite a bit of thumbing threw. Thanks for the read.
Edited~
Alright, First of thank you Tsukihime. That was a lot to ingest. I will really have to think about how I am going to work things before I do any rewriting.