Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Components start method issues with getSceneList() & createConstraint() #335

Closed
Maujoe opened this issue Dec 10, 2016 · 6 comments
Closed
Labels

Comments

@Maujoe
Copy link

Maujoe commented Dec 10, 2016

I notice that the start method of the python components has some issus in upbge.

When I call bge.logic.getSceneList() in the start method it return an empty list.
The same happends with bge.constraints.createConstraint(..) that return None.
In the update method everything works right.
Here is a file:
components_start_bug.zip

When I use agoose77's Addon (https://blenderartists.org/forum/showthread.php?393763-Python-Components-Addon) I have no issues with the start method.

I know nothing about the bge code but I think it could be a timing problem in the Initialization state or something like that.

@youle31 youle31 added the bug label Dec 11, 2016
@sdfgeoff
Copy link

This is because the objects present in a scene are created before the scene is first added.
The simple solution is to do:

import bge
from collections import OrderedDict

class loadsequence(bge.types.KX_PythonComponent):
    '''The Player'''
    args = OrderedDict([
    ])
    def start(self, args):
        print("This runs before the scene is fully added to the game")
        self.update = self.init

    def init(self):
        print("This runs on the first frame with everything in the scene")
        self.update = self.run
        print(bge.logic.getSceneList())

    def run(self):
        print("This will run every frame after")

@youle31
Copy link
Collaborator

youle31 commented Dec 15, 2016

Precision: I marked it as a bug because it makes my blender crash at ge start.

panzergame added a commit that referenced this issue Dec 16, 2016
Previously, components were initialized independently of the component update.
This operation was made in the conversion and in the object replication, but
some issues were noticed: in case of libloading the component must be run in
an unmerged scene but they have to acces to the new scene. The workaround was
to bind the final scene before initialize the python component, but this
solution is unstable.

To avoid this problematic case, the components could be initialized at the
begining of theirs first update. As we ensure that the update is independent
of the lib loading and the object creation.

To realize this solution, the conversion from blender struct PythonComponent
to KX_PythonComponent previously made in KX_GameObject::InitComponents() is
now moves in the converter function: BL_ConvertComponentsObject.
This function converts all the components, put them in a list and set the list
to the game object thanks to KX_GameObject::SetComponents(…).
This conversion is made only for objects converted and doesn't manage the
duplicated object. These duplicated objects must use a new python component
with the same python proxy subclassing. To do so the function GetReplica()
in KX_PythonComponent is overrided. This function is used when the game
object component list is replicated, in the same time each component remap
their game object owner.

The initialization is made in KX_PythonComponent::Start when the member m_init
is checked to false in Update(). But the components have to know which blender
component they are using and which game object is owning them. To answer this
problem the new setter SetBlenderPythonComponent is called in the same time
of SetGameObject when the components are converted.

The initialization is now independent of libloading but not about object duplication.
To avoid issues iterating on the list of objects which can be modified, a second
list is made in KX_Scene::LogicUpdateFrame containing all the objects using
a component at the begining of the frame update. Added object will be update
in the next frame as before.

Reported by Maujoe in #335.
@panzergame
Copy link
Contributor

@Maujoe, @sdfgeoff, @youle31 : Could you confirm the issue is gone and close the issue in consideration ?

@youle31
Copy link
Collaborator

youle31 commented Dec 17, 2016

@panzergame : The crash is fixed for me.

I have this error in the console in Maujoe test file:

Blender Game Engine Started
Debug: Game Controller (XInput Controller) with index 0 initialized
Traceback (most recent call last):
File "C:\Users\yul\Desktop\components_start_bug.blend\comp.py", line 15, in start
File "C:\Users\yul\Desktop\components_start_bug.blend\comp.py", line 30, in constraint_test
SystemError: Representation not overridden by object.
Blender Game Engine Finished
Debug: Game Controller (XInput Controller) with index 0 closed

but I don't know if this is a bug or only due to a wrong usage of start method.

@panzergame
Copy link
Contributor

@youle31 : Iirc it's because constraintvariable in the code isn't printable, but it's to check.

@youle31
Copy link
Collaborator

youle31 commented Dec 17, 2016

All right :) This sounds good. I close the issue. Thanks panzergame. Fixed in 814afdf for crash and in 86adc3c for constraints representation

@youle31 youle31 closed this as completed Dec 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants