Author Topic: Question(s)  (Read 1492 times)

I'm attempting to run a for loop to do stuff with an array. I'm using a global variable, how would I go about getting the number of variables in the array? I've tried $_.getcount(), which doesn't work.

I've actually got another question; I've been trying to add a weapon to someone's inventory through a script. I've been looking at the addItem script, but I can't seem to figure out where I'm going wrong. I've got it cycling through the variables, but it can't seem to tell whether the slot is empty or not.

So, how would I go about this? Any help is appreciated.
« Last Edit: March 19, 2011, 02:35:44 PM by Placid »

What I always did is have a variable that keeps the amount of variables in it like this:

$value[0] = "value";
$value[1] = "value";
$values = 1;


then just do the for loops with the variable values

edit:

So of course when adding that, you have to do $values++;, but that should be given.

array

Blockland doesn't have arrays. It's as simple as that.

Blockland doesn't have arrays. It's as simple as that.
Dohoh. They're still arrays, just a very dumb form of them.

Dohoh. They're still arrays, just a very dumb form of them.
Not actually arrays, but much as a replacement for them.

Expanded on your first post, this is how you'd do something.

$value[$values] = "player 1";
$value[$values++] = "Player 2";
$value[$values++] = "Player 3";
$value[$values++] = "Player 4";
$value[$values++] = "Player 5";
$values++;

for(%x=0;%x<$values;%x++)
{
echo($value[%x]);
}

Will print to the console:
player 1
Player 2
Player 3
Player 4
Player 5

Alright. Thanks guys.

I've actually got another question; I've been trying to add a weapon to someone's inventory through a script. I've been looking at the addItem script, but I can't seem to figure out where I'm going wrong. I've got it cycling through the variables, but it can't seem to tell whether the slot is empty or not.

edit: stuff i forgot to add the code.
Code: [Select]
%tool=%player.tool[%i];
echo("wat1");
                                if(%tool==0) {
echo("wat2");
%player.tool[%i]=$GunNames[%i];
%player.weaponCount++;
messageClient(%c,'',"\c5" @ $GunNames[%g] SPC "has been bought!");
%c.score = %c.score-$GunPrices[%g];
break;
}
the %i is a for loop for the player's slots.
ignore the echo, it's for me trying to find where it's stopping.
it's running through this for loop, but it doesn't sense that %tool is empty.
« Last Edit: March 19, 2011, 02:43:43 PM by Placid »

I believe -1 is an empty slot, but just to make sure, instead of using ==0, use < 1

I would just recommend doing like:
if(!isObject(%player.tool[%i]))



Dohoh. They're still arrays, just a very dumb form of them.

They are ways of setting a variable without using eval.
$variable[$amount] = "hurr durr"; is a replacement to eval("$variable" @ $amount @ " = \"hurr durr\";");
« Last Edit: March 19, 2011, 03:15:03 PM by Bauklotz »

Alright, forget it. Every time I fix one bug, another one comes up. When I fix that one, one that i've already fixed comes up again.

Thanks for all your help, learned a lot.

Double post, bump.

I looked back at the script and realized some of my errors. I only have one problem, now: it won't add the tool. It doesn't work with if(!isObject(%player.tool[%i)) or if(%player.tool < 1)
it always senses that that is true

edit: oh holy stuff i'm a handicap, i started %i at 0 and it's counting 0 as a slot

problem still remains, it's counting slot 1 with a hammer as unfilled
« Last Edit: March 23, 2011, 12:36:45 AM by Placid »

The tool index is .tool[0] to .tool[4] on a standard player.

The tool index is .tool[0] to .tool[4] on a standard player.
alright
Code: [Select]
for(%i=0;%i<%c.player.getdatablock().maxtools;%i++) {
%tool=%player.tool[%i];
echo("wat1");
if(!isObject(%player.tool[%i])) {
echo("wat" SPC %i);
%player.tool[%i]=$GunMod::Image[%g];
%player.weaponCount++;
messageClient(%c,'',"\c3" @ $GunMod::Name[%g] SPC "has been bought!");
%c.score = %c.score-$GunMod::Price[%g];
break;
}

got any ideas why this might be returning true

yes, i've tried < 1
« Last Edit: March 23, 2011, 12:42:40 AM by Placid »

I herped a derp. You were missing a messageClient with a specific type called 'MsgItemPickup'

Type in /test, recieve gun.

Code: [Select]
// Your formatting is dog richard so let me show you how to make your stuff readable.
// You might be able to solve your own problems if you make it a bit cleaner.
function serverCmdTest(%client)
{
echo("INITIATING TEST.");

%obj = %client.player;

if(!isObject(%obj))
return false;

%maxTools = %obj.getDatablock().maxTools;

echo(" +- 1. Starting Loop");

for(%i = 0; %i < %maxTools; %i++)
{
echo(" +- 2. Loop #" @ %i);

%tool = %obj.tool[%i];

if(!isObject(%tool)
{
echo(" +- +- Unoccupied! Giving a gun.");

%player.tool[%i] = GunItem.getID();
%player.weaponCount++;

messageClient(%client, '', "\c3Gun\c6 purchased!");
%client.score = %c.score -= 5;

// This is the part you were missing. It informs the UI we have a nwe item.
messageClient(%client, 'MsgItemPickup', '', %i, GunItem.getID());

return true;
}
else
{
echo(" +- +- Occupied!");
}
}

echo(" +- 3. FAIL! All slots (" @ %maxTools @ ") occupied!");
return false;
}

I herped a derp. You were missing a messageClient with a specific type called 'MsgItemPickup'

Type in /test, recieve gun.

Thanks a lot. There's only one problem now; it gives me the gun image, shows that I have it in slot 4, but it doesn't let me use it. If I scroll down from printer, it shows that I have a printer. If I press q while in slot 4, it doesn't show any item.

Thanks a lot. There's only one problem now; it gives me the gun image, shows that I have it in slot 4, but it doesn't let me use it. If I scroll down from printer, it shows that I have a printer. If I press q while in slot 4, it doesn't show any item.
Try changing GunItem.getID() to GunImage.getID() in both places.