• Dear visitors,

    The email issue has been finally solved.
    Thank you for your patience and happy browsing.

    Team ACM.

How do I make this work (App using shared memory not working)

Unre4alKing!23

New Member
Python:
import sys
import os
import ac
import acsys

sys.path.insert(len(sys.path), 'apps/python/LapTimeOptimizer/third_party').


from third_party.sim_info import info

# Labels for telemetry data
l_lapcount = 0
l_speed = 0
l_throttle = 0
l_brake = 0
l_gear = 0
l_rpm = 0
l_fuel = 0  # Label for fuel

lapcount = 0  # Stores the current lap count

def acMain(ac_version):
    global l_lapcount, l_speed, l_throttle, l_brake, l_gear, l_rpm, l_fuel

    appWindow = ac.newApp("LapTimeOptimizer")
    ac.setSize(appWindow, 250, 300)  # Increase the window size to fit more labels

    ac.log("Telemetry App Initialized")
    ac.console("Telemetry App Running...")

    # Lap Count
    l_lapcount = ac.addLabel(appWindow, "Laps: 0")
    ac.setPosition(l_lapcount, 3, 30)

    # Speed (km/h)
    l_speed = ac.addLabel(appWindow, "Speed: 0 km/h")
    ac.setPosition(l_speed, 3, 50)

    # Throttle
    l_throttle = ac.addLabel(appWindow, "Throttle: 0%")
    ac.setPosition(l_throttle, 3, 70)

    # Brake
    l_brake = ac.addLabel(appWindow, "Brake: 0%")
    ac.setPosition(l_brake, 3, 90)

    # Gear
    l_gear = ac.addLabel(appWindow, "Gear: N")
    ac.setPosition(l_gear, 3, 110)

    # RPM
    l_rpm = ac.addLabel(appWindow, "RPM: 0")
    ac.setPosition(l_rpm, 3, 130)

    # Fuel
    l_fuel = ac.addLabel(appWindow, "Fuel: 0%")
    ac.setPosition(l_fuel, 3, 150)

    return "LapTimeOptimizer"


def acUpdate(deltaT):
    global l_lapcount, l_speed, l_throttle, l_brake, l_gear, l_rpm, l_fuel, lapcount

    # Fetch data from Assetto Corsa using sim_info
    laps = ac.getCarState(0, acsys.CS.LapCount)
    speed = ac.getCarState(0, acsys.CS.SpeedKMH)
    throttle = ac.getCarState(0, acsys.CS.Gas) * 100  # Convert to percentage
    brake = ac.getCarState(0, acsys.CS.Brake) * 100  # Convert to percentage
    gear = ac.getCarState(0, acsys.CS.Gear)
    rpm = ac.getCarState(0, acsys.CS.RPM)

    # Fuel level using sim_info
    fuel = info.physics.fuel

    # Update labels
    if laps > lapcount:
        lapcount = laps
        ac.setText(l_lapcount, "Laps: {}".format(lapcount))

    ac.setText(l_speed, "Speed: {:.1f} km/h".format(speed))
    ac.setText(l_throttle, "Throttle: {:.0f}%".format(throttle))
    ac.setText(l_brake, "Brake: {:.0f}%".format(brake))
    ac.setText(l_gear, "Gear: {}".format(gear if gear > 0 else "N"))
    ac.setText(l_rpm, "RPM: {}".format(rpm))
    ac.setText(l_fuel, "Fuel: {:.0f}%".format(fuel * 100))  # Displaying fuel percentage
The code above is my LapTimeOptimizer.py regular script.

The code below is my sim_info.py

Python:
"""
Original sim_info.py by >>> Rombik <<<

I didn't change much here , beside ctypes import.
    -Hunter Vaners-
"""


import mmap
import functools


# ------------------------------------

# To import ctypes , we need to see if the system is 64 or 32 bits

import platform
import os
import sys

if platform.architecture()[0] == "64bit":
    libdir = 'lib64'
else:
    libdir = 'lib'
sys.path.insert(0, os.path.join(os.path.dirname(__file__), libdir))
os.environ['PATH'] = os.environ['PATH'] + ";."

import ctypes
from ctypes import c_int32, c_float, c_wchar
# ------------------------------------





