Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / non-returning values
- - By il mietitore Date 2011-10-19 10:39 Edited 2011-10-19 13:55
I have a strange problem here.

I'll first copy you the code, than I'll tell you where is the problem:

class Finestra < Gosu::Window
  def update
    if (@menu == true) and (@click == "menu") and (button_down? Gosu::MsLeft) and (mouse_x > 300) and (mouse_x < 701) and (mouse_y > 434) and (mouse_y < 635) then
      @n_ga = true
      @creat_pas = 1
      @fed_name = ""
      @fed_tag = ""
      @down = false
    if (@n_ga == true) then
      case @creat_pas
      when 1 then self.key(@fed_nome, @down, @creat_pas)


  def key (m_t, m_down, m_pas)
    if (m_down == false) then
      if (button_down? Gosu::KbA) then m_t = m_t + "A"
                                       m_down = true 
      elsif (button_down? Gosu::KbB) then m_t = m_t + "B"
                                       m_down = true
    if (m_down == true) then
      unless (button_down? Gosu::KbA) or (button_down? Gosu::KbB) or (button_down? Gosu::KbC) or (button_down? Gosu::KbD) or (button_down? Gosu::KbE) or (button_down? Gosu::KbF) or (button_down? Gosu::KbG) or (button_down? Gosu::KbH) or (button_down? Gosu::KbI) or (button_down? Gosu::KbJ) or (button_down? Gosu::KbK) or (button_down? Gosu::KbL) or (button_down? Gosu::KbM) or (button_down? Gosu::KbN) or (button_down? Gosu::KbO) or (button_down? Gosu::KbP) or (button_down? Gosu::KbQ) or (button_down? Gosu::KbR) or (button_down? Gosu::KbS) or (button_down? Gosu::KbT) or (button_down? Gosu::KbU) or (button_down? Gosu::KbV) or (button_down? Gosu::KbW) or (button_down? Gosu::KbX) or (button_down? Gosu::KbY) or (button_down? Gosu::KbZ) then
        m_down = false
    return m_t, m_down, m_pas

Now: what this code is trying to do is to "write" a sentence. The sentence is the @fed_name variable. The initial value of this one is "" (nothing, yeah).

When the player presses a button on the keyboard (a letter, the space, or enter) the method "Key" add a letter to @fed_name. This means that if I press A, than Key does "" + "A" = "A". If I then press B, then the sentence will be "A" + "B" = "AB", and so on.

The m_down variable is necessary so that if you keep the button pressed than the game won't add the same letter at every frame.

What I know is that this code basically should work: now it's in a different method, so that every time I have to write something I can call the self.key method, and all is ok. But in a previous version that code was written directly where I had to write something, and all worked how it had to work.

What happens now is that when i return the variables, they assume their original value.

For instance: the inital value for @fed_name is "". I pass it to the method, and @fed_name becomes the m_t (message_text, that's why I named it that way). If I press A, than m_t becomes "A" ("" + "A", as I was saying), and m_down becomes true. Then, when I return the values, they return to be "" and false. I know this because if i make a test, adding "puts(m_t)" before the return, the message "A" appears in the command window, while if I add that instruction after the return then nothing appears in the window. And if I keep pressed the A key, than at every frame appears a "A" in the command window, and if I release it then in the command window appers anything (this also means that the m_down value turns back to false, because when it's true the game doesn't add the letter to the m_t string). I also tried to put a "puts(@fed_name)" after the "case @creat_pas" instruction, and also here the @fed_name variable appears to be empty.

So it's all linked to that return instruction.

Parent - - By Spooner Date 2011-10-19 19:49 Edited 2011-10-19 20:03
Well, you are not quite understanding how return works or when you need to pass around variables. To fix your code, I think you need to set the values of the instance variables to the values that are returned:

when 1 then @fed_nome, @down, @creat_pas = key(@fed_nome, @down, @creat_pas)

However, there is no reason at all to pass instance variables around in methods on the same instance, so you should just

when 1 then key

and just alter the instance variable values directly in the #key method!


Also, you can make the code a lot cleaner by creating a map of codes to letters, thus:

  KEY_MAP = {  Gosu::KbA => "A", Gosu::KbB => "B", Gosu::KbC => "C" , ...etc... }

  def key
    @button_held_down = KEY_MAP.keys.find { |k| button_down? k }
    if @button_held_down
      @m_t += KEY_MAP[@button_held_down]

OK, this is pretty inefficient and probably needs some re-writing for your game, but saves about 100 lines of code :D On the other hand, I'd recommend using Chingu, which manages keys and their conditions for you to a significant degree, which would simplify things even more.
Parent - - By il mietitore Date 2011-10-19 20:41 Edited 2011-10-19 20:54
wo! thanks a lot!

I have downloaded Chingu and I'm trying to understand how it works right now. I have to say that probably I'll use it for some other project, because I'm already at a good point with the game right now, and even if it isn't absolutely a perfect code, you know... it works, that's ok, and it's not the right time to begin using a new library. Just to avoid the mess, you know.. :\
Parent - By Spooner Date 2011-10-19 20:44
The thing with Chingu is that it is just an extension to Gosu; you can use as much or as little as you want to of it. You could, for example, just use the key handling stuff to get key events, rather than having to poll all keys yourself manually. However, if what I said about how better to handle keys in vanilla Gosu made sense to you, then that is fine too :)
Parent - - By jlnr (dev) Date 2011-10-20 07:28

>  KEY_MAP = {  Gosu::KbA => "A", Gosu::KbB => "B", Gosu::KbC => "C" , ...etc... }

Or just Gosu's button_id_to_char(id). Maybe even better would be to use Gosu::TextInput but that requires a different program structure, so you can leave that for later.
Parent - By Spooner Date 2011-10-20 15:42
Oops, shows how long it is since I actually did any Gosu :D Thanks for setting me right!
Up Topic Gosu / Gosu Exchange / non-returning values

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill