Unusual profile after replacing with python, Lag

Hi,
a few weeks ago I started my own game project, I made 90% of everything with logicbricks since I had no python knowledge.
But now things become too complicated for just only logic bricks. So I learned python and ~~ 1 week ago I started replacing all logic bricks with python modules. Now I’m almost done with that, but now I notice that sometimes when I play the game it begins to lag. Before I started replacing everything with python this was how the profile looked like:

Framerate 60 fps
Physics 1%
Logic 1%
Animations 0%
Network 0%
Scenegraph 0%
Rasterizer 90%
Services 0%
Overhead 6%
Outside 0%
GPU latency 0%

Now it looks like this and its very very unstable:

Framerate 60 fps
Physics 1%
Logic 0%
Animations 0%
Network 0%
Scenegraph 0%
Rasterizer 8%
Services 20%
Overhead 6%
Outside 60%
GPU latency 0% (now it is 0% but a lot times it is something like 60%)

As I said, it is very unstable: sometimes Outside goes above 90%, but sometimes GPU latency does.
I searched on the internet what all those things mean. If I’m not wrong: outside means that something outside the BGE is taking down the peformance, and you don’t have control over it. But I tried to play the game with and without a lot of other applications running in the background but it doesn’t make any difference.
Everytime I play the game I have 30% chance it is going to lag.
I hope there is a way to get this stable because it would be very annoying for people to have chance the game will lag everytime they startup the game.

I know this is a bit vague, but maybe you can tell me what is happening.

Thanks

Well it can be everything.

First of all check your python code, because you said you replaced all logic to python.
second, you should upload a demo blend with your problem, we cannot see the problem and the description is kinda wierd.

GPU latency 0% (now it is 0% but a lot times it is something like 60%)

i have never seen mine at 0, but it always around 60%, so this is normal.

Rasterizer 90%

this was with logic? then i can say you got a bug somewhere because 90% is way and way to much.

Yup, that was with logic. Maybe it is 90% because everything else is a lot less? 90% doesn’t have to say it is much, or am I wrong?
EDIT: however, maybe it was a bug, but the peformance was perfect
Well, I can post my whole python code… so here you have it:

import bge
from bge import logic


def archer5():
    cont = logic.getCurrentController()
    arch = cont.owner
    once = cont.sensors['Once']        
    ###ONCE
    if once.positive:
        
        ###yup, I know this piece of code could be a lot more simpler, and I know how to make it simpler, but this works, so.. why should I.. 
        
        ### SLOT 1
        if arch['aslot'] == 1:
            ### BOW 0
            if arch['slot1'] == '0I' or arch['slot1'] == '0II' or arch['slot1'] == '0III':
                cont.activate(cont.actuators['0ns'])
            ### BOW 1
            if arch['slot1'] == '1I' or arch['slot1'] == '1II' or arch['slot1'] == '1III':
                cont.activate(cont.actuators['1ns'])
            ### BOW 2
            if arch['slot1'] == '2I' or arch['slot1'] == '2II' or arch['slot1'] == '2III':
                cont.activate(cont.actuators['2ns'])
            ### BOW 3
            if arch['slot1'] == '3I' or arch['slot1'] == '3II' or arch['slot1'] == '3III':
                cont.activate(cont.actuators['3ns'])
            ### BOW 4
            if arch['slot1'] == '4I' or arch['slot1'] == '4II' or arch['slot1'] == '4III':
                cont.activate(cont.actuators['4ns'])
        ### SLOT 2
        if arch['aslot'] == 2:
            ### BOW 0
            if arch['slot2'] == '0I' or arch['slot2'] == '0II' or arch['slot2'] == '0III':
                cont.activate(cont.actuators['0ns'])
            ### BOW 1
            if arch['slot2'] == '1I' or arch['slot2'] == '1II' or arch['slot2'] == '1III':
                cont.activate(cont.actuators['1ns'])
            ### BOW 2
            if arch['slot2'] == '2I' or arch['slot2'] == '2II' or arch['slot2'] == '2III':
                cont.activate(cont.actuators['2ns'])
            ### BOW 3
            if arch['slot2'] == '3I' or arch['slot2'] == '3II' or arch['slot2'] == '3III':
                cont.activate(cont.actuators['3ns'])
            ### BOW 4
            if arch['slot2'] == '4I' or arch['slot2'] == '4II' or arch['slot2'] == '4III':
                cont.activate(cont.actuators['4ns'])
        ### SLOT 3
        if arch['aslot'] == 3:
            ### BOW 0
            if arch['slot3'] == '0I' or arch['slot3'] == '0II' or arch['slot3'] == '0III':
                cont.activate(cont.actuators['0ns'])
            ### BOW 1
            if arch['slot3'] == '1I' or arch['slot3'] == '1II' or arch['slot3'] == '1III':
                cont.activate(cont.actuators['1ns'])
            ### BOW 2
            if arch['slot3'] == '2I' or arch['slot3'] == '2II' or arch['slot3'] == '2III':
                cont.activate(cont.actuators['2ns'])
            ### BOW 3
            if arch['slot3'] == '3I' or arch['slot3'] == '3II' or arch['slot3'] == '3III':
                cont.activate(cont.actuators['3ns'])
            ### BOW 4
            if arch['slot3'] == '4I' or arch['slot3'] == '4II' or arch['slot3'] == '4III':
                cont.activate(cont.actuators['4ns'])
        




