0
\$\begingroup\$

I am developing a car control plugin for GTA IV and everything is working except the lock/unlock doors feature.

Despite using the Player.LastVehicle handle, the code only works while the player is in the car. How can I get the code to work when the player is inside or outside the car?

Here's what I have so far:

'Lock/Unlock
        If e.Key = Keys.OemPipe AndAlso e.Control Then
            'Lock Codes: 1 - Unlocked; 2 - Locked (Player & NPCs); 3 - Locked (Player); 4 - Locked (Player, Inside); 7 - Locked (Can Break Into)
            If DoorLock = False Then
                Player.LastVehicle.HazardLightsOn = True
                Wait(1000)
                Player.LastVehicle.HazardLightsOn = False
                Native.Function.Call("LOCK_CAR_DOORS", Player.LastVehicle, 2)
                DoorLock = True
                Game.DisplayText("Vehicle Locked")
            Else
                Player.LastVehicle.HazardLightsOn = True
                Wait(2000)
                Player.LastVehicle.HazardLightsOn = False
                Native.Function.Call("LOCK_CAR_DOORS", Player.LastVehicle, 1)
                DoorLock = False
                Game.DisplayText("Vehicle Unlocked")
            End If
        End If
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I've never worked with GTA mods but assuming that if you can't actually get LastVehicle when outside of the car (which seems unintuitive to me, given the name) my guess would be to track the last vehicle yourself. When the player is in the car, store LastVehicle as a variable. Then you can refer to it whenever you want.

Update (how to store LastVehicle as a variable):

It's been a while since I did any VB coding and as I said, I've never done any GTA modding so this is just a guess but hopefully I'm not too far off...

Private lastVehicle As Vehicle
If (Not Player.LastVehicle Is Nothing) Then
    lastVehicle = Player.LastVehicle
End If
\$\endgroup\$
4
  • \$\begingroup\$ Yes, that has come to mind. The problem is I don't know how to store the car to a variable. \$\endgroup\$
    – Dog Lover
    Commented Aug 23, 2015 at 2:51
  • \$\begingroup\$ @DogLover I've updated my answer with an example of how to store the variable. \$\endgroup\$ Commented Aug 23, 2015 at 18:50
  • \$\begingroup\$ Thank you. I will let you know if it works when I get home tonight. \$\endgroup\$
    – Dog Lover
    Commented Aug 23, 2015 at 21:51
  • \$\begingroup\$ Sorry for getting back late to you. Unfortunately it is still not working. System.NullReferenceException: Object reference not set to an instance of an object. \$\endgroup\$
    – Dog Lover
    Commented Aug 30, 2015 at 3:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .