1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

cannot convert from 'GTANetworkShared.NetHandle' to 'GTANetworkShared.VehicleHash

Discussion in 'Archive' started by Sartex, Mar 26, 2017.

  1. Sartex

    Sartex New Member

    Messages:
    2
    Likes Received:
    0
    Joined:
    Mar 25, 2017
    Hello everybody, im a new member in this community and have just put up a server.

    I´ve started to develope my own scripts, and now in a couple scripts i´ve encoutered this same problem.

    I am trying to make a script which gives the player a parachute in case he jumps from a plane or a helicopter. For that, i need to use API.getVehicleClass and the event onPlayerExitVehicle.

    Ok, so my problem is that, i dont quite understand how this VehicleHash, NetHandle thing works. I cannot directly use the NetHandle vehicle for the function so i need instructions. I´l be more than happy for recieving help.

    Code:
    private void CheckIfLeavesFlyingVehicle(Client player, NetHandle vehicle)
            {
                int v = API.getVehicleClass(vehicle);
    
                if (v == 15 || v == 16 ) {
                    API.givePlayerWeapon(player, WeaponHash.Parachute, 1, true, false);
                }
            }
    I am getting this error with this code:

    Thanks for helping !
     
  2. stennu

    stennu Member

    Messages:
    9
    Likes Received:
    1
    Joined:
    Jan 2, 2017
    Code:
            private void CheckIfLeavesFlyingVehicle(Client player, NetHandle vehicle)
            {
                int v = API.getVehicleClass((VehicleHash)player.vehicle.model);
    
                if (v == 15 || v == 16)
                {
                    API.givePlayerWeapon(player, WeaponHash.Parachute, 1, true, false);
                }
            }
    
    
    Try this.
     
  3. Amit_B

    Amit_B Member Language Board Moderator

    Messages:
    40
    Likes Received:
    21
    Joined:
    Oct 27, 2016
    For you to understand:

    `Vehicle` is the vehicle element (like vehicleid). With this you can get details of the vehicle (position, model, ...) and more.
    `VehicleHash` is the vehicle model element (like modelid). This can be either a VehicleHash or an int, but they both would have the same result.
    `NetHandle` is the network handle element. This is a feature of GTANetwork which basically means an unique ID for every type of entity (player, vehicle, etc).

    Then the code above should work, however, there's a possibility that player.vehicle will be 'null' as the player exited the vehicle (it depends if onPlayerExitVehicle called before the player exited the vehicle or after).
    If it won't work notify us so we can help you find an alternative.
     
  4. Sartex

    Sartex New Member

    Messages:
    2
    Likes Received:
    0
    Joined:
    Mar 25, 2017
    @Amit_B @stennu Thank you! I know now how to use the hashes in the code.

    But the code itself does not work. I think the problem might be something related to the event onPlayerExitVehicle like amit said in his answer.
     
  5. Amit_B

    Amit_B Member Language Board Moderator

    Messages:
    40
    Likes Received:
    21
    Joined:
    Oct 27, 2016
    Which means that you need to find the last vehicle the player was in. There are two ways I can think of to do that:
    1. Finding the vehicle by the NetHandle - most reliable way but a bit hard.
    2. Saving the player's last vehicle when he enters a vehicle - much simpler. To do that, use setEntityData whenever the player enters a vehicle. When he leaves the vehicle, load the saved data with getEntityData.

    For solution 1 you'll need to create an list of connections between vehicle & nethandle. To do that you can use Dictionary, a C# collection allows to define key and value types, something like Dictionary<NetHandle, Vehicle>. After creating the collection variable itselfs, you should add any vehicle you create to this array. After doing this, finding a vehicle by its NetHandle would be much easier, faster and reliable than any other way -> vehicleNetHandleList[vehicle] -> returns the Vehicle element.

    If you need more help don't hesitate to ask.
     

Share This Page