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.

Scripting Process : Adding Attributes

A method is like a label in events. Its a block that is executed when it is called.

Try this for your code:
Code:
module Energy
  Start = {}
  Start.default = 100
  Gain = {}
  Gain.default = 10
  def self.maxe(actor)
    n = Start[actor.id]
    n += Gain[actor.id] * actor.level
    return n
  end
end

class Game_Actor
  attr_reader :energy
  alias_method :seph_energy_gmactr_setup, :setup
  def setup(actor_id)
    seph_energy_gmactr_setup(actor_id)
    @energy = maxe
  end
  def maxe
    return Energy.maxe(self)
  end
end

Much cleaner than the code you had, but has same effect.
   
 
ok, it is alot less code, and thank you, but I have two problems;
one, how do I determine energy vs max energy
two, when I level someone up, their energy is still 110
EDIT: three, after looking at the code you gave me, i don't understand what it means...

any ideas?
 
1) Simple enough.

Code:
actor.energy = current energy
actor.maxe = max enery

2) The @energy is still 110, but the max isn't. You either have to reset the @energy = maxe when you level up, or by some other means.
 
I'm retarded. I was calling up actor.energy as the max in the menu. woops. now it works.and it does go up as you level. thank you for your help. would I apply the same things to game_enemy, but actor, for enemies?
 
alright, i thought so, i just haven't got a chance to check it yet. Is there a way to make it so items add or subtract from an attribute that's made? I know I could just make a clause to check if its equipped, but i was wondering if there was a n easier way to do it.
 
If you are talking about altering the maxe method, just alias it.
Code:
class Game_Actor
  alias_method :seph_energyaddition_gmactr_maxe, :maxe
  def maxe
    n = seph_energyaddition_gmactr_maxe
    # Make your additions here
    return n
  end
end

When it comes to restoring energy by item effects, its a matter or aliasing the item_effect method. Let me know if that's what you want, as it's a slightly longer process.
 
well, knowing that would be a helpful tidbit of knowledge, if you wouldn't mind showing me.

What i was asking was to just raise the stat with equipment. I figured that out, but the problem is, if I equip something that adds 10 to energy, heal my energy, then remove it, I have more energy than max. Is there a way to fix this?
 
Ok. Restoring an energy from an item, we are once again, going to add a stat to our items for restoring enery.

Code:
class RPG::Item
  Energy_Restore = {}
  Energy_Restore.default = 50
  def energy_restore
    return Energy[@id]
  end
end

Next, it's all just aliasing the item_effect method from Game_Battler 3.
Code:
class Game_Battler
  alias_method :seph_energyrestore_gmbtlr_ie, :item_effect
  def item_effect(item)
    # This will get the result of the original method call.
    # If its true, the item was used on the actor. 
    # If false, don't bother restoring energy because it "missed"
    result = seph_energyrestore_gmbtlr_ie(item)
    if result
      self.energy += item.energy_restore
    end
    return result
  end
end

Simple enough? (Now, by using self.energy, I am calling an def energy= method we haven't created yet, but we will below).

--------------------------

Ok. It's simple enough. We could have used attr_accessor or attr_writer for aliasing our @energy instance. We only used a reader. Just like in rmxp, they don't do this with maxhp or maxsp as well, since the max value can change. We will do the same thing they did with our energy instance.

Code:
class Game_Battler
  def energy=(energy)
    @energy = [energy, maxe].min
  end
end

Simple, no? How this works is rather making @energy = to whatever value we send, we are making sure that value does not exceed our maxe. We simply but the value we are setting it to and the max value in an array and have it pick the smallest value.

That should take care of that. The other way to do this would be to not even use a attr_reader method, but this isn't the best way because if you ever manually set the @energy instance (in the class, using @energy = vs. self.energy =), you can make your @energy instance greater than the max instance. This could be a second error catcher, that makes sure no outside class never reads @energy > maxe, or any where in the class you call self.energy vs. @energy.

Code:
class Game_Battler
  def energy
    return [@energy, maxe].min
  end
end

Work similar to your setting method, but is a second error catcher. Most of the time this is just overkill however. Just make sure no where you aren't using "@instance =" if you have a "self.instance=" method.
 
I'm having a couple of problems;

One, I'm guessing your code sets it up so items defaultly heal 50 energy, regardless. I killed a players energy(reduced it to 0) and used potion on her. I get this error:

Script 'Scene_Item' line 314: ArgumentError occured
wrong number of arguments (2 for 1)

Two, your code didn't change anything for when I equipped the item. Should I call something when things get equipped?
 

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