Skip to content

Commit

Permalink
Add custom pickup support! createPickup + setElementData
Browse files Browse the repository at this point in the history
Thanks @httpRick for the idea
  • Loading branch information
Fernando-A-Rocha committed Jan 16, 2023
1 parent f83c58f commit f0b4a81
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
1 change: 1 addition & 0 deletions [examples]/unittest_newmodels/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local temp_datas = {
player = exports.newmodels:getDataNameFromType("player"),
ped = exports.newmodels:getDataNameFromType("ped"),
object = exports.newmodels:getDataNameFromType("object"),
pickup = exports.newmodels:getDataNameFromType("pickup"),
}

addEventHandler( "onClientRender", root,
Expand Down
15 changes: 15 additions & 0 deletions [examples]/unittest_newmodels/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function (startedResource)
-- test5()
-- test6()
-- test7()
-- test8()

end)

Expand Down Expand Up @@ -191,6 +192,20 @@ function test7()
end
end

-- Custom pickups
function test8()
local thePlayer = getRandomPlayer()
if not thePlayer then return end
local samp = getResourceFromName("sampobj_reloaded")
if not (samp and getResourceState(samp)=="running") then
return ptin("test8: sampobj_reloaded is not running")
end
local customID = 18749 -- SAMPLogoSmall
local pickup = createPickup(0,0,3, 3, 1239)
addEventHandler("onPickupHit", pickup, function() cancelEvent() end)
setElementData(pickup, "objectID", customID)
print("Created custom pickup with ID "..customID)
end


-- Vehicle testing
Expand Down
7 changes: 6 additions & 1 deletion newmodels/_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@

-- saves custom element model ID in its element data under the following names
dataNames = {
ped = "skinID", player = "skinID", -- must be the same
ped = "skinID",
vehicle = "vehicleID",
object = "objectID",
}

dataNames.player = dataNames.ped -- must be the same

-- support pickups with custom object models
dataNames.pickup = dataNames.object -- must be the same

-- saves the element's custom model's base model ID
-- useful for getting a vehicle's base model to fetch its original handling, etc
baseDataName = "baseID"
Expand Down
41 changes: 25 additions & 16 deletions newmodels/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function allocateNewMod(element, elementType, id)
-- as type so we need to change that to 'ped'
local elementType2 = elementType
if elementType2 == "player" then elementType2 = "ped" end
if elementType2 == "pickup" then elementType2 = "object" end

local paths = foundMod.paths
if type(paths) ~= "table" then
Expand Down Expand Up @@ -390,26 +391,32 @@ function setElementCustomModel(element, elementType, id, noRefresh)
end
end

-- refresh model so change can actually have an effect
local currModel = getElementModel(element)
if (currModel == allocated_id) and not (noRefresh) then
if getElementType(element) == "pickup" then
setPickupType(element, 3, allocated_id)
print("set pickup type to 3, "..allocated_id)
else

-- some logic to refresh model
local diffModel = 9--ped
if elementType == "vehicle" then
diffModel = 400
elseif elementType == "object" then
diffModel = 1337
end
if currModel == diffModel then
diffModel = diffModel + 1
end
-- refresh model so change can actually have an effect
local currModel = getElementModel(element)
if (currModel == allocated_id) and not (noRefresh) then

if setElementModel(element, diffModel) then
-- some logic to refresh model
local diffModel = 9--ped
if elementType == "vehicle" then
diffModel = 400
elseif elementType == "object" then
diffModel = 1337
end
if currModel == diffModel then
diffModel = diffModel + 1
end

if setElementModel(element, diffModel) then
setElementModel(element, allocated_id)
end
else
setElementModel(element, allocated_id)
end
else
setElementModel(element, allocated_id)
end

if getElementType(element)=="vehicle" then
Expand Down Expand Up @@ -508,6 +515,8 @@ function updateElementOnDataChange(source, theKey, oldValue, newValue)
local et = getElementType(source)
if et == "player" and data_et == "ped" then data_et = "player" end
if et == "ped" and data_et == "player" then data_et = "ped" end
if et == "pickup" and data_et == "object" then data_et = "pickup" end
if et == "object" and data_et == "pickup" then data_et = "object" end

if data_et ~= et then return end

Expand Down
9 changes: 8 additions & 1 deletion newmodels/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ end
function fixModList()
-- because ped mods can be applied on players too
modList.player = modList.ped
modList.pickup = modList.object
return true
end

Expand Down Expand Up @@ -293,7 +294,10 @@ function doModListChecks()

for elementType, name in pairs(dataNames) do

if elementType ~= "player" then -- exception
if not (
elementType == "player"
or elementType == "pickup"
) then -- exceptions
local mods1 = modList[elementType]
if not mods1 then
return modCheckError("Missing from modList: "..elementType.." = {},")
Expand All @@ -308,6 +312,9 @@ function doModListChecks()
if elementType == "player" then
return modCheckError("Please remove mod from modList: player = {...}, it will be added automatically to match 'ped' mods")
end
if elementType == "pickup" then
return modCheckError("Please remove mod from modList: pickup = {...}, it will be added automatically to match 'object' mods")
end

-- for _,mod in ipairs(mods) do
Async:foreach(mods, function(mod)
Expand Down
5 changes: 0 additions & 5 deletions newmodels/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ function verifySetModelArguments(element, elementType, id)
return false, reason
end

local dataName = dataNames[et]
if not dataName then
return false, et.." mods yet supported"
end

if not tonumber(id) then
return false, "Non-number ID passed"
end
Expand Down

0 comments on commit f0b4a81

Please sign in to comment.