Pickup to object

From GTAMods Wiki
Jump to navigation Jump to search

Pickups itself are objects with some additional properties. But opcodes that work for objects do not accept pickup handles, so many interesting features are unavailable for the pickups, such as making it invisible. The following code converts a given pickup handle to an object one, thus allowing to use such opcodes with pickups.

Requirements

The Code

This code is made as an SCM function. It could be used both in main.scm or CLEO scripts. It accepts the one parameter (pickup handle) and returns converted object handle. Note that it is also made as a proper condition, so you can use the 0AB1 in a conditional statement to check if the conversion was failed (see Example below).

Paste the code somewhere in your script.

:Pickup2Object
// 0@ - input param (pickup handle)
// get pickup pointer
0085: 1@ = 0@ 
0A91: 3@ = 1@ / 65536
0A90: 4@ = 3@ * 65536
0062: 1@ -= 4@
1@ *= 32
1@ += 0x9788C0
// read index
0A8E: 4@ = 1@ + 0x1A
0A8D: 4@ = read_memory 4@ size 2 virtual_protect 0
if
    003B: 3@ == 4@
then    
    // obtain pickup's object pointer
    1@ += 4
    0A8D: 1@ = read_memory 1@ size 4 virtual_protect 0
    if
        1@ > 0
    then
        // convert object pointer to a handle
        0A8D: 8@ = read_memory 0xB7449C size 4 virtual_protect 0
        0AA8: call_function_method 0x465070 struct 8@ num_params 1 pop 0 1@ 1@
        if
            03CA:   object 1@ exists 
        then
            0485: return_true
            0AB2: ret 1 1@
        end
    end
end
059A: return_false
0AB2: ret 1 0@

Now you could call this function to obtain the object handle and use it (see Example).

Example

To test the code create a new pickup.

0213: 10@ = create_pickup #INFO type 3 at 2491.8301 -1681.1062 13.3362

This pickup will be placed near CJ's house on Groove Street.

Now call the function.

0AB1: call_scm_func @Pickup2Object 1 10@ 11@

10@ there is the created pickup. If you used another variable in opcode 0213, replace 10@ in 0AB1 with the needed variable name. The result (object handle) will be copied to 11@ (once again, you can use any other variable there).

After calling the function 11@ will contain either an object handle OR (if the function failed) the passed pickup's handle.

Now you can work with the pickup as an object using obtained handle.

0750: set_object 11@ visibility 0

The pickup will immediately become invisible.

Full test code

0213: 10@ = create_pickup #INFO type 3 at 2491.8301 -1681.1062 13.3362 
while true
    wait 250
    if 
        0AB0:  key_pressed 85 // U button
    then   
        if
            0AB1: call_scm_func @Pickup2Object 1 10@ 10@
        then 
            // the function succeed
            0750: set_object 11@ visibility 0
        end   
    end
end

The questions are welcome on the discussion page.

External link

GTA Net GTAForums: Mission coding