is it possible to run a python script list threaded?

You would be wise to post your code. More than likely it can be written more efficiently, without loosing flexibility. The general attitude should be - is the task difficult? (for most bge games, no.) if no, then your code is inefficient. I mentioned frameworks, because they are the only place you might start to loose performance (e.g my game framework)

A-frame work is not this, this is a game :slight_smile:

Sent from my SM-G920F using Tapatalk

the code I am using is posted, except for a few minor modifications on the first post,

ah, ok, I can see your code.

Unfortunately it is pretty complex and seem to serve a lot of different purposes.

Some remarks:


def main():

main() is unnecessary and adds no value (not even a meaningful name). Even worse it forces the reader to scroll to the end of the file just to see that you have to go to top again. This is no crime thriller where I want to see the murderer beforehand. -> remove it


if 'CompList' in own:

Why do you check this? Isn’t your code expecting it to be there? -> unnecessary check = remove it


if type(own['CompList']) is not str:

Why do you expect this property to be a string? Either you want it to be a string or something else. Does the content change often? Why you would change the type? If you want a string value use a different property name.

A frequent check for it’s type should only be necessary when the content changed. But there is no reason to write unexpected values into this property (non-string). If something is doing that - that change is your problem not the validation -> remove the check.


for component in own['CompList']:

lopping over a list … no big deal. Due to the immense size of the following code block I suggest to place it into one or more functions that deals with a single component at the time.


if 'health' in component:

Again, why do you even have a component that does not has this property? Your code does not do anything with it = wasted processing time -> remove this check.


if component.parent:

Why do you expect a component without parent? Your code does not do anything with it = wasted processing time -> remove this check.


for slots in component.parent['Jacks2']:
    if slots==component:

This looks like a linear search to me. Better use the Python function list.index(x). It is much more efficient than your code.

-> it might be worth to simply remove the object from the list, rather than to rely on the index. But I do not know if this is information that you need at another place.


if type(component['child']) is not str:

Why do you expect this property to be a string? Either you want it to be a string or something else. Does the content change often? Why you would change the type? If you want a string value use a different property name.


component['child']

Dictionary search is pretty fast. But you have the result already. Why do you do a dictionary search 6 times in a row. If you found a result in the first place, you will find the same result at the sixth time too -> write the first result into a variable and use the variable. Variable access is much faster than dictionary search.


component['child']['Owner']="Empty"
component['child']="NotSet"

There is a nifty little value called None. Python very good in identifying this value. It is much faster than comparing strings. --> replace “Empty” and “NotSet” with constant None as that is what this constant expresses (other languages call that nil, null etc.).


40. if 'CompList' in own:

Beside the statement above … why do you check it again? unnecessary -> remove the check


45. if component['Type']=="Antigrav":
...
52. if component['Type']=='Lifter':

You already know it is an “Antigrav”. No need to check for “Lifter” again -> replace “if” with “elif” at the subsequent checks.

This is enough for now. Even with this few changes your code should become smaller and more efficient.
In general I suggest to split the code. As it is right now - nobody can understand what it is doing without scrolling around and around and around -> who has time to do that. Finally there is not really much that tells what it is supposed to do (I can’t read your mind ;)).

see this:


if component['Type']=="Antigrav":
    processAntigrav(component)
elif component['Type']=="Lifter":
    processLifter(component)
if component['Type']=='Holder':
    processHolder(component)
if if component['Type']=='Head':
    processHead(component)

Then you can focus on the single component.

This could be even more abstracted by using of classes. But this is a different topic.

Some of those checks

if component is a holder, the property child will be a string if there is no weapon
attached,

about if health in component, I can remove that, and ensure all components and cores have health,

when the game starts, the component list is a string “Empty”

lots of these filters were do to python throwing a error for a single frame.
I can probably clean them up some.

about the linear search through the jack table,
I check to see if any of the components are a head, and then check the heads alignment, and then set the alignment of the body based on if a ‘evil’ head is attached.

You can set an empty list by using

components = []

using a for ob in list with an empty list will just skip the operation, it won’t raise an error.

so if you do:

components = []
for component in components:
    print ("x")

it won’t print “x”. :slight_smile: this is very useful when using lists. You can also find that

components = []
if components:
    print ("A")
if not components:
    print ("B")

Will print “B”.

Also using None is the best way to show that something is not active when dealing with other types such as strings or objects.

parent = None

if parent:
    print ("y")
if not parent:
    print ("z")

That makes checks very easy and fast.

if component.children:

yeah, but then if a navtarget is targeting a gun arm, it will think it has a gun
when in fact it’s child is a navtarget :smiley:

the creating a empty list thing *

I only need to do it one time, as the list is not regenerated each frame,

Why? it makes no sense? Better use one property to carry the string and another to carry the object. Mixing that creates confusion and is error prone.

Yes, that is a good idea.

Why? it is much better if the component list is simply not there. When you create it, you create a list not a string. A string makes no sense.

Yes, that is the plan. It tells you something is not setup correctly. Fix the setup and the errors are gone. This is much more efficient than validating the input all the time.

If you separate your code more efficiently, your stack tracebacks will indicate where the error happened in a more useful fashion. Small functions with a limited number of inputs are easier to fix than large functions with lots of mixed data.

Originally Posted by BluePrintRandom http://blenderartists.org/design/baorg2012dark/images/buttons/viewpost-right.png
when the game starts, the component list is a string “Empty”

Why? it is much better if the component list is simply not there. When you create it, you create a list not a string. A string makes no sense.

I use

if property does not equal empty------------Run list

but I guess that 99% of the time, the list is not empty so, I guess I can change this.

That is not the point.

The points are:

  • The code does nothing in the alternate case.
  • If you know the value will not change anymore, you do not need to validate again and again. Typically you setup/initialize your configuration beforehand (and just once), that the assumptions you made are valid.