Just as much as the title says, I have a problem. How and why do these errors happen? What does it mean by "can't add a new key into hash during iteration"?
Game Interpreter: Can't add a new key into hash during iteration
Learning Ruby and RGSSxThat's a protection... You're iterating the hash so it stops you from editing it until you finish the iterations to avoid possible problems.
One possible problem would be developing an infinite loop by adding a new key with each iteration which effectively makes the hash contents infinite. Imagine being told to check the name of each guest on a list but each time you check a guest, a new one is added to the list...
one possible workaround would be using a 2nd hash for the iteration
Anyway, can you post the actual code that you're running?
One possible problem would be developing an infinite loop by adding a new key with each iteration which effectively makes the hash contents infinite. Imagine being told to check the name of each guest on a list but each time you check a guest, a new one is added to the list...
one possible workaround would be using a 2nd hash for the iteration
Code:
hash1 = {}hash2 = hash1 #or if you want hash1.clonehash2.each do |key,value|end
Hi,
This error will usually appear when you try to add a new event to the map within a parallel process event. Events are stored in a hash that is iterated during the event update phase (which includes the execution of parallel process event pages).
I don't know exactly why it is prohibited though. Unlike Ruby 1.8 hashes seem to contain lists of the insertion order of all keys, the risk of screwing the iteration process by rehashing should be gone as far as I can see.
Modifying an iterated enumerable is not always a good idea though. For example:
array = [1, 2, 3]array.each { |i| p i; array.push(i) } # => 1 2 3 1 2 3 1 2 3 1 ... (endless)array = [1, 2, 3]array.each { |i| p i; array.unshift(0) } # => 1 1 1 1 1 1 1 1 1 1 ... (endless)array = [1, 2, 3]array.each { |i| p i; array.shift } # => 1 3 (2 is skipped after decreasing its index) Edit: Engr. Adiktuzmiko was faster
This error will usually appear when you try to add a new event to the map within a parallel process event. Events are stored in a hash that is iterated during the event update phase (which includes the execution of parallel process event pages).
I don't know exactly why it is prohibited though. Unlike Ruby 1.8 hashes seem to contain lists of the insertion order of all keys, the risk of screwing the iteration process by rehashing should be gone as far as I can see.
Modifying an iterated enumerable is not always a good idea though. For example:
array = [1, 2, 3]array.each { |i| p i; array.push(i) } # => 1 2 3 1 2 3 1 2 3 1 ... (endless)array = [1, 2, 3]array.each { |i| p i; array.unshift(0) } # => 1 1 1 1 1 1 1 1 1 1 ... (endless)array = [1, 2, 3]array.each { |i| p i; array.shift } # => 1 3 (2 is skipped after decreasing its index) Edit: Engr. Adiktuzmiko was faster
A good example of infinite loop due to adding a new item per iteration 🙂
Thanks to both of you. I will try to fix and see how it goes. Also, after making the event that calls the script call into action event rather than a parallel process, somehow the error stops.
hash1 = {}
hash2 = hash1
Just wanted to straighten out that by doing this you just give your hash an alias name, you don't clone it (so you'd probably have to use hash1.clone)
hash2 = hash1
Just wanted to straighten out that by doing this you just give your hash an alias name, you don't clone it (so you'd probably have to use hash1.clone)
Original thread: https://forums.rpgmakerweb.com/threads/game-interpreter-cant-add-a-new-key-into-hash-during-iteration.38449/