Game Development Community

Simple command to server not working

by Nmuta Jones · in Torque 3D Beginner · 01/31/2010 (11:08 am) · 10 replies

T3D

at the END of my default.bind.cs I have
moveMap.bindCmd(keyboard, t, throwsomething);


function throwsomething( )
{
   echo("trying to throw"); 
      commandToServer('Throw', "Pepper");
}

and in my script exec file I have confirmed that I am executing commands.cs, and in my commands.cs I have
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Misc. server commands avialable to clients
//-----------------------------------------------------------------------------

  function serverCmdThrow(%client, %data)
{
   %player = %client.player;
   if(!isObject(%player) || %player.getState() $= "Dead" || !$Game::Running)
      return;
   switch$ (%data)
   {
      case "Weapon":
         %item = (%player.getMountedImage($WeaponSlot) == 0) ? "" : %player.getMountedImage($WeaponSlot).item;
         if (%item !$="")
            %player.throw(%item);
      case "Ammo":
         %weapon = (%player.getMountedImage($WeaponSlot) == 0) ? "" : %player.getMountedImage($WeaponSlot);
         if (%weapon !$= "")
         {
            if(%weapon.ammo !$= "")
               %player.throw(%weapon.ammo);
         }
      default:
         if(%player.hasInventory(%data.getName()))
            %player.throw(%data);
   }
}


pretty basic stuff, and I've done this before, but my throwsomething( ) function is not even being called, because when I hit "t", I don't get anything echoed to the console saying "trying to throw".

I do have Pepper in my inventory and item scripts and I am able to pick it up fine.

thanks.




#1
01/31/2010 (11:22 am)
function throwsomething(%val)
{
if(%val)
   echo("trying to throw"); 
      commandToServer('Throw', "Pepper");
}
#2
01/31/2010 (11:58 am)
oh, so you HAVE to send it a parameter.
#3
01/31/2010 (1:47 pm)
You have to detect the keystroke, eg: %val = if(key_is_pressed ==1);
#4
01/31/2010 (2:29 pm)
Also, make sure that whenever you add new binds to default.bind.cs, you delete scripts\client\config.cs - I can't tell you how many times I've been caught out by that!
#5
02/01/2010 (3:00 pm)
To clear up some possible confusion. You DO NOT have to handle the %val parameter, which you can name anything you like it's just the first parameter. The ActionMap will still call your function whether or not you have that parameter.

In your example you have the keyboard key "t" bound to your function "throwsomething". So long as your ActionMap, "moveMap", is the latest action map pushed (in the ActionMap set) that defines an action for t your function will be called when t is pressed. The only exception to this is if "t" is bound to something in the GlobalActionMap (overrides everything) or is handled by the current UI (overrides all other action maps). If it's defined in the global ActionMap it will always be called no matter the order of other ActionMaps.

The purpose of the %val parameter is to differentiate between make and break events, or in the case of a keyboard better known as keydown and keyup events. The ActionMap will call your function once for each of the two events.

When you press the key the ActionMap will call your function will %val = 1, the make event. When you release the key the ActionMap will call your function with %val = 0, the break event. If you do not handle the %val parameter your function will just be called twice and your code executed twice.

The most likely reason for trouble is what Eikon said above. Next I would check to be sure the ActionMap is pushed in the ActionMap set and be sure that no other ActionMap is overriding "t".
#6
02/01/2010 (3:07 pm)
Wow, Wes, thank you! Well, yes, when I put the parameter in, it started working.

But thanks for your clarity....that really helps.
#7
02/01/2010 (3:25 pm)
If it worked because you added %val then it's likely to have been a .dso issue and just needed to be recompiled. Sometimes if your script is just not working and it does not make any logical sense try deleting your dso files.
#8
02/01/2010 (3:26 pm)
Yeah that's what I thought but I'm using T3D so there was no .dso file. I don't know.....one of those things at 2 am where you are sure that your lack of sleep is a contributing factor to not being even able to spell your own name without a compiler error.

#9
02/01/2010 (3:32 pm)
My mistake, this is the T3D forum, that means everything I've said before could be different in T3D, although I doubt it.

I did just notice that you're using bindCmd not bind. In that case you're specifying make and break script commands instead of a single function. You would want that to read:
moveMap.bindCmd(keyboard, t, "throwsomething();");

If you wanted to handle up and down separately like above for example you could do:

moveMap.bindCmd(keyboard, t, "throwsomethingMake();", "throwsomethingBreak();");
#10
02/01/2010 (3:37 pm)
Oh right.... I forgot to tell you.....that was the real problem. It worked when I changed to bind instead of bindCmd. Sorry, I left that part out.