1
$\begingroup$

I have written a script in python that successfully saves and loads the position and rotation of my player cube and camera for use between scene transitions. However, something is going wrong with the minimum and maximum rotation values of the mouse look actuator. I have a feeling that I should be adjusting those values in python at the same time I change the camera rotation from the save, but I'm really hung up on this.

Here is the code I wrote:

import bge
from bge import logic as g

def save():
    s = g.getCurrentScene()
    pos = s.objects["player"].position
    rotP = s.objects["player"].worldOrientation
    rotC = s.objects["player_camera"].localOrientation

    g.globalDict["pos"] = [pos.x, pos.y, pos.z]
    g.globalDict["rotP"] = rotP.to_euler()

    g.globalDict["rotC"] = rotC.to_euler()

    print("SAVED")

def load():
    s = g.getCurrentScene()
    s.objects["player"].position = g.globalDict["pos"]
    s.objects["player"].worldOrientation = g.globalDict["rotP"]
    s.objects["player_camera"].localOrientation = g.globalDict["rotC"]

    print("LOADED")

It's as if these values are being changed by something: enter image description here

Thank you for your time. I'm really stuck on this.

$\endgroup$
1
$\begingroup$

You should use the actuator.angle attribute, or actuator.angle[0] if only the X value is needed. Here's a example script I made and test file attached here.

import bge

from bge.logic import expandPath
from ast import literal_eval

def main(cont):

    # Objects
    own = cont.owner

    # Sensors
    S = cont.sensors['S']
    L = cont.sensors['L']

    # Actuators
    MLook = own.actuators['MLook']

    # Properties
    path = expandPath('//save.txt')

    data = {
        'pos' : list(own.worldPosition),
        'rot' : list(own.localOrientation.to_euler()),
        'mouse' : MLook.angle
        }

    ### PROCESSING ###
    # Save
    if S.positive:

        with open(path, 'w') as opened_file:
            opened_file.write(str(data))
            print('Saved data to', opened_file.name)

    # Load  
    if L.positive:

        try:
            with open(path, 'r') as opened_file:
                data = literal_eval(opened_file.read())
                print('Saved data to', opened_file.name)

            # Set obj data
            own.worldPosition = data['pos']
            own.localOrientation = data['rot']
            MLook.angle = data['mouse']

        except:
            print('Cant load saved file')
$\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.