I want to await a signal, but I only want to wait for one with specific parameters set. For example, I have a ticket processing class that has the following signal

signal ticket_processed(id:int, message:String)

Now I have a bit of code that needs to await that signal, but only if the ticket id is set to a specific value

func send_message(ticket_id:int, text:String):
    
    ticket_processor.process_ticket(ticket_id, text)
    
    #I only want to wait for the signal where id == ticket_id
    await ticket_processor.ticket_processed
        my_label.text = <message returned from the signal>

I need to use await because this is going to be asynchronous. But I need to wait for a specific signal and I need to use the signal parameters too. Can I do this?

  • kitfox

    soundgnome 's reply looks like what you need, maybe something along the lines of:

    func send_message(ticket_id: int, text: String):
      ticket_processor.ticket_processed.connect(_on_ticket_processed)
      ticket_processor.process_ticket(ticket_id, text)
    
    func _on_ticket_processed(ticket_id: int, text: String, any_other_parameter_you_need):
      if id == ticket_id:
        label.text = <message returned from the signal>
        ticket_processor.ticket_processed.disconnect(_on_ticket_processed) # this instance no longer has a lunch ticket?

I don't think that's possible. If there aren't many ticket id's, you could emit different signals for each id.

Not sure if it would mess up your code structure but can't you just return the ticket/ticket ID in ticket_processor.process_ticket()? If you're not using the signal for anything else I think that would be much cleaner.

    You could also connect the signal to a method which could check the id before doing anything.

      DexyBentai Well, the idea is that many different objects could be submitting tickets to be processed. The idea of the ticket_id is like a lunch counter where you place your order and then get a ticket so you can come to get it when your number is called. So if a different number is called out, you ignore it and keep waiting for your ticket number.

        kitfox

        soundgnome 's reply looks like what you need, maybe something along the lines of:

        func send_message(ticket_id: int, text: String):
          ticket_processor.ticket_processed.connect(_on_ticket_processed)
          ticket_processor.process_ticket(ticket_id, text)
        
        func _on_ticket_processed(ticket_id: int, text: String, any_other_parameter_you_need):
          if id == ticket_id:
            label.text = <message returned from the signal>
            ticket_processor.ticket_processed.disconnect(_on_ticket_processed) # this instance no longer has a lunch ticket?