0
$\begingroup$

Im trying to get 2 modules to work together and get executed from MainScript

MainScript:

import Map
#calling the "Map.module" from mainscript
def map():
    Map.module()
    return

This is what "Map.module" looks like:

import bge
import Functions
class module():
    def __init__(self):
        self.cont = bge.logic.getCurrentController()
        self.own = self.cont.owner
        self.openmap = self.cont.sensors["m"]
        self.oooopenmap()
    def oooopenmap(self):
        if self.openmap.positive:
         ###Add Functions.py module here:
            Functions.module()

And this is what "Functions.module", getting executed when openmap.positive is true, looks like:

import bge 
class module():
    def __init__(self):
        self.cont = bge.logic.getCurrentController()
        self.own = self.cont.owner
        self.pause = self.own.actuators["Pause"]
        self.pausee()
    def pausee(self):
        self.cont.activate(self.pause)

The console gives me this keyError:

Traceback (most recent call last): File "test.py", line 14, in File "test.py", line 10, in functions File

"\Functions.py", line 23, in main KeyError: 'requested item "Scene" does not exist'

Or to probably point a finger at my problem this ValueError:

Traceback (most recent call last): File

"Main.py",line 18, in map File

"Map.py",line 27, in init File

"Map.py",line 36, in oooopenmap File

"Functions.py",line 22, in init File

"Functions.py",line 26, in pausee ValueError: Pause not in this python controllers actuator list

can someone help ?

greetz

$\endgroup$
1
$\begingroup$

Your question and your issues have no real relation to each other. Therefore I split them into separate sections.

KeyError

Traceback (most recent call last): File "test.py", line 14, in File "test.py", line 10, in functions File

"\Functions.py", line 23, in main KeyError: 'requested item "Scene" does not exist'

Analysis

There is an error in file test.py. As the error says you try to find an item with key "Scene". But there is no such item. I guess the code is supposed to find a scene. But there is no scene "Scene" loaded.

Suggestion

Check the scene names and what scenes are loaded at the moment of execution of this code. Ensure the scene is loaded and has the name "Scene".

ValueError

Traceback (most recent call last): File

"Main.py",line 18, in map File

"Map.py",line 27, in init File

"Map.py",line 36, in oooopenmap File

"Functions.py",line 22, in init File

"Functions.py",line 26, in pausee ValueError: Pause not in this python controllers actuator list

Analysis

I did not had that error in the past. I guess it is pretty much the same as the KeyError. You try to get an actuator called "Pause", which is not connected to the current actuator.

Suggestion

Ensure the currently running controller is connected to an actuator called "Pause".

How to combine modules?

This is a plain Python question.All you need is "import" as you already do.

You can import any module into any script/module. The imported module can be in

  • a text block
  • a file that resides in the python search path
  • a package that resides in the python search path.

Caching context

I strongly suggest to avoid storing context data (current controller, current scene) when it is supposed to be used as context. You can't be sure this is still valid within any subsequent uses of the class.

To be more specific: you store a reference of the current controller inside of the object "module". But when you call any function of that object from another controller, this class still refers to the original controller.

This can be the reason why you get the ValueError (see the section above).

Example: your controller "Python" executes Python code that creates a new object of class "module".

myModuleObject = Map.module()

A controller "Python.001" executes Python code that uses with this object:

myModuleObject.oooopenmap()

This can fail as the current controller (Python.001) is not the stored controller (Python). You can only activate actuators that are connected to the current controller regardless what controller you store in your object.

Recommendation

Always read the current controller from context. Store it in a local variable, but not in a class or module variable. Do not store it in a game object property.

You can store the current object and the current scene when it is not meant to be used as "current". E.g. you want to store the current object as target.

I hope this helps a bit

$\endgroup$
0
$\begingroup$

I think self.own.actuators should just read self.actuators or self.cont.actuators

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.