Python help.

Alright, i am trying to get the following code to read from a file called “Account.acc” but first i need to create such a file via python and have the user input a name witch is then written inside the “Account.acc” file. I then want this code, to, as i said read from the file and extract the name witch was written via python from what the user had input on a “Enter a nickname” screen for example, and replace ‘Guest’ with that extracted name.

The Code:

 
GameLogic.pseudo = 'Guest'
GameLogic.host = '192.168.2.2'
#GameLogic.host = 'CR-O'
 
GameLogic.protocol_version = "1.0"
GameLogic.port = 50000
 
import bzClient
from bzClient import bzClient
 
print "bzInit"
if hasattr(GameLogic,'bzInitialised') == False:
#Game Variable init
GameLogic.bzInitialised = True
GameLogic.bzClient = bzClient (GameLogic.host, GameLogic.port, GameLogic.pseudo)
GameLogic.bzClient.Connect()
 
 
#Switch to Main scene
actu = GameLogic.getCurrentController().getActuator('Main')
GameLogic.addActiveActuator(actu, True)
 
else:
print 'Not Connected .. Please Exit and Restart',

I’m hoping someone can assist me with this, i have tried numerous different things, but nothing seems to work.

Thanks,
Brave.

So you’re trying to read and write text files via python?

Writing


file = open("/accounts.acc", 'w')
file.write("Helloes!")
file.close()

Reading


file = open("/accounts.acc", 'r')
text = file.read()
file.close()

Hope that helps,
-Chaser.

Yes, but i don’t want to have what is written in the file, in the python script, i have a user screen where the user inputs a name, i want that name to be written to the file. Then, have the above script read it and replace ‘Guest’ with the name written.

Brave. :wink:

Just set the property value of the username to be the text.

if own.username is the name property, then

file.write(own.username)

Here you go, Brave:

http://www.blendenzo.com/Files/Help/inputUserName.blend

Sometimes a .blend is worth a thousand words…
You will not need to use an external file to do what you want to. Look at the .blend I’ve given you, and you should be able to see how it’s done. The user enters their name in the input box, then when the “Log On” button is clicked, the script “logOn.py” is run. The userName property is read from the input field and can then be used in the script. “logOn.py” is just a fake file I created for this example. You should replace it with your connection script. Where you have “GameLogic.psuedo = ‘Guest’”, replace it with “GameLogic.user = GameLogic.getCurrentScene().getObjectList()[“OBUserName”].userName” or your equivalent object and property.

Hope that helps you out. Let me know if you anymore explanation.

Alright, nice blendenzo! I incorporated your save file stuff, and i now have your file set up to save the name i type in that box to a file! Just what i wanted. :rolleyes: I don’t fully understand how you done it though :o . Now, on to the next problem i need to have my connection code open that same file and take out the name typed into it and replace Guest here:

 
<b>GameLogic.pseudo = <i>'Guest'</i></b>
GameLogic.host = '192.168.2.3'
#GameLogic.host = 'LODweb.dyndns.org'
 
GameLogic.protocol_version = "1.0"
GameLogic.port = 50000
import bzClient
from bzClient import bzClient
print "Connected"
if hasattr(GameLogic,'bzInitialised') == False:
    #Game Variable init
    GameLogic.bzInitialised = True
    GameLogic.bzClient = bzClient (GameLogic.host, GameLogic.port, GameLogic.pseudo)
    GameLogic.bzClient.Connect()
 
    #Switch to Main scene
    actu = GameLogic.getCurrentController().getActuator('Main')
    GameLogic.addActiveActuator(actu, True)
else:
 print 'Not Connected .. Please Exit and Restart',

I gather i need to use the load file method, but how would i get that in the above?

Thanks alot! :smiley:
Brave.

I still need help with the above! Anyone? :frowning:

I also have a new problem i need help fixing, my game runs fine in blender but when i create a runtime i get these errors in the console, however the game doesn’t crash:

http://img341.imageshack.us/img341/7388/helphm9.jpg

How would i go about fixing this? Do i need my python scripts to be in the same folder as the .exe or somthing?

Please help!

Thanks,
Brave.:wink:

yes you need to have the iether the python24.dll or the other modules that you used… inside the python24.dll are all the standard modules that come with python like the math module and the operator module etc… if you look in the blender root directory there is a python24.dll… try using copying that to where your exe is?

Alright thanks, i extracted all my scripts and they are now inside the game directory. It then asked me for a module called “Blender”, i just faked it by making a new python file and calling it “Blender” with “Import Blender” inside it, and it did it!

Thanks,
Brave.
:wink:

now way! that’s weird cool it worked tho!