Problem with Obj-C

I am currently working on a project that adds support for game controllers through Objective-C. I am trying to get haptics working, and I’ve almost done it. However, whenever I try to call the function:

engine:createPlayerWithPattern_error_(pattern, err)

The variable that I assign the new pattern player to is nil, which shouldn’t happen. If you need a bit more context, I suggest you go to this link: https://developer.apple.com/documentation/corehaptics?language=objc and read CHHapticEngine, CHHapticEvent, CHHapticPattern and CHHapticPatternPlayer. Anyway, I’m using an instance of CHHapticEngine that was created from this line of code:

objc.GCController.current.haptics:createEngineWithLocality_()

I’ve also checked that the method is spelled right too, so it’s definitely not a spelling mistake. If someone could attempt to help me figure out this mess, I would very greatly appreciate it!

Hi @Creator27,

It wasn’t easy, but here’s a sample that makes my PS5 controller vibrate. Hopefully, this should unblock you :slight_smile:

function setup()
    --- haptics: objc.GCDeviceHaptics
    if objc.GCController.current then
        haptics = objc.GCController.current.haptics

        engine = haptics:createEngineWithLocality_("Default")

        --- CHHapticEventParameter: objc.CHHapticEventParameter
        CHHapticEventParameter = objc.CHHapticEventParameter
        intensity = CHHapticEventParameter:alloc():initWithParameterID_value_("HapticIntensity", 1.0)
        sharpness = CHHapticEventParameter:alloc():initWithParameterID_value_("HapticSharpness", 1.0)
        parameters = { intensity, sharpness }

        --- CHHapticEvent: objc.CHHapticEvent
        CHHapticEvent = objc.CHHapticEvent
        event = CHHapticEvent:alloc():initWithEventType_parameters_relativeTime_("HapticTransient", parameters, 0.0)
        
        --- CHHapticPattern: objc.CHHapticPattern
        CHHapticPattern = objc.CHHapticPattern
        pattern = CHHapticPattern:alloc():initWithEvents_parameters_error_({event}, {}, nil)

        --- engine: objc.CHHapticEngine
        engine:startAndReturnError_(nil)

        --- player: objc.PatternPlayer
        player = engine:createPlayerWithPattern_error_(pattern, nil)
        player:startAtTime_error_(0.0, nil)
    else
        print("objc.GCController.current is nil, make sure controller is connected and try again.")
    end
end

function draw()
    
end
1 Like

@jfperusse thank you so much for your contribution! I’ve been waiting nearly two weeks for someone to reply and you really delivered so thank you!

1 Like