Setting properties with Python

I´m coding the script for the Game Engine and ran into this problem: when I set a few Text properties, only the last one gets set:

import GameLogic
cont = GameLogic.getCurrentController()

Line1 = cont.getOwner("Line1")
Line2 = cont.getOwner("Line2")
Line3 = cont.getOwner("Line3")
Line4 = cont.getOwner("Line4")

Line1.Text = "Text line 1"
Line2.Text = "Text line 2"
Line3.Text = "Text line 3"
Line4.Text = "Text line 4"

The result is that all the Text properties get set to “Text line 4”. Is there any workaround for this?

Something like this should work:

g = GameLogic
c = g.getCurrentController()
o = c.getOwner()

Line1 = g.getCurrentScene().getObjectList("OBLine1")
Line2 = g.getCurrentScene().getObjectList("OBLine2")
Line3 = g.getCurrentScene().getObjectList("OBLine3")
Line4 = g.getCurrentScene().getObjectList("OBLine4")

Line1.Text = "Line 1 Text"
Line2.Text = "Line 2 Text"
Line3.Text = "Line 3 Text"
Line4.Text = "Line 4 Text"

An added advantage is you don’t need to connect the objects with logic bricks.

Wow, excelent! Thanks a lot, Andy.
Great not having to link all the objects, too!

On the first implementantion, I had the Line objects with a Near sensor linked to the Python script. How should I set this up now? I think I´ll still link the bricks, because of the Near Sensor… Or maybe I´ll try to put this into Python later on.

I think it´s not working here yet…
It´s giving a Attribute error: getCurrentScene. I´m using Blender 2.36 RC2.

Don’t forget the brackets () on getCurrentScene. It’s something I’ve forgetten a few times.

Edit: I may also have the syntax of the getObjectList() wrong. It’s something I can never remember correctly :-? I’m at work at the moment so I can’t check. Try using getObjectList([“OBLine1”])

I’m not sure exactly what you intend to happen, but perhaps this will help. The near sensor would need to be linked.

g = GameLogic 
c = g.getCurrentController() 
o = c.getOwner()

Near = c.getSensor("Near")

if Near.isPositive():

     Line1 = g.getCurrentScene().getObjectList("OBLine1") 
     Line2 = g.getCurrentScene().getObjectList("OBLine2") 
     Line3 = g.getCurrentScene().getObjectList("OBLine3") 
     Line4 = g.getCurrentScene().getObjectList("OBLine4") 

     Line1.Text = "Line 1 Text" 
     Line2.Text = "Line 2 Text" 
     Line3.Text = "Line 3 Text" 
     Line4.Text = "Line 4 Text"

I´m (we´re) going bits by bits but I will still add some functionality. This is, for the script to sence which Near sensor triggered tit, so it could output the right strings.

I got it! It was a “minor” syntax detail:
Line1 = g.getCurrentScene().getObjectList()[“OBLine1”]

import GameLogic
g = GameLogic
c = g.getCurrentController()
o = c.getOwner()

Near = c.getSensor("NearSen")

if Near.isPositive():
	Line1 = g.getCurrentScene().getObjectList()["OBLine1"]
	Line2 = g.getCurrentScene().getObjectList()["OBLine2"]
	Line3 = g.getCurrentScene().getObjectList()["OBLine3"]
	Line4 = g.getCurrentScene().getObjectList()["OBLine4"]

	Line1.Text = "Line 1 Text"
	Line2.Text = "Line 2 Text"
	Line3.Text = "Line 3 Text"
	Line4.Text = "Line 4 Text"