def archer4():
    cont = logic.getCurrentController()
    arch = cont.owner
    sensW = cont.sensors['W']
    scene = logic.getCurrentScene()
    objs = scene.objects
    for item in objs:
        if 'HP' in item:
            HP = item
    ###W
    if sensW.positive and HP['Alive'] == True:
        if arch['aslot'] == 1 and arch['slot2'] is not 'L':
            cont.activate(cont.actuators['click'])
            arch['aslot'] = 2
            arch['tus'] = 0
        else:
            
            if arch['aslot'] == 2 and arch['slot3'] is not 'L':
                cont.activate(cont.actuators['click'])
                arch['aslot'] = 3
                arch['tus'] = 0
            else:
                if arch['aslot'] == 2 and arch['slot3'] == 'L':
                    cont.activate(cont.actuators['click'])
                    arch['aslot'] = 1
                    arch['tus'] = 0
                else:
                    if arch['aslot'] == 3:
                        cont.activate(cont.actuators['click'])
                        arch['aslot'] = 1
                        arch['tus'] = 0




def archer2():
    #########
    # T U R #
    #########
    cont = logic.getCurrentController()
    arch = cont.owner
    s1r = cont.sensors['s1r']
    s2r = cont.sensors['s2r']
    s3r = cont.sensors['s3r']
    
    
    if s1r.positive:
        cub = arch['slot1']
        level = cub[1:]
        if level == 'I':
            tur = 50
        else:
            if level == 'II':
                tur = 35
            else:
                if level == 'III':
                    tur = 20
        
        if arch['s1tur'] < tur:
            arch['s1tur'] += 5
        else:
            if arch['s1tur'] == tur:
                arch['s1tur'] = 0
                arch['s1r']  = False
                arch['tus'] = 0
    if s2r.positive:  
        cub = arch['slot2']
        level = cub[1:]
        if level == 'I':
            tur = 50
        else:
            if level == 'II':
                tur = 35
            else:
                if level == 'III':
                    tur = 20
        
              
        if arch['s2tur'] < tur:
            arch['s2tur'] += 5
        else:
            if arch['s2tur'] == tur:
                arch['s2tur'] = 0
                arch['s2r']  = False
                arch['tus'] = 0
    if s3r.positive:  
        cub = arch['slot3']
        level = cub[1:]
        if level == 'I':
            tur = 50
        else:
            if level == 'II':
                tur = 35
            else:
                if level == 'III':
                    tur = 20
        
                  
        if arch['s3tur'] < tur:
            arch['s3tur'] += 5
        else:
            if arch['s3tur'] == tur:
                arch['s3tur'] = 0
                arch['s3r']  = False
                arch['tus'] = 0
def archer1():
    cont = logic.getCurrentController()
    sensA = cont.sensors['A'] 
    sensD = cont.sensors['D']
    sensSB = cont.sensors['Spacebar']
    sensLB = cont.sensors['lb']
    sensGR = cont.sensors['ground']
    sensAR = cont.sensors['air']
    
    
    
    arch = cont.owner
    scene = logic.getCurrentScene()
    objs = scene.objects
    
    slot = 'slot' + str(arch['aslot'])
    
    cub = arch[slot]
    level = cub[1:]
    if level == 'I':
        cubtus = 40
    else:
        if level == 'II':
            cubtus = 20
        else:
            if level == 'III':
                cubtus = 10
        
    for item in objs:
        if 'HP' in item:
            HP = item   
    
    
    ###LB
    if sensLB.positive and HP['Alive'] == True:
        sr = 's' + str(arch['aslot']) + 'r'
        if arch[sr] == False and HP['Alive'] == True:
            if arch['tus'] < cubtus:
                arch['tus'] += 1
                bow = cub[:1]
               
                cont.activate(cont.actuators[bow + 'la'])
            else:
                if arch['tus'] == cubtus:
                    cont.activate(cont.actuators['shootsound'])
                    arch[sr] = True
                    arch['tus'] += 1
                    cont.activate(cont.actuators['S' + cub])
                    bow = cub[:1]
                    cont.activate(cont.actuators[bow + 'ns'])
                    
    else:
        arch['tus'] = 0
        bow = cub[:1]
        cont.activate(cont.actuators[bow + 'ns']) 
    
       
   
    ###Ground
    if sensGR.positive and HP['Alive'] == True:
        
        cont.activate(cont.actuators['ground'])
        arch['onground'] = True
    if sensAR.positive and HP['Alive'] == True:
        cont.activate(cont.actuators['air'])
        arch['onground'] = False
    
    
    ###A
    if sensA.positive and HP['Alive'] == True:
        cont.activate(cont.actuators['left'])
        
    else:
        cont.deactivate(cont.actuators['left'])
    ###D
    if sensD.positive and HP['Alive'] == True:
        cont.activate(cont.actuators['right'])
        
    else:
        cont.deactivate(cont.actuators['right'])
    ###Spacebar
    if sensSB.positive and HP['Alive'] == True and arch['onground'] == True:
        cont.activate(cont.actuators['Up'])
        cont.activate(cont.actuators['js'])
    else:
        cont.deactivate(cont.actuators['Up'])
        
        
        
