Author Topic: Swapping Inventories Problem  (Read 1205 times)

Okay so I've been stuck on this piece of code for a very long time and I can't figure out what's wrong. This is supposed to switch two people's inventories based on how many inventory slots they both have (must have the same amount) and it's been derping.
Code: [Select]
//%client and %user are two client objects

%client.player.naToolCount = 0;
while(%client.player.naToolCount < %client.player.getDataBlock().maxtools)
{
%client.player.BDtool = %client.player.tool[%client.player.naToolCount];
%user.player.BDtool = %user.player.tool[client.player.naToolCount];
%client.player.tool[%client.player.naToolCount] = %user.player.BDtool;
%user.player.tool[%client.player.naToolCount] = %client.player.BDtool;
messageClient(%client, 'MsgItemPickup', '', %client.player.naToolCount, %client.player.tool[%client.player.naToolCount]);
messageClient(%user, 'MsgItemPickup', '', %client.player.naToolCount, %user.player.tool[%client.player.naToolCount]);
%client.player.naToolCount++;
}

%client.player.BDtool = "";
%user.player.BDtool = "";
%client.player.naToolCount = "";
The problem is that, after it executes, the fields storing the ID of the item on %user's player are completely gone (according to dump();) and the %client still has his but can't see them (he's not being messaged about them properly). Can anybody shed some light on this for me?
« Last Edit: June 05, 2012, 07:58:17 PM by The Russian »

I can see that you have %client.player.naToolCount in both messageclients, but I havn't heard of the properties BDtool or NAtoolcount before.

There are a few errors in here. For example you have client....stuff instead of %client....stuff. Anyways, this may help you.

i haven't tested this, nor does it have proper checks, but this should help you.


function switchTools(%client1,%client2)
{
   //I think the variable to check a datablocks max tools is
   //datablock.maxTools , but we'll use 5 because I'm doing
   //this off of memory
   
   for(%i=0;%i<5;%i++)
   {
      %t[%client1,%i] = %client1.player.tool[%i];
      %t[%client2,%i] = %client2.player.tool[%i];
      
      %client1.player.tool[%i] = %t[%client2,%i];
      messageClient(%client1, 'MsgItemPickup', '', %i, %t[%client2,%i]);
      
      %client2.player.tool[%i] = %t[%client1,%i];
      messageClient(%client2, 'MsgItemPickup', '', %i, %t[%client1,%i]);
   }
}



I can see that you have %client.player.naToolCount in both messageclients, but I havn't heard of the properties BDtool or NAtoolcount before.
Those are two I created. BDTool stored the tools' ID temporarily and NAToolCount was used to determine which tool to update every time the loop ran.
There are a few errors in here. For example you have client....stuff instead of %client....stuff. Anyways, this may help you.

i haven't tested this, nor does it have proper checks, but this should help you.


-snip-

Thanks, I'll look over it in a bit. Also the datablock field was a bit derpy when I tried using it so I found that getDatablock().maxtools works better. Can you elaborate a little bit more on the the example error you gave?
« Last Edit: June 05, 2012, 11:27:07 PM by The Russian »

You just forgot a % on the fourth line from the while loop inside of the array.

You just forgot a % on the fourth line from the while loop inside of the array.

What array?

« Last Edit: June 06, 2012, 01:54:53 AM by elm »


I don't see anything related to arrays on that line.

The square brackets - that's how arrays work in most languages...

The lines at the end %client.player.BDtool = ""; will not clear any of the arrays. It would be better to store the BDtool, naToolCount, etc. as local variables which are cleared at the end of function scope automatically.

msgItemPickup requires that you use the ID of an item (nameToID(%item) / %item.getID()) which might be why they aren't working correctly.
« Last Edit: June 06, 2012, 04:33:31 AM by Space Guy »

The square brackets - that's how arrays work in most languages...

He's just trying to be pretentious because torque script doesn't "really" use arrays, it's best just to ignore him.

torque script doesn't "really" use arrays

It doesn't use anything related to arrays in any way.

%val[%something] = "hello";

is syntaxical sugar for

eval("%val" @ %something @ " = \"hello\";");

The official documentation call this syntax "arrays". It's usable as an array therefore it's helpful to call it one in this case.
« Last Edit: June 06, 2012, 04:43:27 AM by Space Guy »

You can call it a pseudoarray if it really bothers you that much

It doesn't use anything related to arrays in any way.

%val[%something] = "hello";

is syntaxical sugar for

eval("%val" @ %something @ " = \"hello\";");
If we're gonna play the "be a stuffty nitpicking bastard" game, you meant "syntactical".

Yeah, look at how much it contributes to the discussion.

You just forgot a % on the fourth line from the while loop inside of the array.
Oh lol. I was swapping around variables and haven't executed it since.
It doesn't use anything related to arrays in any way.

%val[%something] = "hello";

is syntaxical sugar for

eval("%val" @ %something @ " = \"hello\";");
I bet you're the kind of person who corrects someone when they say space is a vacuum.
You can call it a pseudoarray if it really bothers you that much
It has a nice ring to it.
The square brackets - that's how arrays work in most languages...

The lines at the end %client.player.BDtool = ""; will not clear any of the arrays. It would be better to store the BDtool, naToolCount, etc. as local variables which are cleared at the end of function scope automatically.

msgItemPickup requires that you use the ID of an item (nameToID(%item) / %item.getID()) which might be why they aren't working correctly.
Yeah I decided to do that after looking over elm's code. Also, I'm pretty sure the tool field stores the ID because I've changed tools in my own inventory in console before using that. I'll still try that though, just in case.

Thanks for the help guys. Except you Port.
« Last Edit: June 06, 2012, 09:03:30 PM by The Russian »