Resultados 1 al 1 de 1

Tema: Blender game networking

  1. #1
    Fecha de ingreso
    Apr 2002
    Mensajes
    26,002

    Blender game networking

    blender game networking
    By Brian cordell hynds.




    introduction
    Th rouge this tutorial, we Will create a working server/client for use in the Blender Game Engine. You Will be introduced todo Low level Python networking th rouge the use of sockets. Everything Will be covered step-by-step and illustrated, and the completed tutorial files are available for download.
    part. Networking and scripting
    Let take a minute todo explain a few things about networking. I could write an entire encyclopedía on computer networking, but i dont fel you ned todo know much more than the very basic of what a packet is. You dont ned todo fully understand this next part, but it included for the sake of those who may. When a computer neds todo send data todo another computer, it sends a packet of información across a Physical medium todo the receiving end. This packet is encapsulated with Header información that tells where the packet neds todo go. When the packet is sent and received, it flows th rouge a standard known as the osi model. The only thing you ned todo know about the osi model (and the TCP/IP model) is that it works. Packets are sent th rouge a service that is associated todo a port. A port is Simply a number that TCP uses todo send and receive información.

    Now that ive summarized about six months of networking school into one paragraph, take a look at this next diagram: this is a simple chart demonstrating how we Will be setting up our server and client. The client Will connect todo the server and a socket connection Will be established. The client Will send a string of data todo the server. The server Will then read the data and send it right bak todo the client and close the connection. That it. This model is known as an echo server, since it sole function is todo repeat the data bak todo the client.



    First, fire up Blender. To avoid the hassle of going step-bystep th rouge creating a game menú, ive taken the liberty of creating one for demonstration purposes. You can download the file from the enlace at end of the article.

    Lets take a look at what we have here. In a nutshell, there a Camera, 3 imputs (server IP, port, and message), along with a connection button. There a los a field for the server response and an Empty that well use for the user interfaz controller. Lets start hoking everything together.

    To get some interaction with the game, were going todo ned our cursor visible. Well make a Python script that Will do this. Open up the text editor window and make a new file called ongameload, py. This Will give me ha god opportunity todo introduce you todo Python scripting, so the rest of the tutorial wont sem as intimidating.
    With Python, we can add almost limitless functionality todo our games. Logic bricks are a nice bien todo add simple functionality without knowing any code, but can become very complex when trying todo add functionality that requires more then 20 brik connections. By using Python, we can avoid sorting th rouge a web of brik connections, and extend beyond the limitations that logic bricks hold us todo. However, without logic bricks, we would have no bien todo call our Python scripts.

    Let get bak todo the ongameload, py script. We want todo make a script that Will enable the cursor todo be viewed in the game. The Python module that handles this is rasterzer. To Access the rasterizer functions, we Will first ned todo import the module into our script. After a module is imported, you Will have Access todo all of it functions. You can import the script using import [module]. To save typing, well use the as [name] option. This helps tremendously when creating very long scripts that require you todo type the module name a lot.
    Import rasterizer as r.
    To show the mouse on screen, well use the showmouse () function like this.
    R, showmouse (1).
    Notice, todo call the showmouse () function, we neded todo reference the module first. To call any functions contained within a module, you reference them in module, function(), subfunction() order.

    Another thing todo take note of is the function attribute. In this case, y am using 1 todo reference a true value. false is represented as 0. In fact, any non-zero value is a true value. We could have a los used r, showmouse (true) and it Will work just the same. This is a matter of personal preference, but i think typing 1 and 0 is much faster and easier than true and false.

    Now that our script is made, we ned todo create some logic bricks that Will call them. Well assing them todo our empty, called gamecontroller. Select the Empty and pull up the logic bricks window [f4]. Add an always sensor and disable true level pulsing. Then add a Python controller. This is where we enter the name of the script todo be loaded. Connect the dots, and test out the game. You should now be able todo se your cursor in the game view.

    That should give you a god Understanding of how Python scripts are handled in the Game Engine. Now let get out of begginer mode and have a look at some real coding. From here on out, im not going todo explain everything as clik this, point here, etc. If youre seriously looking todo add networking into your games, you should have a god Understanding of the basic by now.
    part 2 : setting up the Python server
    Since we got our begginer course out of the bien (quite possibly a primer for some), let get our basic TCP Python server running. I like todo use the Blender text editor todo edit my scripts, mostly because i hate switching bak and Forth between applications. [Control + w] Will save the.blend file, and [Alt+s] Will save the text file outside of the.blend.

    This next script Will ned todo be saved outside of the.blend file, because it Will be our server application. I have tried todo find ways todo run the server easily within the game itself, but i cannot find any easy bien todo do this. Running it outside of Blender Will a los make it much faster in responding and handling client connections.

    So lets have a look at the Heart of the networking relationship, the server. As i mentioned before, were going todo use Python sockets todo make the connection between the client and the server. Im going todo show you the TCP Python server well be using, and within the code ill break it down into understandable pieces using comentarios.
    # let import the socket module import socket.
    # assing a port number todo a variable. Assigning values such as this makes.
    # it much easier todo change, instead of having todo edit every reference of the.
    # port in the script. Note, use a port value above 1024. Popular services, such.
    # as ftp, http, use the lower port numbers and this Will help todo avoid conflict, port = 2020
    # here is where we Will create our socket. We assing the socket todo s by calling.
    # the socket module and socket() function. The socket() attributes tell the socket.
    # todo create a stream socket type from the Inet socket family, s = socket, socket(socket. Af_inet, socket. Sock_stream)
    # next, we bind the port todo the hostname. This Will tell the server that we want.
    # todo use the specified port todo listen on, bind((socket, gethostname (), port))
    # now lets tell the server todo start listening for client connections.
    # for testing purposes, well setup the server todo handle 1 simultaneous.
    # connection. You can change this later if you ned, listen(1)
    # since this script Will be run outside of the game, most likely in a terminal.
    # window, we can use print() todo show that the server is running. Ive included.
    # the port value in the command todo display the port number, print game server is running on port, port.
    # this is our main loop todo handle incoming client connections.
    # connections are accepted and the message is estored into a variable.
    # the connection información is print() ed todo the terminal window and the message.
    # is then sent bak todo the client. Then the connection is closed, while 1: con, addr =, accept()
    Data = con, recv(1024)
    # display client connection información.

    Print connected, addr, data.

    Conn, send(data)
    Conn, close ().
    Save this file as server, py. If you created the file inside of Blender, use Alt+s todo save it outside of the.blend. Then open up a terminal window, navigate todo the directory where you saved the server script, and type Python server, py todo run the server. You should se something like this.
    $ Python server, py.

    Game server is running on port 2020.
    To properly kill the process, use the top command and search for the Python listing under command. Remember the pid next todo it and press the que key todo exit top. Then use the command kill [pid] replacing [pid] with the pid of the Python command from top. After it executed, you should notice a terminated command at the end of your server.
    part 3 : creating the client
    Now that we have a running server, it not going todo do much if we dont create our clients. Using the.blend file from the previous articles (here), let set a few things up before creating the client code.

    First of, if you look at the menú, youll notice there are 3 imputs: server IP (input1), port (input2) and message (input3) are used todo create the connection with the server. Currently, there no easy bien todo enter the data for múltiple text objects, so well have todo script it.

    Before we do, select each input object and create two properties. The first one being a string named text and the second one as a bol named edit. Set input1 edit property todo true, and the others todo false. This Will give us somewhere todo start.

    Im just going todo summarize the script, instead of walking you th rouge it step-by-step. We create a trigger that checks todo se if a sensor (a keyboard sensor) is positive. If it is, then we create a Python list of the 3 input objects. Then, we run an if/elif loop todo set the edit properties of the appropriate imputs according todo which conditional statement is true. Simple enough.



    Here the script. Save this as tabcontroller, py in the Blender text editor.
    Import Gamelogic as gl.

    Cont = gl, getcurrentcontroller()
    Trigger = cont, getsensor(tabcheck)
    If trigger, ispositive ():
    # get objects.

    Input1 = gl, getcurrentscene (), getobjectlist()[obinput1"] # server IP
    Input2 = gl, getcurrentscene (), getobjectlist()[obinput2"] # port.

    Number.

    Input3 = gl, getcurrentscene (), getobjectlist()[obinput3"] # message.

    Proplist = [input1, input2, input3]
    # server => port.

    If (proplist[0].edit == 1):
    Proplist[0].edit = 0
    Proplist[1].edit = 1
    Proplist[2].edit = 0
    # port => message.

    Elif (proplist[1].edit == 1):
    Proplist[0].edit = 0
    Proplist[1].edit = 0
    Proplist[2].edit = 1
    # message => server.

    Elif (proplist[2].edit == 1):
    Proplist[0].edit = 1
    Proplist[1].edit = 0
    Proplist[2].edit = 0.
    Select the gamecontroller Empty and setup a keyboard sensor. For this exercise, y am going todo be using the return key, Simply because using the tab key actually produces @ key returns which mess up the text input. You could use the tab key if you want, but youll have todo edit the script todo remove the @ in the text object. Connect this keyboard sensor todo a Python controller, and use the tabcontroller, py file.



    Here where a few problems lie within the logic brik design. What we ned todo do is create two mouse sensors (mouse over and left button), and when they are both positive a Python script is started. At first glance you might think, well, hok them both up to a Python script controller. This isnt going todo work because each mouse sensor Will ACT independently as if it were an or condition, and the script Will run every time a left mouse button is pressed and every time you mouse over the button. We ned todo connect them todo an and controller, but now were presented with the problem of not being able todo run the script directly. Since there is no Python actuator, were going todo use the property actuator todo set a bol property, which Will trigger a property sensor that Will initialize the script. Take a look at the image below todo se how this works: onto the script. Much like the server script, im going todo show you the script well be using, and then use comentarios within the script todo explain what going on.


    .
    # since well be using Blender Game Engine functions, # we ned todo import the Gamelogic module, import socket.

    Import Gamelogic as gl.
    # this is another problem with the logic bricks. We can initialize scripts using the.
    # Python controller, but we cant estop them when their sensor is not true. If we dont.
    # chek the property, the script Will actually run twice (on click and on release).
    # well create a loop todo test if the property is true, this bien we only run it once, conprop = gl, getcurrentcontroller(), getowner()
    If (conprop, connect == true):
    # now let get the text property values from the input objects, and assing.
    # them todo respective variables. Notice that we ned todo use the int() function.
    # for the port settings. If we dont, the variable is assigned as a string, and.
    # you Will get an error when Binding the port because it not an integer value, hostinput = gl, getcurrentscene (), getobjectlist()[obinput1"]
    Host = hostinput. Text.

    Portinput = gl, getcurrentscene (), getobjectlist()[obinput2"]
    Portstring = portinput. Text.

    Port = int(portstring)
    # as with the server, we create the socket, s = socket, socket(socket. Af_inet, socket. Sock_stream)
    # instead of listening for connections, well connect todo the server, connect((host, port))
    # well Grab the message from input3 and use the the send() function.
    # todo send it todo the server, message = gl, getcurrentscene (), getobjectlist()[obinput3"]
    Sendme = message. Text.

    S, send(sendme)
    # assing the server response todo a variable, # so we can use it later, and close the connection, data =, recv(1024)
    S, close ()
    # now we can display the echoed message in the server response.
    # text object by assigning it todo it text property, resp = gl, getcurrentscene (), getobjectlist()[obresponsevalue"]
    Resp. Text = data.
    # and finally, we reset the connect property bak todo false, conprop, connect = 0 summary.
    Let test it all out. Fire up the server, then play the game. Type in the IP of the server, the port number the server is running on, and a message youd like todo send. Press connect, and if all went well, the server should respond with your echoed message which is displayed in the response área. You can se the connections printed in the server terminal window. Hopefully, this Will have given you a god Understanding how simple socket connections are made and how you can get networking within your games.

    You can download the entire.blend file, which includes the server and the game client, here.

    In my next article, well cover how todo design a simple Python chat server, complete with password authentication, and setting up the Blender UI todo create a chat client in the Game Engine.

    Until then, if you have any questions, comentarios, or problems, fel free todo e-mail me at bchynds@Mac.com, or Jump into the discussion at blenderartists in this forum hilo:
    http://blenderartists.org/forum/showthread.php?t=111532.

    By Brian cordell hynds. www.blenderart.org.


    -- IMÁGENES ADJUNTAS --




    Miniaturas adjuntas Miniaturas adjuntas 1.jpg   2.jpg   3.jpg   4.jpg  

    5.jpg  
    |Agradecer cuando alguien te ayuda es de ser agradecido|

Temas similares

  1. Ofertas de Trabajo Opera game Studio busca programador c# networking unity3D
    Por Stratos en el foro Oferta y demanda profesional
    Respuestas: 0
    : 16-01-2016, 08:37
  2. Ofertas de Trabajo Opera game Studio busca programador c# Networking Unity3d
    Por Stratos en el foro Oferta y demanda profesional
    Respuestas: 0
    : 12-01-2016, 08:37
  3. Ofertas de Trabajo Opera game Studio busca programador c# Networking Unity3d
    Por Stratos en el foro Oferta y demanda profesional
    Respuestas: 0
    : 17-12-2015, 08:37
  4. Game engine Blender
    Por MAYAMAN en el foro Videojuegos
    Respuestas: 2
    : 14-03-2010, 18:32
  5. Blender Blender 2 31a más raytrace más game engine Blender power ::
    Por SHAZAM en el foro Programas de Diseño 3D y CAD
    Respuestas: 18
    : 14-01-2004, 11:34