AC_STATUS = c_int32
AC_OFF = 0
AC_REPLAY = 1
AC_LIVE = 2
AC_PAUSE = 3
AC_SESSION_TYPE = c_int32
AC_UNKNOWN = -1
AC_PRACTICE = 0
AC_QUALIFY = 1
AC_RACE = 2
AC_HOTLAP = 3
AC_TIME_ATTACK = 4
AC_DRIFT = 5
AC_DRAG = 6
AC_FLAG_TYPE = c_int32
AC_NO_FLAG = 0
AC_BLUE_FLAG = 1
AC_YELLOW_FLAG = 2
AC_BLACK_FLAG = 3
AC_WHITE_FLAG = 4
AC_CHECKERED_FLAG = 5
AC_PENALTY_FLAG = 6

class SPageFilePhysics(ctypes.Structure):
    _pack_ = 4
    _fields_ = [
        ('packetId', c_int32),
        ('gas', c_float),
        ('brake', c_float),
        ('fuel', c_float),
        ('gear', c_int32),
        ('rpms', c_int32),
        ('steerAngle', c_float),
        ('speedKmh', c_float),
        ('velocity', c_float * 3),
        ('accG', c_float * 3),
        ('wheelSlip', c_float * 4),
        ('wheelLoad', c_float * 4),
        ('wheelsPressure', c_float * 4),
        ('wheelAngularSpeed', c_float * 4),
        ('tyreWear', c_float * 4),
        ('tyreDirtyLevel', c_float * 4),
        ('tyreCoreTemperature', c_float * 4),
        ('camberRAD', c_float * 4),
        ('suspensionTravel', c_float * 4),
        ('drs', c_float),
        ('tc', c_float),
        ('heading', c_float),
        ('pitch', c_float),
        ('roll', c_float),
        ('cgHeight', c_float),
        ('carDamage', c_float * 5),
        ('numberOfTyresOut', c_int32),
        ('pitLimiterOn', c_int32),
        ('abs', c_float),
        ('kersCharge', c_float),
        ('kersInput', c_float),
        ('autoShifterOn', c_int32),
        ('rideHeight', c_float * 2),
        ('turboBoost', c_float),
        ('ballast', c_float),
        ('airDensity', c_float),
        ('airTemp', c_float),
        ('roadTemp', c_float),
        ('localAngularVel', c_float * 3),
        ('finalFF', c_float),
        ('performanceMeter', c_float),
        ('engineBrake', c_int32),
        ('ersRecoveryLevel', c_int32),
        ('ersPowerLevel', c_int32),
        ('ersHeatCharging', c_int32),
        ('ersIsCharging', c_int32),
        ('kersCurrentKJ', c_float),
        ('drsAvailable', c_int32),
        ('drsEnabled', c_int32),
        ('brakeTemp', c_float * 4),
        ('clutch', c_float),
        ('tyreTempI', c_float * 4),
        ('tyreTempM', c_float * 4),
        ('tyreTempO', c_float * 4),
        ('isAIControlled', c_int32),
        ('tyreContactPoint', c_float * 4 * 3),
        ('tyreContactNormal', c_float * 4 * 3),
        ('tyreContactHeading', c_float * 4 * 3),
        ('brakeBias', c_float),
        ('localVelocity', c_float * 3),

    ]

class SPageFileGraphic(ctypes.Structure):
    _pack_ = 4
    _fields_ = [
        ('packetId', c_int32),
        ('status', AC_STATUS),
        ('session', AC_SESSION_TYPE),
        ('currentTime', c_wchar * 15),
        ('lastTime', c_wchar * 15),
        ('bestTime', c_wchar * 15),
        ('split', c_wchar * 15),
        ('completedLaps', c_int32),
        ('position', c_int32),
        ('iCurrentTime', c_int32),
        ('iLastTime', c_int32),
        ('iBestTime', c_int32),
        ('sessionTimeLeft', c_float),
        ('distanceTraveled', c_float),
        ('isInPit', c_int32),
        ('currentSectorIndex', c_int32),
        ('lastSectorTime', c_int32),
        ('numberOfLaps', c_int32),
        ('tyreCompound', c_wchar * 33),
        ('replayTimeMultiplier', c_float),
        ('normalizedCarPosition', c_float),
        ('carCoordinates', c_float * 3),
        ('penaltyTime', c_float),
        ('flag', AC_FLAG_TYPE),
        ('idealLineOn', c_int32),
        ('isInPitLine', c_int32),
        ('surfaceGrip', c_float),
        ('mandatoryPitDone', c_int32),
        ('windSpeed', c_float),
        ('windDirection', c_float),

    ]

