Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: azdesign on August 26, 2012, 01:07:41 am

Title: Strange bug, or just me ?
Post by: azdesign on August 26, 2012, 01:07:41 am
Create a new project, insert new script sheet, write :

$dialogue = {
 :Aaaa => 'aasd',
 :Bbbb => 'qweq'
}
class Game_Temp
 def message_text=(string)
   if string =~ /^\[:(.+)\]$/
     @message_text = $dialogue[$1.to_sym]
   else
     @message_text = string
   end
 end
end


then, create 2 event :
1. Show Text [:Aaaa]
2. Script print $dialogue

Pictures :
Spoiler: ShowHide
(http://img824.imageshack.us/img824/2233/44148715.png)
(http://img684.imageshack.us/img684/8181/55801987.png)
(http://img443.imageshack.us/img443/2020/82130390.png)



Picture explained :
1. play the game, go to event 2, trigger it, you should see a console window popped out and display : {:Aaaa=>"aasd",:Bbbb=>"qweq"}
2. Go to event 2, trigger it, text box should appear displaying "aasd"
3. go back to event 2, trigger it, you should see a console window popped out and display : {:Aaaa=>"",:Bbbb=>"qweq"}

WTF the value of :Aaaa was gone, how come ? I tried creating another variable called $dialogue2 = $dialogue for backup, after step2, the value of :Aaaa in $dialogue2 was also gone! Someone can explain this ? I'd be happy if someone can fix it  :D
Title: Re: Strange bug, or just me ?
Post by: ForeverZer0 on August 26, 2012, 03:15:18 am
Does it still do it if you use this:
class Game_Temp
  def message_text=(string)
    if string =~ /^\[:(.+)\]$/
      @message_text = String.new($dialogue[$1.to_sym])
    else
      @message_text = string
    end
  end
end
Title: Re: Strange bug, or just me ?
Post by: azdesign on August 26, 2012, 03:31:49 am
Yay it works, Thanks  :haha:

Can you explain what caused it ? I mean, it delete the value of the origin of the text, even delete another clone which is not directly involved ($dialogue2). Its strange really

Title: Re: Strange bug, or just me ?
Post by: ForeverZer0 on August 26, 2012, 11:20:58 am
It was setting the text to the value in in the hash, not a copy of the value in the hash. Therefore when the text is cleared or changed, it changes the value in the hash as well.

Take a look at some tuts to see the difference between copying by reference, and copying by value. 9/10 if are ever get a similar problem, you can solve it by setting a variable to $my_variable[INDEX].clone instead of just $my_variable[INDEX]. It creates and returns a new and separate instance of the original, and leaves the original alone.