Sensor Logic Brick: invalid

invalid
  • Returns whether or not a reference to an object (KX_GameObject, SCA_ILogicBrick, KX_Scene, etc.) is invalid or not. 

Type: 
  • boolean

Values:

  • 1 = True = object isn't valid
     
  • 0 = False = object is valid
Note:
  • Pointers to game data can be stored as a global variable.
     
  • If the game object is freed/deleted/destroyed, the global variable holds a pointer to game data that no longer exists.
     
  • Trying to access this data will raise a system error.
     
  • invalid allows you to test for this case without raising the system error.
Sample Code: Part 1


################  save sensor as a global variable
 
# import bge module

import bge

# get the python controller
cont = bge.logic.getCurrentController()

# get the sensor named Sensor_1
sen = cont.sensors["Sensor_1"]

# save sensor reference as a global variable
bge.logic.globalDict["Sensor_1"] = sen
 
Sample Code: Part 2


################  Check if the saved reference is invalid

# import bge module
import bge

# check if saved sensor reference is invalid
if bge.logic.globalDict["Sensor_1"].invalid == False:

# invalid == False.  Sensor_1 still exists
sen = bge.logic.globalDict["Sensor_1"]