def archer3():
    ###Mouse movement -> archer looking to mouse cursor
    cont = logic.getCurrentController()
    scene = logic.getCurrentScene()
    objs = scene.objects
    for item in objs:
        if 'HP' in item:
            HP = item
    mouse = cont.sensors['mmovement']
    if mouse.positive and HP['Alive'] == True:
        ls = bge.render.getWindowWidth() / 2
        mx = mouse.position[0] 
        for item in objs:
            if 'sprite' in item:
                sprite = item
        if mx < ls:
            sprite.localScale = [ -1.0000, 1.0000, 1.0000]
            
        else:
            sprite.localScale = [ 1.0000, 1.0000, 1.0000]
            
        
            
        

Well, nevermind, if you can find something wrong in my code please say it. but otherwise I don’t care anymore. I have a good gaming pc where I am making my game on, and I just tested my game on the most crappiest pc of the world and it had a few little lag spikes by loading new objects and scenes, but it worked perfect. So yeah… nevermind…
Sorry for posting the most vague post ever :wink:

Percentages are useless when it comes to speaking generally of your performance. If you game does 4 things, then each thing may take 25% of the time, but 0.0000000000ms. Percentages are used to indicate which is the slowest component, and from that information, analyse the timing (ms) to see how slow the component actually is.

That outside time is probably the BGE’s rather poor frame rate limiter. I’d recommend disabling the Use Framerate (or whatever it’s called) option and relying more on VSync. Make sure none of your scripts are frame rate dependent by using change in time instead of change in frame (e.g., every tick). Also, disable VSync when profiling for more accurate results.

@agoose77, yes that was what I was trying to tell, thanks.
@Moguri, thanks for your helpful information, I have use framerate and Vsync on. Maybe that is what is causing my lag. I’ll try without use framerate and tell you the result.

EDIT: Yup, use framerate caused my lag. Thanks

Replacing all logic bricks is the quite similar to using logic bricks only. It is much better to use Python when it is better to use Python and use logic bricks when it is better to use logic bricks. Even when people think Python makes everything “magically” fast … it does not. It is just another way to describe behavior. It is in direct comparison slower than logic bricks. The important thing is to know what you want and how you can get that. It is even better if you know about several different ways and you can judge them.

Your code does not look like it has potential bottlenecks. To be honest I do not know if you will still understand that code after a one week vacation.

… wait there is a potential bottleneck (and design issue as well):


for item in objs:
        if 'HP' in item:
            HP = item   

(several times)
and


       for item in objs:
            if 'sprite' in item:
                sprite = item

As more objects you have as more this will eat processing time, especially when you run this all the time (I guess you run that all the time).

Maybe it helps

Yes, I heard that already a few times. But in my case I had a ocean of logic bricks: 1 big mess. And I wanted things that are not possible/extremely hard to do with logic bricks but they are simple to do with python.

To be honest I do not know if you will still understand that code after a one week vacation.

Lol. I never had problems with that before, but I really understand you :P. But it is sure more clear than 1 big mess of logic bricks.

… wait there is a potential bottleneck (and design issue as well):
Code:
for item in objs:
if ‘HP’ in item:
HP = item
(several times)
and
Code:
for item in objs:
if ‘sprite’ in item:
sprite = item
As more objects you have as more this will eat processing time, especially when you run this all the time (I guess you run that all the time).

Ok, I was already afraid for that but what is another way to get an object by it’s property name? Maybe it’s possible to get a group its objects instead of a scene its objects?

Thanks for your information!!

with python

1 object doing 1 thing with logic = faster then python

1 object doing 1 thing with python that logic could do = slow

1 object doing 1 thing to a list of 100 objects = faster then 100 objects using logic

edit: about grouping

have a empties spawn in any objects that are to be added to the list,

each object adds itself to a list, if its not on it.

     
import bge
cont = bge.logic.getCurrentController()
own = cont.owner

added = bge.logic.getCurrentScene().addObject('AddedObjectName',own,0)

Keeper = bge.getCurrentScene().objects['Keeper']

if 'NameOfListOfThings' not in Keeper:
   Keeper['NameOfListOfThings']=[]

if added not in Keeper['NameOfListOfThings']:
    Keeper['NameOfListOfThings'].append(added)

own.endObject()