Author Topic: Strange bug, or just me ?  (Read 949 times)

Offline azdesign

  • Awakened Visionist
  • **
  • Posts: 64
  • LV: 2
  • Gender: Male
    • View Profile
Strange bug, or just me ?
« on: August 26, 2012, 07:07:41 AM »
Create a new project, insert new script sheet, write :

Code: [Select]
$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 :
(click to show/hide)


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
« Last Edit: August 26, 2012, 07:10:38 AM by azdesign »
~ Check out my deviantart http://my-az-design.deviantart.com/ ~

Offline ForeverZer0

  • CP's Pedophile
  • Global Moderator
  • Guardian of Chaos
  • ****
  • Posts: 3255
  • LV: 298
  • Gender: Male
  • Remember you are unique, just like everyone else.
    • View Profile
Re: Strange bug, or just me ?
« Reply #1 on: August 26, 2012, 09:15:18 AM »
Does it still do it if you use this:
Code: [Select]
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.

Offline azdesign

  • Awakened Visionist
  • **
  • Posts: 64
  • LV: 2
  • Gender: Male
    • View Profile
Re: Strange bug, or just me ?
« Reply #2 on: August 26, 2012, 09: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

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

Offline ForeverZer0

  • CP's Pedophile
  • Global Moderator
  • Guardian of Chaos
  • ****
  • Posts: 3255
  • LV: 298
  • Gender: Male
  • Remember you are unique, just like everyone else.
    • View Profile
Re: Strange bug, or just me ?
« Reply #3 on: August 26, 2012, 05:20:58 PM »
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.