How to recover every person's MP; Help with yields

● ARCHIVED · READ-ONLY
Started by Titanhex 7 posts View original ↗
  1. Hey, I was wondering if there's a way to access and change the MP of the characters not in your game party.

    I'm sure there is, but I couldn't find it in Game Actor or Game Actors.

    What I want to do is every few seconds replenish the mana of every actor, not just the ones in your party.

    I understand this may use a Yield function, which I learned a while back but forgot exactly how to use it. I know it can operate a block of code on a list, but I'm having trouble reading code with the yield function.

    I'm also doing these functions from a unique module, and not from inside any of the classes.

    Any help is appreciated, thanks.
  2. you can access if from $game_actors (it's a list of all actors you have in database in the form of Game_Actor instance).

    and since Game_Actor is a child class of Game_Battler and Game_Battler is child class of Game_BattlerBase

    you can access Game_BattlerBase method:

    specifically:

    def mp=(mp)

    you can call it like this (untested)

    i'm using each do method btw

    $game_actors.each do |actor|   actor.mp += 10endit will restore 10 mp to each actor

    $game_actors.each do |actor|   actor.mp = actor.mmpendit will restore each actor to max mp
  3. See, I honestly thought that would work too.

    Unfortunately I get this error:

    undefined method 'each' for ...

    Finishes off by referencing game actors.

    There's must be another way to go through each actor. Even searching the entire RGSS code I couldn't find one instance of $game_actors.each.

    I do however see it used with instances of {|id| $game_actors[id]}
  4. Game_Actors is not enumerable, but it contains a @data array that is. Game_Actors is simply an array wrapper set up to dynamically assign a default for an element the first time it is accessed, presumably since the object is serialized and procs cannot be serialized (so a simple Hash with default proc wouldn't work).

    You can iterate all currently existing actors if you just define the 'each' method to return an enumerator for this data set:

    Code:
    class Game_Actors  def each    @data.compact.each  endend
    Which would cause Game_Actors#each to return an enumerator for the array of currently existing actors. Externally, it will function just how you would expect it to.
  5. Thanks Mithran. I tried this out and noticed it wasn't working with the do iterator.

    A little investigation showed me that the wrappers that define .each have {|x| yield x}

    Of course I have limited understand of yield, but I decided to tack one on for actor and it worked.

    I definitely need to study yield more.
  6. ah yes. forgot again that Game_Actors is NOT real array :D . I do that a lot. >.<.

    thx mithran for the code. it helps a lot.
  7. 'yield' just invokes the given block. Anything following 'yield' will be passed into the block as an argument. The local scope of the evaluation is the scope the block was created at. It basically just allows you to invoke a block without explicitly binding it proc and calling it. Here, you are just iterating through the @data set and passing each value up as an argument to invoke the supplied block. Since the supplied block is also intended for an "each", you are effectively just iterating the existing @data set.

    My (bad) example above returns an enumerator. I forgot that enumerators won't take a block unless explicitly reinvoked again using another enumerator method, so, to invoke:

    class Game_Actors def each @data.compact.each endendYou'd actually have to use:
    Code:
    $game_actors.each.each { |actor| some_method }
    Which is rather silly.Conversely, calling yield explicitly will cause a localjump error if no block was supplied to the method (it will not automatically generate an enumerator).

    Binding and transparently passing the block would work for both:

    Code:
    class Game_Actors  def each(&    @data.compact.each(&  endend
    You don't really need an enumerator for this collection, though, so simply iterating the data set and using yield explicitly is probably the better choice.