check my scripts

Hi,
I’ve been working on a game for a while now. There are a lot of things that are working perfectley, but even more things don’t work XD… yet.
I just want to ask some things about problems I run into.

Ok first off all, I have this script:



import GameLogic
players = GameLogic.globalDict.get("players")
activePlayer = GameLogic.globalDict.get("activePlayer")
ow = GameLogic.getCurrentController()

pID = ow['pID'] # get the property

if activePlayer == pID : 
    if players[pID]["energy"] <= 0 :      # if player is dead
        activePlayer = activePlayer + 1   # next player in turn
    else :                                # player is still alive
        
        # turn starts -> set state
        actuatorTurn1 = ow.actuators['setStateTurn1']
        activate (actuatorTurn1)
        
else :
    actuatorEmpty = ow.actuators['setEmptyState']
    activate (actuatorEmpty)


Now in this case the pID is equal to the property pID. Also the energy is 100, so it should change the state?
But it doesn’t do that. Any idea how that comes? I know it might be confusing because you fall in the middle of a project, so if you need additional info, just let me know.
I will probably also upload a blend file any time soon.

Thanks
pcforme

you need the controller to activate an actuator:

ow.activate(“actuator_you_wish”)

ok, so I now got this:

import GameLogic
players = GameLogic.globalDict.get("players")
activePlayer = GameLogic.globalDict.get("activePlayer")
ow = GameLogic.getCurrentController()

pID = ow['pID']

if activePlayer == pID : 
    if players[pID]["energy"] <= 0 :      # if player is dead
        activePlayer = activePlayer + 1   # next player in turn
    else :                                # player is still alive
        
        # turn starts -> set state
        actuatorTurn1 = ow.actuators['setStateTurn1']
        ow.activate (actuatorTurn1)
        
else :
    actuatorEmpty = ow.actuators['setEmptyState']
    ow.activate (actuatorEmpty)

but still the same problem

better use:


cont = GameLogic.getCurrentController()
ow = cont.owner

a bit revised:


import GameLogic
players = GameLogic.globalDict.get("players")
activePlayerID = GameLogic.globalDict.get("activePlayer")
 
cont = GameLogic.getCurrentController()
own = cont.owner
 
currentID = own['pID'] # get the property
if activePlayerID == currentID : 
    if players[activePlayerID ]["energy"] <= 0 :      # if player is dead
        activePlayerID = activePlayerID + 1   # next player in turn
    else :                                # player is still alive
        # turn starts -> set state
        cont.activate ('setStateTurn1')
 
else :
    cont.activate ('setEmptyState')

I hope it helps

edit: first check the console for errors. They are important :wink:

ok, it helped indeed. Thanks for that, but it’s not working yet.
Now it changes to the ‘emptyState’. Which is strange because the ‘activePlayerID’ is equal to the ‘currentID’

You might want to add some print statements to verify that your assumption is true (or not :wink: ).

ok, I thought of that to, but where can I see the result of these prints?
Because I tried to find it, but they don’t appear in the python console.

ok, next to the problem mentionned above I’ve run into some other problems.
For example I’m working on a healthbar for the characters. The script is:


import GameLogic

players = GameLogic.globalDict.get("players")
cont = GameLogic.getCurrentController()
own = cont.owner

pID = own['pID'] #finding out which character's energy I have to know

own['health'] = players[pID]['energy'] #setting another property to the energy


I have now an F-curve actuator set to property. This works fine, only the value of the “health” property always stays 100 (= no damage) while I have set it to 20 as a test.
So I suppose it’s something in the script?

Print output appears in the console window (not console view). I think you must enable it manually in 2.5x.

Is your curve actuator enabled?
It must be enabled at least once (e.g. with an always actuator)

The script is enabled with an always script that triggers positive pulses.
And I can’t find the console window or any option to enable it.
I’m working with 2.58 if that is important?

Yes, Blender 2.5 hides the console window by default, so you need to enable it under the Help menu (if you’re on Windows). On Mac, it’s a different process.

and ubuntu? Because the option isn’t in the help menu.
Or do I need to run blender from the terminal?

ok, so apperently you have to run blender from the terminal to get the results.
Ok, now I finnaly can see what the problem is. It gives for activePlayer a value = none.
Which is extremely strange.
I got this script:

import GameLogic

players=[
  { 'name':'paul',  'energy':20, },
  { 'name':'keith', 'energy':100, },
  { 'name':'ringo', 'energy':100, },
  { 'name':'john',  'energy':100, },
  { 'name':'mick',  'energy':100, },
  { 'name':'brian', 'energy':100, }
]

activePlayer = 1

GameLogic.globalDict['players'] = players
GameLogic.globalDict['activePlayer'] = activePlayer

So why is the activePlayer not equal to 1?

You use get() to get the dictionary item.

None means there is no item for this key ‘activePlayer’. I think you simply didn’t run the above code (at least not before you tried to access the dict)

well I thought that to, but this script is connected to an always sensor, as is the other script with the get().
So how do I make sure that the script with these starting data is the first thing that should be run in the game?

You always sensor is in True Pulse mode?

The simplest thing is:


players = GameLogic.globalDict.get("players")
if player is None:
  return

So it does nothing when the players are not set (yet). With the next call it can deal with the hopefully set players.

If there is no Pulse set, change the always sensor to a delay sensor with a delay of 1. This means it runs in frame 1 rather than in frame 0. As the players are set in frame 0, they should be present than.

Awesome !! The delay sensor completley did it.
Thanks for the help.
Now I think I’ve setup everything for the characters. The only thing is that I’ve set it up only for one character.
I was wandering, what would be the most efficient way to duplicate all the scripts, logic,… for the other characters?
Because if I just duplicate it I still have to change some things for every character seperate. Is there a way to do this automaticly or something?

<ctrl+c> at least in 2.49

or spacebar menu 2.5