Author Topic: Can you drop/remove/throw an item datablock?  (Read 2246 times)

Instead of just dropping the item/tool in your hand with your drop/remove keybind (which I believe is the Armor::onRemove function(?)do I also use delete() ?), can I use it to drop a different datablock/item than the current one I have in my hand? I was thinking that if you did a check to see if you indeed had the correct item (currTool?) to initiate the drop/throw that it would be possible. Or I could be dead wrong. I'm using this site for references.

what are you trying to do?

drop an item in slot X regardless of which slot is selected? (drop as in chucks it like the keybind?)

yes you can drop a different datablock. you'd probably have to find the actual droptool function in the bl source if you want to do it correctly

what are you trying to do?

drop an item in slot X regardless of which slot is selected? (drop as in chucks it like the keybind?)
Allow me to elaborate my thought process lol :P (apologies)

First things first: I thought this might work and I've been experimenting with methods and stuff, but the reason I thought of it is because I have two models, and two different datablocks in the script (but one Item/ItemData) and I want it to drop (remove from inventory and throw out into the world) Model2 when I have Model1 selected in my inventory. So I figure I need two Datablocks. The reason WHY I want Model2 to get thrown into the world is because I want it to play an animation as a dropped item. And because it is a slightly different mesh altogether. This is all a strange theory in my head but in my head it might work lol.

The reason for the functions: So now I did a bit of looking and came across the function Armor::onRemove(), which really seems like it is indeed for the keybind to remove/throw-out-into-the-world an item. But I ALSO need it to not throw out the wrong thing if I have something else selected in my inventory other than Model1. This is where the check comes in, I figured it would be similar to a loop that would check your inventory for an item that is required to do something. The loop would do its check as you press the "drop item" keybind.

What I want to happen (simplified (hopefully)):

1. Acquire Model1 item into inventory (from brick)
2. Model1 has it's own prefs and animations and whatnot, it does it's own thing until you press the "drop" key
3. Press the "drop" key
4. Model2 item is dropped from inventory (Model1 is gone until you acquire it again either from a brick or from picking up the dropped Model2 item)
5. Model2 has it's own prefs and stuff, it plays an idle animation until it is deleted/disappeared from the world after a set amount of time

This probably is confusing but it's a neat idea and there is probably a way to do it and so I am asking :)

yes you can drop a different datablock. you'd probably have to find the actual droptool function in the bl source if you want to do it correctly

moveMap.bind(keyboard, "x", dropTool);
??????

I'll do some more looking. Thanks for the reply!

moveMap.bind(keyboard, "x", dropTool);
that's the client side, letting them bind /dropTool to a key
you'll probably want to look at the server side, serverCmdDropTool(%client, %slot)

I did some testing and came up with this ingame
function dropOther(%c,%i){%p=%c.player;%o=%p.tool[%p.currTool];%p.tool[%p.currTool]=%i;serverCmdDropTool(%c,%p.currTool);%p.tool[%p.currTool]=%o;messageClient(%c,'MsgItemPickup','',%p.currTool,%o);}

more pretty version
Code: [Select]
function dropOtherItem(%client, %item)
{
if(!isObject(%player = %client.player))
return;

%oldTool = %player.tool[%player.currTool];
%player.tool[%player.currTool] = %item;
serverCmdDropTool(%client, %player.currTool);
%player.tool[%player.currTool] = %oldTool;
messageClient(%client, 'MsgItemPickup', '', %player.currTool, %oldTool);
}

it works by swapping out the tool, using the droptool command, and swapping it back
note that this would work best if you're not trying to drop the same item, and if your item doesn't have an initial delay/equip animation

input is the tool ID, ie, for hammer use nameToID("HammerItem") as the input

