picking up a thrown item with a dynamic field... not retaining the value
by Grey9999 · in Torque 3D Professional · 03/09/2013 (11:45 am) · 13 replies
Ok I have an item (a Glock Magazine) that has it's own onthrow function, and towards the end where the
item is actually created I have a dynamic field defined called magrounds.
%obj = new Item()
{
datablock = GlockMag;
rotation = "0 0 1 "@ (getRandom() * 360);
rotate = true;
count = %amount;
magrounds = %ammotopass;
};
When I pick this item back up, near the beginning of the OnPickup function I have
echo (%this.magrounds); which comes back empty. Why does this not work? I can echo magrounds in the OnThrow function and it will echo the proper value but when it is picked up it seems it can not get it. The only way I have gotten it to work is if I have it actually assign the dynamic field to the datablock instead but of course it changes all glock mags existing in the level if I do that. Is it because OnPickup does not get the actual items information it just gets everything from the datablock??
item is actually created I have a dynamic field defined called magrounds.
%obj = new Item()
{
datablock = GlockMag;
rotation = "0 0 1 "@ (getRandom() * 360);
rotate = true;
count = %amount;
magrounds = %ammotopass;
};
When I pick this item back up, near the beginning of the OnPickup function I have
echo (%this.magrounds); which comes back empty. Why does this not work? I can echo magrounds in the OnThrow function and it will echo the proper value but when it is picked up it seems it can not get it. The only way I have gotten it to work is if I have it actually assign the dynamic field to the datablock instead but of course it changes all glock mags existing in the level if I do that. Is it because OnPickup does not get the actual items information it just gets everything from the datablock??
About the author
#2
03/09/2013 (11:01 pm)
surefunction GlockMagazine::onPickup(%this, %obj, %shape, %amount)
{
if (Parent::onPickup(%this, %obj, %shape, %amount))
serverPlay3D(GlockDrawSound, %shape.getTransform());
echo (%this.magrounds);
%this.magrounds = %ammotopass;
if ((%shape.getInventory(GlockAmmo1))==0)
{
%shape.setInventory(GlockAmmo1, %ammotopass);
echo ("Glock mag 1 has ",%shape.getInventory(GlockAmmo1));
echo ("Glock mag 2 has ",%shape.getInventory(GlockAmmo2));
return;
}
else
{
if ((%shape.getInventory(GlockAmmo2))==0)
{
%shape.setInventory(GlockAmmo2, %ammotopass);
echo ("Glock mag 1 has ",%shape.getInventory(GlockAmmo1));
echo ("Glock mag 2 has ",%shape.getInventory(GlockAmmo2));
return;
}
}
}
function GlockMagazine::onThrow(%this, %user, %amount)
{
// Remove the object from the inventory
if (%amount $= "")
%amount = 1;
if (%this.maxInventory !$= "")
if (%amount > %this.maxInventory)
%amount = %this.maxInventory;
if (!%amount)
return 0;
// Logic That selects the lesser value "magazine" to throw, or if there is only one mag throws that one.
//GlockAmmo1 and GlockAmmo2 are the items that hold the notional ammo in the players spare "magazines" in the inventory
//GlockMag itself is more of a helper item.
if ((%user.getInventory(GlockAmmo2)) <= (%user.getInventory(GlockAmmo1)) && (%user.getInventory(GlockAmmo2)) || (!%user.getInventory(GlockAmmo1)) && (%user.getInventory(GlockAmmo2)))
{
%ammotopass = %user.getInventory(GlockAmmo2);
%user.setInventory(GlockAmmo2, 0);
echo ("Ammo to pass:",%ammotopass);
echo ("Throwing Glockmag 2!");
}
else
{
if ((%user.getInventory(GlockAmmo1)) <= (%user.getInventory(GlockAmmo2)) && (%user.getInventory(GlockAmmo1)) || (!%user.getInventory(GlockAmmo2)) && (%user.getInventory(GlockAmmo1)))
{
%ammotopass = %user.getInventory(GlockAmmo1);
%user.setInventory(GlockAmmo1, 0);
echo ("Ammo to pass:",%ammotopass);
echo ("Throwing Glockmag 1!");
}
else
{
return 0;
}
}
// Construct the actual object in the world, and add it to
// the mission group so it's cleaned up when the mission is
// done. The object is given a random z rotation.
%user.decInventory(%this,%amount);
%obj = new Item()
{
datablock = GlockMag;
rotation = "0 0 1 "@ (getRandom() * 360);
rotate = true;
count = %amount;
canSaveDynamicFields = 1;
magrounds = %ammotopass;
};
echo(%obj.magrounds);
MissionGroup.add(%obj);
//%obj.schedulePop();
return %obj;
}
#3
The onPickup() method is called against the datablock, so the %this parameter in your version is the datablock itself. The Item object should be the %obj parameter. You can see how the onPickup() method is set up and called in scripts/server/inventory.cs within ShapeBase::pickup().
I hope that helps!
- Dave
03/11/2013 (8:09 am)
@Grey9999:The onPickup() method is called against the datablock, so the %this parameter in your version is the datablock itself. The Item object should be the %obj parameter. You can see how the onPickup() method is set up and called in scripts/server/inventory.cs within ShapeBase::pickup().
I hope that helps!
- Dave
#4
Changed %this.magrounds to %obj.magrounds and it still does not work though.
On another note it doesn't help that I had %obj.magrounds = %ammotopass either it should be %ammotopass = %obj.magrounds in the onpickup function....hah.
03/11/2013 (9:37 am)
Thanks for the reply.Changed %this.magrounds to %obj.magrounds and it still does not work though.
On another note it doesn't help that I had %obj.magrounds = %ammotopass either it should be %ammotopass = %obj.magrounds in the onpickup function....hah.
#5
So perhaps you should do this instead:
03/11/2013 (10:02 am)
I just noticed that your scope name is GlockMagazine; You are assigning the item created a datablock of GlockMag here;%obj = new Item()
{
datablock = GlockMag;
rotation = "0 0 1 "@ (getRandom() * 360);
rotate = true;
count = %amount;
canSaveDynamicFields = 1;
magrounds = %ammotopass;
}; So perhaps you should do this instead:
%obj = new Item()
{
datablock = %this;
rotation = "0 0 1 "@ (getRandom() * 360);
rotate = true;
count = %amount;
canSaveDynamicFields = 1;
magrounds = %ammotopass;
};
#6
03/11/2013 (11:11 am)
Itemdata (GlockMag) has a classname of "GlockMagazine" so it still calls the right onpickup function, thanks though.
#7
03/11/2013 (11:26 am)
You're sure? Because it's called on the datablock and not the object class. So if GlockMag::onPickup() and GlockMag::onThrow() don't exist, then it's not keeping the data. Just saying; do it however you like - what do I know?
#8
However, I changed all the methods names GlockMag instead of GlockMagazine like you said anyway just to try it and still the value for %obj.magrounds comes up blank when I echo it with GlockMag::onPickup
03/11/2013 (11:31 am)
yeah it was definitely calling it. However, I changed all the methods names GlockMag instead of GlockMagazine like you said anyway just to try it and still the value for %obj.magrounds comes up blank when I echo it with GlockMag::onPickup
#9
Of course it does.
This might echo out your number.
Just out of curiosity, what have you got ShapeBaseData::onPickup() doing? By default it returns false....
And I prefer something like
03/11/2013 (11:49 am)
echo (%this.magrounds);
Of course it does.
echo (%obj.magrounds);
This might echo out your number.
Just out of curiosity, what have you got ShapeBaseData::onPickup() doing? By default it returns false....
And I prefer something like
echo(" @@@ number of rounds: " @ %obj.magrounds);because I can be certain my echo is being hit and it's easy to find the @@@ in the log - but that's just personal preference.
#10
03/11/2013 (11:52 am)
No it still is not getting the value.
#11
I haven't messed with ShapeBaseData::onPickup(), I'm not sure I understand your question. I am new to scripting.
03/11/2013 (11:56 am)
Yeah I have it echoing this way so I know it's getting hit:echo ("Magrounds ", %obj.magrounds);I haven't messed with ShapeBaseData::onPickup(), I'm not sure I understand your question. I am new to scripting.
#12
03/11/2013 (1:06 pm)
Ok, got it. Parent::onPickup() deletes the object, so it has been destroyed by the time you try to use it. Move that to the end of your method, like so:function GlockMagazine::onPickup(%this, %obj, %shape, %amount)
{
echo (%this.magrounds);
%this.magrounds = %ammotopass;
if ((%shape.getInventory(GlockAmmo1))==0)
{
%shape.setInventory(GlockAmmo1, %ammotopass);
echo ("Glock mag 1 has ",%shape.getInventory(GlockAmmo1));
echo ("Glock mag 2 has ",%shape.getInventory(GlockAmmo2));
return;
}
else
{
if ((%shape.getInventory(GlockAmmo2))==0)
{
%shape.setInventory(GlockAmmo2, %ammotopass);
echo ("Glock mag 1 has ",%shape.getInventory(GlockAmmo1));
echo ("Glock mag 2 has ",%shape.getInventory(GlockAmmo2));
return;
}
}
if (Parent::onPickup(%this, %obj, %shape, %amount))
serverPlay3D(GlockDrawSound, %shape.getTransform());
}
#13
03/11/2013 (10:20 pm)
Works! Thanks a lot I would have never figured that out. My reloading system for my game keeps track of all your magazines after reloading so this is awesome that you can now drop them for your teammates who need ammo or for yourself to swap for a mag with more rounds in it.
Employee Michael Perry
ZombieShortbus