RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
[VXA] Problem with alias and stack level

0 Members and 1 Guest are viewing this topic.

**
Rep:
Level 41
Addict
Hey everyone!

I´m currently working on an item weight script and have a problem when aliasing the "gain_item" method from Game_Party. I sum up all item weights and want to compare to the sum of all actors carrying capacity combined. This works quite well up to the point where I try to access any actor of the party. If I do this in my aliased function I´ll always end up in "stack level too deep". I can access the actors if I edit the original code, but thats no solution I want to stick with.

I really don´t get it and I tried to fix this for almost two hours yesterday.
I created a minimal testcase that reproduces my problem with a few lines of code.

Can anyone here help me out?
This is probably something really obvious I just don´t see after starring at the freaking code for hours.

Heres the testcase:
Code: [Select]
class Game_Party < Game_Unit
  alias :stack_level_test_case_gain_item :gain_item
  def gain_item(item, amount, include_equip = false)
    stack_level_test_case_gain_item(item, amount, include_equip)
    print(leader.name)
  end
end


EDIT:
Ok, I kind of fixed the problem for now, but I still don´t know why this was happening. Would be glad if someone could tell me.

Fixed version:
Code: [Select]
class Game_Party < Game_Unit
  alias :stack_level_test_case_gain_item :gain_item
  def gain_item(item, amount, include_equip = false)
    stack_level_test_case_gain_item(item, amount, include_equip)
    return unless item
    print(leader.name)
  end
end
« Last Edit: March 12, 2013, 08:30:35 PM by p3king »

*
Rep:
Level 82
Rewriting the code myself, the error is only caused when I add the print(leader.name) line. In fact, even using leader.name or even leader (either trying to print the object or not) is causing the issue.

I'm assuming that the issue isn't in the leader method itself, as that calls on another method which is to  return an array and itself then returns the 0th element.

leader calls the following method, which in turn calls another method:

Code: [Select]
  def battle_members
    all_members[0, max_battle_members].select {|actor| actor.exist? }
  end

  def all_members
    @actors.collect {|id| $game_actors[id] }
  end

I can't be certain as to the cause of the issue without some extra testing but you can maybe use that as a starting grounds. It's getting late for me to delve too much into the issue at the minute, I'm afraid. Perhaps someone else can use that to explain the problem too.
(Why do I always feel like it's the end of the world and I'm the last man standing?)

*****
Rep:
Level 84
This text is way too personal.
Bronze - GIAW 11 (Hard)Silver - GIAW Halloween
Logan was headed in the right direction; the error concerns itself with how $game_party and $game_actors works, not with the alisaed method itself.

That weird error has nothing to do with the aliased method, but rather the weird way that RPG Maker handles blank equips. You'll notice that if all your actors have all 5 equipment slots filled at the start of the game, the stack error won't occur. This is because when an actor is first being setup, you'll notice that RPG Maker tries to equip all the actor's items using the init_equips command. As a failsafe, RPG Maker then refreshes the actors items, releasing any unequippable items by first seeing if they can be traded within the party, and then shoving them back into the inventory if they can't. Obviously, nil items are unequippable, so they are thrown into the inventory as empty space. As an unintended consequence of this, when RPG Maker attempts to assign the nil item to an actor and cannot, the gain_item/lose_item methods are used on the nil object to store it in the inventory.

While the above is harmless in regular circumstances, during startup the actor has not been fully initialised or setup; thus, when we attempt to find the name of the leader of the party through "p leader.name", we are searching through the archives of Game_Actors. The way Game_Actors works is that it checks whether or not a form of the Actor is already cached; if it is not, then a new actor is made in its stead in the form of Game_Actor.new. Seeing as our actor was never fully initialised (it stopped short after our gain_item method attempted to search for our leader's name), RPG Maker attempts to make a new Game_Actor to fulfill the role of the leader, and then our gain_item method tries to get the name of the party leader (who still doesn't yet exist), so Game_Actors makes a new Game_Actor, and so on, in a recursive loop until the stack error finally occurs. 

I hope my above explanation was clear enough, and if any more talented scripters find some errors that I made in my explanation, please point them out, as I would love to learn more about this bizarre and very specific error.

**
Rep:
Level 41
Addict
Wow thanks for the explanation!

I also suspected the starting items to be the cause after posting because when I printed the item I got some nil at the start, but I really couldn´t tell why.
So I got an endless loop because I created an actor when creating an actor :)

I knew it wasn´t related to the leader method itself but more every way I tried to access an actor with. The snippet was really only a sample to reproduce this.


Thanks for the help guys, I think this is solved.