citizenfx / fivem

The source code for the Cfx.re modification frameworks, such as FiveM, RedM and LibertyM, as well as FXServer.
https://cfx.re/
3.54k stars 2.08k forks source link

[RedM] GET_SHAPE_TEST_RESULT_INCLUDING_MATERIAL - Extra Native Error #2453

Closed Alexr03 closed 6 months ago

Alexr03 commented 6 months ago

What happened?

When attempting to use the GET_SHAPE_TEST_RESULT_INCLUDING_MATERIAL native that was introduced in https://github.com/citizenfx/fivem/pull/1495 an error is thrown.

RedM_b1491_GTAProcess_9ae96165-37cc-4ffb-a21e-3b0696639cde

Expected result

Shape test result returns normal data along with material hash

Reproduction steps

This should return the shape test result of the ground in front of you.

  1. client;.lua
    
    local testCoords = GetEntityCoords(PlayerPedId()) + vector3(0.0, 0.5, 0.0)
    local testEndCoords = testCoords + vector3(0.0, 0.0, -1.5)
    local shapeTest = StartExpensiveSynchronousShapeTestLosProbe(testCoords.x, testCoords.y, testCoords.z, testEndCoords.x, testEndCoords.y, testEndCoords.z, 0, 0, 4)

local retval, hit, endCoords, surfaceNormal, materialHash, entityHit = Citizen.InvokeNative(0x4301E10C, shapeTest)

print('GET_SHAPE_TEST_RESULT_INCLUDING_MATERIAL', retval, hit, endCoords, surfaceNormal, materialHash, entityHit)



2. Observe F8 console

### Importancy

Slight inconvenience

### Area(s)

RedM

### Specific version(s)

RedM 7788

### Additional information

_No response_
thelindat commented 6 months ago

Can't you use the actual named native now, rather than invoking? GetShapeTestResultIncludingMaterial. Otherwise pretty sure you'd need to pass pointer-args.

Disquse commented 6 months ago

Thanks @thelindat, indeed if you want to return any pointer values from a raw Citizen.InvokeNative call, you should pass pointer catching arguments. Here's an example:

      local retval, hit, endCoords, surfaceNormal, materialHash, entityHit = Citizen.InvokeNative(GetHashKey("GET_SHAPE_TEST_RESULT_INCLUDING_MATERIAL") & 0xFFFFFFFF, shapeTest,
        Citizen.PointerValueInt(), Citizen.PointerValueVector(), Citizen.PointerValueVector(), Citizen.PointerValueInt(), Citizen.PointerValueInt(), Citizen.ReturnResultAnyway())

Let me know if this code will work for you.

Alexr03 commented 6 months ago

Ah perfect, yes the code above works, I had tried something similar but I was using ResultAs...() instead of PointerValue...() 😭. I did try with a named native before, and just tried again to make sure I wasn't going mad, the named native doesn't seem to work, its nil and doesn't resolve to a function. RedM_b1491_GTAProcess_f2bc0e49-7e78-4dbb-98a4-1054189e724a

Sojobo commented 6 months ago

I seem to be getting the following hash returned as the material everywhere: -1775485061. I'm trying to get the ground material and I've messed around with the coords and using offsets and other shapetests, the method I use, which works on FiveM is just returning that same hash everywhere in RedM. Does the native work differently here? Is there a different method of grabbing the ground material that I should be using?

Disquse commented 6 months ago

@Alexr03 The declaration for this native is currently only available on Canary.

@Sojobo if your code works in FiveM, it doesn't mean it's going to work on RedM without any changes. This is a perfect example of that. -1775485061 is a hash of the "DEFAULT" material name, but Red Dead Redemption 2 now expects material names to have a suffix containing their internal index. For example, "DEFAULT" becomes "DEFAULT_0", "PROP_ROCK" becomes "PROP_ROCK_207", and so on.

I'm attaching a small script for you guys to test it out: https://pastebin.com/eqJk0Zhp RedM_b1491_GTAProcess_sIuQUmkYHU

Sojobo commented 6 months ago

Thanks for the example code there @Disquse - I've tested it out and it actually showcases my issue pretty well, it looks like most of the world is actually just default material, the solution for my use-case is probably just checking to make sure that it is default and nothing else. I'm trying to do a farming script and I don't want players putting crops on roofs or other places that don't make sense. Looks like roads and buildings all have something other than default while fields are all default though so yeah, checking for default probably works for me, thanks again!