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 :
(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
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
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.