class SPageFileStatic(ctypes.Structure):
    _pack_ = 4
    _fields_ = [
        ('_smVersion', c_wchar * 15),
        ('_acVersion', c_wchar * 15),
        ('numberOfSessions', c_int32),
        ('numCars', c_int32),
        ('carModel', c_wchar * 33),
        ('track', c_wchar * 33),
        ('playerName', c_wchar * 33),
        ('playerSurname', c_wchar * 33),
        ('playerNick', c_wchar * 33),
        ('sectorCount', c_int32),
        ('maxTorque', c_float),
        ('maxPower', c_float),
        ('maxRpm', c_int32),
        ('maxFuel', c_float),
        ('suspensionMaxTravel', c_float * 4),
        ('tyreRadius', c_float * 4),
        ('maxTurboBoost', c_float),
        ('airTemp', c_float),
        ('roadTemp', c_float),
        ('penaltiesEnabled', c_int32),
        ('aidFuelRate', c_float),
        ('aidTireRate', c_float),
        ('aidMechanicalDamage', c_float),
        ('aidAllowTyreBlankets', c_int32),
        ('aidStability', c_float),
        ('aidAutoClutch', c_int32),
        ('aidAutoBlip', c_int32),
        ('hasDRS', c_int32),
        ('hasERS', c_int32),
        ('hasKERS', c_int32),
        ('kersMaxJ', c_float),
        ('engineBrakeSettingsCount', c_int32),
        ('ersPowerControllerCount', c_int32),
        ('trackSPlineLength', c_float),
        ('trackConfiguration', c_wchar * 33),
        ('ersMaxJ', c_float),
        ('isTimedRace', c_int32),
        ('hasExtraLap', c_int32),
        ('carSkin', c_wchar * 33),
        ('reversedGridPositions', c_int32),
        ('pitWindowStart', c_int32),
        ('pitWindowEnd', c_int32),

    ]

class SimInfo:
    def __init__(self):
        self._acpmf_physics = mmap.mmap(0, ctypes.sizeof(SPageFilePhysics), "acpmf_physics")
        self._acpmf_graphics = mmap.mmap(0, ctypes.sizeof(SPageFileGraphic), "acpmf_graphics")
        self._acpmf_static = mmap.mmap(0, ctypes.sizeof(SPageFileStatic), "acpmf_static")
        self.physics = SPageFilePhysics.from_buffer(self._acpmf_physics)
        self.graphics = SPageFileGraphic.from_buffer(self._acpmf_graphics)
        self.static = SPageFileStatic.from_buffer(self._acpmf_static)

    def close(self):
        self._acpmf_physics.close()
        self._acpmf_graphics.close()
        self._acpmf_static.close()

    def __del__(self):
        self.close()


For some reason it's not working when i try to access it. THe app dissapears and only if I remove the fuel variable it works. I'm not sure what to do. In apps folder i have my app called LapTimeOptimizer a folder inside called third_party and a python script called LapTimeOptimizer. In the third_party, there is two folders lib and lib64 each containing the _ctypes.pyd.
 

fughettaboutit

aka leBluem
For errors see ...Documents\Assetto Corsa\logs\py_log.txt
or with CSP - ingame in the Python debug app (can live reload your app too!)

remove the "." after syspath insert
and also remove "third_party." when importing:

8824

then its only this:
Code:
sys.path.insert(len(sys.path), 'apps/python/LapTimeOptimizer/third_party')
from sim_info import info
...and working here; (btw no need to mult fuel by 100, its in liters)
8825
 
Last edited:

Unre4alKing!23

New Member
For errors see ...Documents\Assetto Corsa\logs\py_log.txt
or with CSP - ingame in the Python debug app (can live reload your app too!)

remove the "." after syspath insert
and also remove "third_party." when importing:

View attachment 8824
then its only this:
Code:
sys.path.insert(len(sys.path), 'apps/python/LapTimeOptimizer/third_party')
from sim_info import info
...and working here; (btw no need to mult fuel by 100, its in liters)
View attachment 8825
Hey thanks for responding. One thing is it's not working for me. I have done the exact same things as you removing the period and the third_party.
 
Top