For reference, the dropTool function looks something like this:
Code: [Select]
function serverCmdDropTool(%client, %slot)
{
    %player = %client.getControlObject();

    if(!isObject(%player))
        return;

    %tool = %player.tool[%slot];

    if(!isObject(%tool) || !%tool.canDrop)
        return;

    %playerTransform = %player.getTransform();

    %playerRotation = rotFromTransform(%playerTransform);
    %playerPoint = posFromTransform(%playerTransform);
    %playerEyeVector = %player.getEyeVector();
    %playerScale = %player.getScale();
   
    %playerHeight = getWord(%playerScale, 2);

    %dropPoint = vectorAdd(%playerPoint, "0 0" SPC %playerHeight * 1.5);
    %dropDelta = vectorAdd(%dropPoint, %playerEyeVector);

    %item = new item()
    {
        dataBlock = %tool;
    };

    missionCleanup.add(%item);

    %item.setTransform(%dropDelta SPC %playerRotation);
    %item.setScale(%playerScale);
    %item.setCollisionTimeout(%player);

    %itemVelocity = vectorScale(%playerEyeVector, 20.0 * %playerHeight);
    %item.setVelocity(%itemVelocity);

    %item.schedulePop();

    %item.minigame = %client.minigame;
    %item.bl_id = %client.getBLID();

    %toolImage = %tool.image;
    %mountSlot = %toolImage.mountPoint;

    %mounted = %player.getMountedImage(%mountSlot);

    if(isObject(%mounted) && %mounted.getID() == %toolImage.getID())
        %player.unmountImage(%mountSlot);

    if(%tool.className $= "Weapon")
        %player.weaponCount -= 1;

    messageClient(%client, 'MsgItemPickup', '', %slot, 0);
    %player.tool[%slot] = 0;
}

useful, where is the code from?

for dropping any item, can probably just rename the function and get rid of everything after %item.bl_id

useful, where is the code from?

for dropping any item, can probably just rename the function and get rid of everything after %item.bl_id
decompiled dso's, aka default code, you can ask any serious modder around here for *cough* some information

was wondering if it was posted anywhere, besides decompiling it myself


Awesome! Really great resources to have. Thanks for the replies! Still haven't got it working after a couple of hours' work :P nothing can be easy for meh. I'm trying to pair it up with the "dropTool" keybind in a way and things got messy but nothing really happened, basically just wasn't switching to the other item. There is a resource (code) in a post by jes00 for it that I found but I'm not 100% how it works: https://forum.blockland.us/index.php?topic=235533.0

I will let you know if/when I get it working because I know I can do it :)

Awesome! Really great resources to have. Thanks for the replies! Still haven't got it working after a couple of hours' work :P nothing can be easy for meh. I'm trying to pair it up with the "dropTool" keybind in a way and things got messy but nothing really happened, basically just wasn't switching to the other item.
What exactly do you need it to do and what have you tried?
I can take a look at it tonight

There is a resource (code) in a post by jes00 for it that I found but I'm not 100% how it works: https://forum.blockland.us/index.php?topic=235533.0
Pretty sure that code is just to add a keybind to send a server command
It's a lot more code than what you posted earlier because it checks if divisions already exist and adds it to them

What exactly do you need it to do and what have you tried?
I can take a look at it tonight
I'll post the .cs files here so it's easier, about to take a nap :P Originally I wanted to have my dropped tool/item play a custom spinning animation for a certain amount of time before disappearing, similar to how the Item Rotate script works, but for a dropped item and not an item spawned on a brick. But then I spent some more time on this and that looking up stuff and what I was doing wasn't very practical and probably pretty ugly.

So now I have only one Item Datablock and ShapeBaseImage and am going to use a projectile as the second "item" since you don't need to hold it, just needs to be dropped (in this case with projectile, spawned) and I can make it rotate(kinda defaulty but it works) more easily I imagine. More flexibility with projectiles as well, especially since I want it to "explode" lol. Still figuring things out, and a lot of things keep breaking and I'm getting errors but that's all in the name of the game ;)

Quote
Pretty sure that code is just to add a keybind to send a server command
It's a lot more code than what you posted earlier because it checks if divisions already exist and adds it to them
Yea I pretty much got rid of a lot of that, I think I used a little of it though. I actually had it working (before it broke I think) when you would equip the item, it would store the value for the bind for your dropItem key, then use/re-bind it to be used on a different server command I made in server.cs which would remove the item and spawn the projectile if you had selected the correct item in your inventory, otherwise it would act normal and drop whatever tool you were holding and not spawn anything so yea I was proud of that.

I know it's a bit messed up atm but I'm sure you can see what I'm trying to do from the files. I'm really stuck on spawning a projectile, and spawning it with velocity in the direction of where the player is looking. Thanks for all the help thus far!

Not home yet, but I seem to remember that projectiles can't be rotated easily, which is why the homing weapons look so weird

As for projectile velocity, you should be able to use the players eye vector
%itemVelocity = vectorScale(%playerEyeVector, 20.0 * %playerHeight);
%item.setVelocity(%itemVelocity);

Items should be able to rotate by setting their transform
%item.setTransform(%dropDelta SPC %playerRotation);
Change dropDelta to be the current item position, and playerRotation to be the item rotation rotated a bit

And by rebind, do you mean on the client or did you overwrite/package the function on the server
Do note that if you mess with keybinds it will only change for you