Blockland Forums > Modification Help
Getting the object created from a default/different function
phflack:
Instead of doing the if checks, why not just keep track of the last added?
--- Code: ---function onAddStuff(blub)
{
return ($lastAdded = parent::onAddStuff(blub));
}
--- End code ---
(On mobile right now, will fix when I get back)
Conan:
--- Quote from: phflack on October 26, 2017, 12:41:21 PM ---Instead of doing the if checks, why not just keep track of the last added?
--- Code: ---function onAddStuff(blub)
{
return ($lastAdded = parent::onAddStuff(blub));
}
--- End code ---
(On mobile right now, will fix when I get back)
--- End quote ---
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
phflack:
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
Swollow:
--- Quote from: Kyuande on October 26, 2017, 10:00:19 AM ---You parented the itemdata vars incorrectly, just an info for everyone planning to copy that code without thought
--- End quote ---
--- Quote from: Shift Kitty on October 26, 2017, 11:04:46 AM ---0/10
--- End quote ---
thanks for the corrections guys, fixed, I should have paid more attention
--- Quote from: phflack on October 26, 2017, 12:41:21 PM ---Instead of doing the if checks, why not just keep track of the last added?
--- Code: ---function onAddStuff(blub)
{
return ($lastAdded = parent::onAddStuff(blub));
}
--- End code ---
(On mobile right now, will fix when I get back)
--- End quote ---
this works fine too
something like
--- Code: ---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);
--- End code ---
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
Shift Kitty:
Yeah, not good to return it, nor expect it to return a value.