User Tag List

Results 1 to 6 of 6

Thread: Pylacewing - global variables

  1. #1
    Clicker Fusion 2.5

    Join Date
    Jun 2006
    Posts
    402
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Pylacewing - global variables

    Hi,

    This is a test server that I am messing around on, and it seems declaring variables that are global isn't straight forward, see here:

    from twisted.internet import reactor

    from lacewing.server import ServerProtocol, ServerFactory, ServerDatagram

    class MyProtocol(ServerProtocol):
    clients = 0

    def connectionAccepted(self, kthxbye):
    clients += 1
    print str(clients)

    class MyFactory(ServerFactory):
    protocol = MyProtocol

    newFactory = MyFactory()
    reactor.listenTCP(6120, newFactory)
    reactor.listenUDP(6120, ServerDatagram(newFactory))
    print "Running"
    reactor.run()
    All I want it to do is add 1 to clients and then print everytime someone connects, but it always returns the error: local variable 'clients' referenced before assignment

    How can I make the variable global and accessible all over the application?

    Regards

  2. #2
    Clicker Multimedia Fusion 2 Developer

    Join Date
    Jun 2006
    Location
    Darlington, UK
    Posts
    3,298
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Pylacewing - global variables

    I think you need to put "self.clients" in order to get the class variable. Just "clients" could be trying to get a (currently non-existent) function-local variable.

  3. #3
    Clicker Fusion 2.5

    Join Date
    Jun 2006
    Posts
    402
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Re: Pylacewing - global variables

    - but that would mean the variable clients would be assigned to each client that joins, I want the clients variable to just be one.

    If it helps, the aim of the server code in my first post it to count howm any clients have joined in total.

  4. #4
    Clicker Fusion 2.5

    Join Date
    Jun 2006
    Posts
    402
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Re: Pylacewing - global variables

    Aha! Mathias fixed it for me!

    Code:
    from twisted.internet import reactor
    
    from lacewing.server import ServerProtocol, ServerFactory, ServerDatagram
    
    class MyProtocol(ServerProtocol):
    
      def connectionAccepted(self, kthxbye):
        self.factory.clients += 1
        print str(self.factory.clients)
    
    class MyFactory(ServerFactory):
      protocol = MyProtocol
      clients = 0
      
    newFactory = MyFactory()
    reactor.listenTCP(6120, newFactory)
    reactor.listenUDP(6120, ServerDatagram(newFactory))
    print "Running"
    reactor.run()

  5. #5
    Clickteam Clickteam
    LB's Avatar
    Join Date
    Jun 2007
    Location
    Richardson, Texas, North America
    Posts
    8,937
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Re: Pylacewing - global variables

    def connectionAccepted(self, kthxbye):

    Haha! Who thought of that?
    Working as fast as I can on Fusion 3

  6. #6
    Clicker Fusion 2.5 DeveloperAndroid Export ModuleSWF Export ModuleXNA Export Module
    Fusion 2.5 (Steam)
    Mathias's Avatar
    Join Date
    Jun 2006
    Location
    Copenhagen, Denmark
    Posts
    1,083
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)

    Re: Pylacewing - global variables

    Well, the proper method definition can be found here:

    http://aq.darkkiller.dk/lacewing/python/docs/lacewing.server.ServerProtocol-class.html#connectionAccepted

    Cough.

Similar Threads

  1. Confused about Global variables and strings
    By BrashMonkey in forum Multimedia Fusion 2 - Technical Support
    Replies: 3
    Last Post: 30th August 2011, 07:36 AM
  2. MMF2 - Global Variables
    By droberson in forum Multimedia Fusion 2 - Technical Support
    Replies: 4
    Last Post: 27th March 2010, 05:54 PM
  3. global variables
    By bufferOverflow in forum Extension Development
    Replies: 2
    Last Post: 19th June 2007, 08:01 AM
  4. Global Variables
    By stuckboy in forum Multimedia Fusion 2 - Technical Support
    Replies: 25
    Last Post: 17th July 2006, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •