Getting the object created from a default/different function

Author Topic: Getting the object created from a default/different function  (Read 3711 times)

For instance, if my script calls servercmdDropTool, that function creates an item in front of the player. How can i get that items object ID when the function gets called?
« Last Edit: October 25, 2017, 02:14:20 PM by Trogtor »

call .getid(); when so said item spawns

theres nothing to use the getID function on. im not asking how to get an object ID. im asking how to find the object that was just spawned.

Unless the function returns the object it creates, you can't magically get it.
For example:
function makeObject()
{
    %obj = new Item();
    return %obj;
}
talk(makeObject());

Would print the new object's ID. If you don't return the new object nothing will be echoed.

If you're looking to get an item's ID from serverCmdDropTool you might have to loop through missionCleanup to look for items and find the one that was most recently spawned/belonged to your client.


call .getid(); when so said item spawns
If you're referring to /getID, that doesn't work on item objects.

Unfortunately, most if not all serverCmds aren't designed to return any value, nor should they be. The problem is that serverCmdDropTool instead contains all the code for dropping a tool, rather than calling a function that would drop the tool and return the object id. If it were like that, then we'd be able to easily just call that function instead and get an object from it to call additional functions on it.

You need to rewrite the entire function if you want to do anything else with it.

If you're looking to get an item's ID from serverCmdDropTool you might have to loop through missionCleanup to look for items and find the one that was most recently spawned/belonged to your client.
Never do that. Like, ever.

::onAdd exists.

package droptool:

turn on a global var
call parent
turn off global var


package itemdata on add:

if global var
talk %obj
return parent


Code: [Select]
package captureDroppedItem
{
     function serverCmdDropTool(%cl,%slot)
     {
          $dropItemCapture ="";
          parent::serverCmdDropTool(%cl,%slot);
          %item = $dropItemCapture;
          %item.setVelocity("0 0 100");
     }
     function itemData::onAdd(%db,%item)
     {
          if(!$dropItemCapture)
               $dropItemCapture= %item;
          parent::onAdd(%db,%item);
     }
};
activatePackage(captureDroppedItem);

Edit: Woops thanks for the correction guys I didn't test it
« Last Edit: October 26, 2017, 02:11:16 PM by Swollow »

You parented the itemdata vars incorrectly, just an info for everyone planning to copy that code without thought

Code: [Select]
package captureDroppedItem
{
     function serverCmdDropTool(%cl,%slot)
     {
          $dropItemCapture ="";
          parent::serverCmdDropTool(%cl,%slot);
          %item = $dropItemCapture;
          %item.setVelocity("0 0 100");
     }
     function itemData::onAdd(%db,%item)
     {
          if(!$dropItemCapture)
               $dropItemCapture= %item;
          parent::onAdd(%this,%obj);
     }
};
activatePackage(captureDroppedItem);
0/10
You parented the itemdata vars incorrectly, just an info for everyone planning to copy that code without thought

Instead of doing the if checks, why not just keep track of the last added?

Code: [Select]
function onAddStuff(blub)
{
return ($lastAdded = parent::onAddStuff(blub));
}

(On mobile right now, will fix when I get back)

Instead of doing the if checks, why not just keep track of the last added?

Code: [Select]
function onAddStuff(blub)
{
return ($lastAdded = parent::onAddStuff(blub));
}

(On mobile right now, will fix when I get back)
may not want to constantly pick up the last value, esp if you plan to do something more with it that might involve more item creation halfway through

for example, spawning an item, then calling a function that spawns a bunch of items from it, then giving a random item from the ones that spawned to a player.

theres probably no situation that explicitly requires you to keep the original object on the value, but its a lot easier to keep track of if you have a bunch of code going around creating items and you only want a specific one to be saved and used. esp if your server is open to the public/friends while developing

In either case you'd need to use a bit of code right after the item is created, either to set the flag or to put the object ID somewhere useful

You parented the itemdata vars incorrectly, just an info for everyone planning to copy that code without thought
0/10
thanks for the corrections guys, fixed, I should have paid more attention

Instead of doing the if checks, why not just keep track of the last added?

Code: [Select]
function onAddStuff(blub)
{
return ($lastAdded = parent::onAddStuff(blub));
}

(On mobile right now, will fix when I get back)
this works fine too

something like

Code: [Select]
package captureDroppedItem
{
     function serverCmdDropTool(%cl,%slot)
     {
          parent::serverCmdDropTool(%cl,%slot);
          %item = $dropItemCapture;
          %item.setVelocity("0 0 100");
     }
     function itemData::onAdd(%db,%item)
     {
          $dropItemCapture = %item;
          parent::onAdd(%db,%item);
     }
};
activatePackage(captureDroppedItem);

I wouldn't return the parent onAdd if I were you though, it could produce console errors


either way is essentially the same, the if checks are basically completely negligible and dropping them from the code will not result in really any performance increase since it's just a boolean check
« Last Edit: October 26, 2017, 02:16:07 PM by Swollow »

Yeah, not good to return it, nor expect it to return a value.