about using socket class (to make online game)

Game Engine, Players & Web Plug-in, Virtual Reality, support for other engines

Moderators: jesterKing, stiv

Post Reply
Posts: 0
Joined: Fri Jan 03, 2003 4:51 pm

about using socket class (to make online game)

Post by shizu »

Hi, :D

I want to make a online multiplayer game. so I have to use socket class of python, although we can call script at controller of LogicBrick only in gameBlender(not only?). To use sockets, it need 'onetime' initilizing of itself at first.
I will write init code at top of script. but because of the call executing the script from first line to end line each time, sockets (and instances, flags etc.) will be re-initilized each time at calling.
When a socket established connection, it don't need to init more.
I don't know how to avoid this.

Please someone help me with good idea...

Posts: 233
Joined: Sun Oct 13, 2002 7:37 pm
Location: University of Minnesota (USA)

Post by SirDude »

You could set a property and then test the property to see if its

Posts: 0
Joined: Fri Jan 03, 2003 4:51 pm

Post by shizu »

I am sorry that my reply is overdue.

Thanks to you, I was able to do onetime initialization.
but there is another problem. When a script is completed, the object made within the script is destroyed. I want to continue connection of a socket, without destroying an object.
Is there any way of leaving this initialized object?
(In order to leave, it is refused even if it assigns the made object as an attribute to the game object.)

thanks again.

Posts: 12
Joined: Wed Dec 04, 2002 8:14 am

Post by wiseman303 »

I'm not sure how much of this you might already know, so I appologize if I mention something that seems obvious.

Game objects (the ones you get with controller.getOwner()) are limited in the types of properties they can have. (Ints, floats, and strings, I think.)

I ran into this problem when I was trying to assign a dictionary to a game object, I ended up converting it to a string, and reading it with the eval() function.

For what you're doing, I'd suggest you assign your socket object to the GameLogic module, (imported automaticly in blender 2.23+,) or another module that you import. Not only will your object persist into future frames, but it will also be accessable by all other scripts that import that module, (if you assign it to the GameLogic module, all scripts will automaticly be able to access it, since GameLogic is automaticly imported.)

Posts: 0
Joined: Fri Jan 03, 2003 4:51 pm

Post by shizu »

thx lot!
and I am sorry that my text is long.
wiseman303 wrote:I'm not sure how much of this you might already know, so I appologize if I mention something that seems obvious.
I know few :oops: .
so your help is very usefull for me.

Well, the convert function is "repr()", isn't it?
This way is tricky, but cool!!

However, it seems that it could not be used for a socket.
I tried it at first, but I got a error.(below)

Code: Select all

>>>sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> eval(repr(sock))==sock
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 1
    <socket._socketobject instance at 0x0091B300>
SyntaxError: invalid syntax
If socket object was converted to string certainly, a expression "eval(repr(sock))==sock" would return "True".
So I searched infomations about both repr() and socket.

and I refered these I got.
1. http://www.python.org/doc/current/ref/c ... ation.html
2. http://mail.python.org/pipermail/python ... 15362.html

the 1. is python reference document. In this,
Called by the repr() built-in function and by string conversions (reverse quotes) to compute the ``official'' string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object.
I think socket class is not convertible.
So it could not be assigned to GameObject etc.

I don't know how to slove without changing source codes of blender.
but I will do my effort...

Posts: 110
Joined: Wed Oct 16, 2002 6:52 am

Hello, welcome to the fun:)

Post by Saluk »

You can store any kind of object on GameLogic, so no str(), repr(), eval() whatever is needed. Im not sure but I don't think repr works for anything but the basic datatypes. If you wanted to, you could use pickle or some form of serialization to convert the socket into a string. But that wont be the best method in this, The best method is to simply put the socket on GameLogic:

init script:
GameLogic.socket = socket

Then you can access the object from anywhere else with GameLogic.socket.

I worked on multiplayer for about a month with my dad, you can see what I have done so far here:http://spinhead.homeip.net/?id=multipla ... e=projects

Right now it all mostly works, on computers with python installed. But the server only runs on windows, and my main computer is linux. The server won't run through a firewall, so I can't even run my own server anymore! To test my game out, you'll have to run your own server and tell my game to connect to that. My email is saluk@flashmail.com if you have any more questions, feel free to rip out pieces of my code and use them, or just study it to help you learn:)

Good luck.

Posts: 0
Joined: Tue Jan 14, 2003 4:24 pm

Post by burper »

Maybe my notes-to-self will help someone on this same path:
Q: "ImportError: No module named socket", What am I doing wrong?

A: Nevermind, I found the answer by setting PYTHONPATH before starting blender, i.e. the Python setting in the info window doesn't work as I expected.


Q: Now my problem is this:
"ImportError: Module use of python22.dll conflicts with this version of Python."

A: OK, am using Python 2.0.1 for now to avoid the problem.

Does anyone know if there is a better combination?

Another update: I met my first milestone by creating a simple model that can be manipulated in various ways depending on what is read on a socket connection to a server. I have a simple java server (it's what I know best) that, when a client connection is made, outputs "a\n", "b\n", "c\n", "d\n", "e\n", with 1 second sleeps in between, then closes the connection. I have a simple model (an incomplete castle tutorial) with a bouncing (actor,dynamic) 'main' mesh object. An "always" sensor is connected to a python script controller and then to a replaceMesh actuator. The python script inits the client socket connection if it hasn't been already and stores it in GameLogic (thanks to previous posts in this thread). It then reads from the socket if there is anything to read, and depending on what it has read, replacesMesh with a different other mesh stored on layer 2.

I'll post some code here if anyone cares.

It seems weird to me that the actuator has to be connected in the gui to the controller for the controller to be able to find it. It then seems weird to me that the controller doesn't simply return a "firing" boolean similar to the AND,OR controllers, or that it doesn't actuator.invioke(), or something. But I am very new to both blender and python.

For my purposes, I will try to develop a simple protocol for doing this sort of stuff, with the ability to create, delete and modify named objects. If anyone cares, chime in on what you think it should look like. Maybe it's too early to try this, as blender/python are still changing? Maybe a tutorial on controller a blender game via a socket connection.

Background info:
I am new to both blender and python, but am an experienced graphics and networking programmer.
I am trying to get some kind of socket stuff working for multiplayer, as it seems others are doing. Actually, am trying to design a 3d client for an existing server, so the protocol is fixed, but will be filtering it through a client-local proxy server.
I am currently using blender 2.25 with python2.0.1 on both Solaris8 and windows xp.

Posts: 17
Joined: Tue Oct 22, 2002 10:35 pm

Post by Blender_owl »

i found some good resources on flipcode.com if you read them you might get some ideas

Post Reply