Strange bug, or just me ?

Started by azdesign, August 26, 2012, 01:07:41 am

Previous topic - Next topic

azdesign

August 26, 2012, 01:07:41 am Last Edit: August 26, 2012, 01:10:38 am by azdesign
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





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
~ Check out my deviantart http://my-az-design.deviantart.com/ ~

ForeverZer0

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
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

azdesign

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

~ Check out my deviantart http://my-az-design.deviantart.com/ ~

ForeverZer0

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.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.