2
$\begingroup$

I am trying to create my own sensor block in Blender's Game Logic to detect a text file being created in my C drive by another program.

Is it possible to perhaps hack an existing sensor or to create a new one in which you can define where the text file is located?

I tried using the suggested answer method stated below, but the python code would only run once upon finding the variable in the test.txt file. Any ideas on how to keep the always loop running? Here is some sample code:

import bge
from bge import logic as GL #GL=GameLogic
import os 
import re

# Get the whole bge scene
scene = bge.logic.getCurrentScene()
cont = GL.getCurrentController() #access the logic brick that the script is being called from and assign it to a variable
#logic brick is returned by getCurrentController() -this method is passed to the variable cont
own = cont.owner #accessing the Armature object that is the owner of the controller

#Sensors - now have access to the controller and to the object that owns it. The next thing to do is to retrieve the necessary sensors
AlwaysSensor=cont.sensors['Always1'] # sensors assigned meaningfully name variable
#Actuators -  retrieval of the actuators
box_action = cont.actuators["Action1"]
#Now all the necessary logic brick components are accessible within the script, and the code for the movement itself can begin. 

#setting up Action - animations
box_action.action = 'upMovement'
box_action.frameStart = 1
box_action.frameEnd = 24
box_action.mode = 2 #play modes are represented by integers: 0 represents the Play play mode, 1 represents the Property play mode, 2 represents the Flipper play mode, 3 represents the Loop Stop play mode, and 4 represents the Loop End play mode.


print("writing read charcters")
#note text file is in blender file in C drive
text_file = open("test.txt", "r")#varialbe=parameterOpen(parameterFileName, r read)
#print (text_file.read(1))#prints the first character '1' in txt file
var=(text_file.read(1))

print ("var=", var)
if (var == "a"):
#if (text_file.read(1)) == 'h':
    if AlwaysSensor.positive:
        cont.activate(box_action)#run upMovement animation
        print("a found+animation run")     

text_file.close() #closes the file once read

Here is a screenshot of the game logic blocks setup: enter image description here

$\endgroup$
1
$\begingroup$

I would connect an always sensor into a python controller to run a python script that tests for the file and handles it accordingly.

enter image description here

$\endgroup$
  • $\begingroup$ Thanks sambler, I did this but the python code only seems to run once, then stops working. Is there a way to end the code so that the always sensor starts working again? $\endgroup$ – CubeBot88 Feb 11 '14 at 7:57
  • $\begingroup$ At the left of the always sensor are two buttons and a freq. You need to enable one of the buttons for it to repeat, the freq defines how often it is repeated (in logic ticks). $\endgroup$ – sambler Feb 11 '14 at 8:39
  • $\begingroup$ Sorry I keep adding stuff to the original question, but yes I did play with the freq options. When I start, the python code does run and the animation moves. The odd thing is the animation only activates once, however the System Console shows that the python scripts are running again, but the animations are not... $\endgroup$ – CubeBot88 Feb 11 '14 at 8:56
  • $\begingroup$ In the action brick turn on Add. Also your only reading one char but not deleting or clearing the file. $\endgroup$ – sambler Feb 11 '14 at 9:41
  • $\begingroup$ I have played with that function a bit too, not luck so far. Maybe the screenshot I added to the Q will help? $\endgroup$ – CubeBot88 Feb 11 '14 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.