[Preview]habi's Pickup Binder

Started by habi, Jul 31, 2023, 06:14 PM

Previous topic - Next topic

habi




What is so special in this?
The code !
function onPlayerCommand(player, cmd, text)
{
    if(cmd=="heal")
    {
        local pickup = CreatePickup(366, player.Pos+Vector(3,3,0));
        player.BindPickup(pickup, function(){
            this.Health=100;//'this' will be player
            pickup.Remove();//because local variable created before function is accessible to the function
            ::ClientMessage("You have been healed!", this, 255, 255, 0);// :: is very important,
            //because the 'this' is instance and not roottable.
        });
        ClientMessage("Health pickup created near you!", player, 255, 0, 255);
    }
}

NB: Only that player who typed /heal will be able to pick_it_up
Download links: attached, and fixed some bugs.(01.08.2023)

vitovc

Good luck. I only wanted to note that due lag player can send ID of pickup that already removed and created for another purpose. Its rare bug and many people dont know about. So to be make sure that its correct pickup better to check coords as addotional to ID (or model - it depends of script mechanics)
...::: vice city :::...
Useful things for vcmp: Relative position and finding an angle (in 3d), 3d line (like laser)

habi

#2
I tried to reproduce the bug you mentioned. VCMP Server seems to handle this issue.

Not only pickup id, another (3 byte number) is send along with pickup id.

This number is incremented whenever pickup is removed and another one is created.
client->server
b2 00 00 50 00 the b2 is identifier for event, last 00 is pickup id
So this number (hex: 00 00 50) tell server which pickup.

When pickup is removed and created again, the new pickup will have this number
00 00 58, the next one has (00 00 60), the next one (00 00 68) and so on.

I had stopped packets from server to client, removed pickup from server and created another one same id. Since stopped, pickup was not removed for player. I went near and picked that pickup, resumed connection, but onpickup event was not called. Why? this number was changed.

PS: Uploaded the implementation in first post

vitovc

#3
maybe its fixed in newer versions of vcmp but it was before
Thanks for testing!
...::: vice city :::...
Useful things for vcmp: Relative position and finding an angle (in 3d), 3d line (like laser)

habi

#4
Documentation
  • UninstallBinder()
    Restores everything in script back to its original state which 1. removing delegate of roottable, 2. Moving onPickupPickedUp and onPlayerPart functions back to roottable 3. Deleting tables such as __eventTable, __PlayerMatchPickupTable, etc which were used internally, 4. Removing the method 'BindPickup' from CPlayer class, 5. Replacing CPickup.Remove function to its original one
  • player.BindPickup( pickup, callback )
    pickup :- an instance of pickup
    Properties of Callback function
    Quote from: habi on Jul 31, 2023, 06:14 PMfunction onPlayerCommand(player, cmd, text)
    {
        if(cmd=="heal")
        {
            local pickup = CreatePickup(366, player.Pos+Vector(3,3,0));
            player.BindPickup(pickup, function(){
                this.Health=100;//'this' will be player
                pickup.Remove();//because local variable created before function is accessible to the function
                ::ClientMessage("You have been healed!", this, 255, 255, 0);// :: is very important,
                //because the 'this' is instance and not roottable.
            });
            ClientMessage("Health pickup created near you!", player, 255, 0, 255);
        }
    }
    • The function must be zero-parameterised
    • The 'this' of function when called will be player instance and not roottable.
    • The onPickupPickedUp event will not be called if this callback does not return anything ( or return null or 0 ). Otherwise, after this callback, onPickupPickedUp event will be called.
    • Due to 2, global variables must be accessed through :: inside the function. So instead of print, MessagePlayer, etc, ::print, ::MessagePlayer, .. must be used. The same is true for ordinary global variables - instead of a, b , ::a, ::b must be used.
    • The pickup variable inside callback cannot be accessed, unless it was a local variable at the time of callback creation as in the example provided.
    • Caution against removing pickup and return 1: If you remove the pickup and return a non-zero value, then onPickupPickedUp will be called with an invalid pickup instance.

PS: There was a small bug onPlayerPart in yesterdays release, and is fixed.

Nihongo^

Isn't players can use it to prevent evading like before the duel they can type /heal, and after losing some hp, take it.