News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: Move any Object  (Read 1883 times)

0 Members and 1 Guest are viewing this topic.

Offline Ming

  • Full Member
  • ***
  • Posts: 154
  • Gender: Male
  • 抽刀斷水水更流,舉杯消愁愁更愁
    • View Profile
Move any Object
« on: April 25, 2022, 09:25:18 PM »
Code: [Select]
MoveObj = function()
 
  while true do
   
    if IsButtonBeingPressed(2, 0) then
     
      Wait(100)
     
      Heading = PedGetHeading(gPlayer)
      Rotation = math.deg(Heading)
      DeltaX = -math.sin(Heading)
      DeltaY = math.cos(Heading)
      gPX, gPY, gPZ = PedGetPosXYZ(gPlayer)
     
      FrontObjects = {ObjectFindInArea(gPX + DeltaX, gPY + DeltaY, gPZ + 0.3, 0.3)}
      FrontObject = FrontObjects[1]
     
      ObjectBreak(FrontObject)
     
      PEIndex, PE = CreatePersistentEntity(FrontObject, HX + DeltaX, HY + DeltaY, HZ - 0.5, Rotation, AreaGetVisible())
     
      repeat
       
        DeletePersistentEntity(PEIndex, PE)
       
       
        Heading = PedGetHeading(gPlayer)
        Rotation = math.deg(Heading)
        DeltaX = -math.sin(Heading)
        DeltaY = math.cos(Heading)
        gPX, gPY, gPZ = PedGetPosXYZ(gPlayer)
       
        PEIndex, PE = CreatePersistentEntity(FrontObject, gPX + DeltaX, gPY + DeltaY, gPZ + 0.3, Rotation, AreaGetVisible())
       
        if IsButtonBeingPressed(2, 0) then
          break
        end
       
        Wait(0)
      until IsButtonBeingPressed(2, 0)
     
    end
   
    Wait(0)
  end
 
end
When I press aim button, game crash

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Re: Move any Object
« Reply #1 on: April 29, 2022, 08:09:01 AM »
Ah.. I see what you're experimenting with.
It's just.. a bit rushed though :hmm:

Why don't you start with ObjectFindInArea function first.
I can clearly see the function is not supposed to be like that lol.

ObjectFindInArea is a function like PedFindInAreaXYZ and VehicleFindInAreaXYZ, the function argument is pretty much similar except that ObjectFindInArea cannot be used for a long distance. You can't put ObjectFindInArea(X, Y, Z, 999999) or your game will be crashed instantly. Approximately, the maximum distance is about 7-10.

Also, the first returned array from ObjectFindInArea is a boolean. It checks if there's any object around us, true if there's any, and false if there isn't. Therefore, you can't use ObjectBreak function to the first array because it isn't object id. If the first array is true, then the rest of them are object id and can be used for ObjectBreak. If it's false, there will be no second array.

Example of use:
Code: [Select]
if IsButtonBeingPressed(3, 0) then
  local X, Y, Z = PlayerGetPosXYZ()
  local OBJ = {ObjectFindInArea(X, Y, Z, 2)}
 
  -- check if there's any object around us
  if OBJ[1] then
    for ID = 2, table.getn(OBJ) do
      ObjectBreak(OBJ[ID])
    end
   
    local COUNT = table.getn(OBJ) - 1
    TextPrintString(COUNT..' object'..(COUNT > 1 and 's' or '')..' removed!', 3, 1)
  else
    TextPrintString('There is no object around!', 3, 1)
  end
end
« Last Edit: April 29, 2022, 12:56:00 PM by Altamurenza »

Offline Ming

  • Full Member
  • ***
  • Posts: 154
  • Gender: Male
  • 抽刀斷水水更流,舉杯消愁愁更愁
    • View Profile
Re: Move any Object
« Reply #2 on: April 30, 2022, 02:10:32 AM »
Got it!
Code: [Select]
FrontObjects = {ObjectFindInArea(X, Y, Z, 1)}
      if FrontObjects[1] then
        FrontObject = FrontObjects[2]
                ObjectBreak(FrontObject)
        PEIndex, PE = CreatePersistentEntity(FrontObject, X, Y, Z, Rotation, AreaGetVisible())
Can I do this? Or Object ID not work in CreatePersistentEntity
« Last Edit: April 30, 2022, 02:15:57 AM by Ming »

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Re: Move any Object
« Reply #3 on: April 30, 2022, 02:32:04 PM »
Or Object ID not work in CreatePersistentEntity

Maybe you forgot how to use CreatePersistentEntity, perhaps.

ObjectFindInArea function will return object id (number) if there is an object in range, whereas the CreatePersistentEntity function require object name (string). So.. you can't use the object id to replace object name position in CreatePersistentEntity as its first function argument.

Also, you can move your cursor to the spoiler if you are open to little criticism.
You don't have to if you are not open to that, no worries at all ;).

In logical term, you skipped the key point that your premise cannot be drawn to the right conclusions. I actually realized it in the first place by saying that your experiment was a bit rushed and told you to start with ObjectFindInArea first in hoping that you found the key difference in your experiment. For the next experiment, i suggest you to do it gradually and systematically in order to reduce the possibility of fallacies.

You did great, keep that up.

Offline Ming

  • Full Member
  • ***
  • Posts: 154
  • Gender: Male
  • 抽刀斷水水更流,舉杯消愁愁更愁
    • View Profile
Re: Move any Object
« Reply #4 on: May 02, 2022, 03:00:51 AM »
You are right, I am really in a hurry to do mods, thanks for the advice, you are the best modder :D