Random Startup Position

Hello All…

I have an actor (a simple cube) that I want to place randomly at the game’s startup.

I’ve been playing w/ a simple python script using the getRandomFloat() and setPosition() functions.

I cannot get my character to move out of its default position at startup.

Can someone help?

Using Blender 2.25

Thanks!

hm… well this scripts should work I just modified it for the gameengine…Just link this to a sensor.


import GameLogic

strn = [0.00, 0.00, 0.00]
ob = GameLogic.getCurrentController().getOwner() 

def randObjMove():
	global ob
	x = ob.getPosition()[0]
	y = ob.getPosition()[1]
	z = ob.getPosition()[2]
	# This is just some crap to add the random addition to its current position
	strRand = str(GameLogic.getRandomFloat())
	strn[0] = str(x)
	strn[1] = str(y)
	strn[2] = str(z)
	xstr = int(strn[0].find("."))
	ystr = int(strn[1].find("."))
	zstr = int(strn[2].find("."))
	
	addx = strn[0][:xstr] + strRand[1:]
	addy = strn[1][:ystr] + strRand[1:]
	addz = strn[2][:zstr] + strRand[1:]
	loc = (float(addx), float(addy), float(addz))
	return ob.loc

ob.setPosition(randObjMove())

Thanks for the tip…
I copied/pasted your code and linked up the sensor and Python Controller.

When the script executes, it generates the following error:

PYTHON SCRIPT ERROR:
Traceback (most recent call last):
File “random”, line 27, in ?
File “random”, line 25, in randObjMove
AttributeError: loc

Any clues?

I dunno but it could be:

Blender version
Capital L
wrong command :wink:

I had actually tried that, chaning the varialble from loc to Loc, but it didn’t make a difference…

Dang!!

ACK!!! I am sooooo soryy lol I not alct returned the wrong this but also used the worng brakects!



import GameLogic

strn = [0.00, 0.00, 0.00]
ob = GameLogic.getCurrentController().getOwner()

def randObjMove():
   global ob
   x = ob.getPosition()[0]
   y = ob.getPosition()[1]
   z = ob.getPosition()[2]
   # This is just some crap to add the random addition to its current position
   strRand = str(GameLogic.getRandomFloat())
   strn[0] = str(x)
   strn[1] = str(y)
   strn[2] = str(z)
   xstr = int(strn[0].find("."))
   ystr = int(strn[1].find("."))
   zstr = int(strn[2].find("."))
   
   addx = strn[0][:xstr] + strRand[1:]
   addy = strn[1][:ystr] + strRand[1:]
   addz = strn[2][:zstr] + strRand[1:]
   loc = [float(addx), float(addy), float(addz)]
   return loc

ob.setPosition(randObjMove()) 

that defintly I should really have tested it

Thanks! I’ll give it a go!

Well, the good news is the script doesn’t error.

The bad news is, I can’t tell that the object is moved at all from its default placement.

I was hoping that it would be at pointA one time, then at pointB the next time the game started up…so that the player got a different startup experience each time he/she played.

I

Download this file. It should do, what you need.

Thanks Ashid…but:

Forbidden
You don’t have permission to access /random.blend on this server

Could you just email me the file?
[email protected]

Thanks!

Hmmm, try to use SaveAs. But I sent it by mail, anyway.

Strange, forbidden download when just click link. But I tried again with download accelerator, and it works. So someone want to use it, maybe i can upload it to my server. By the way, nice blend file :smiley:

Ashid,
I got it. It is EXACTLY what I was looking for!!

Thanks!!

Odd…the .blend from Ashid runs fine.

I copy the script into an existing project I have and it runs fine.

I copy the script into a new project and it gives the error regarding the init line. I even try to import the text from Ashid’s .blend, rnd.py, into the new project and it errors regarding the init.

Very bizzaro … :Z

DOH!!!

I am such a f@cking idiot!

The object that is linked to the script MUST have a property (in this case) called ‘init’.

/me goes and has a seat on the “Gotta do Python more often couch”

Yeah, it works in on my game character, it’s never on the same place when I start the game each time. Thanks dudes :stuck_out_tongue: