Game Development Community

Cloaking, I just can't get it working...

by Marcus Fernstrom · in Torque Game Engine · 03/03/2009 (7:03 am) · 16 replies

The title says it all I'm afraid.
I've looked around the forum, but I'm somewhat new to TGE and in places where the commands are clear enough, I've no clue where to put it..

In other posts I've seen directions on where to put it, but more or less pseudocode..

Anyone feeling like helping out a newbie with this, or can point me to a "tutorial" that goes over how to make cloaking work?

I'm just trying to make myself cloak when pressing a button. So, it's simple enough.. err..

Right now I'm using a fresh install of TGE 1.5.2 and running starter.fps (I figured it was a good a place as any to break with my bad scripts)

#1
03/03/2009 (8:26 am)
I don't know the cloaking system in any sort of detail, and it'll probably be better for you if I don't hold your hand. Here's the basic process you'll need to go through:

You want cloaking to depend on user input, specifically a key press. Therefore, you'll need to create a function and bind it to a key. Have a look at default.bind.cs.

User input is on the client, but cloaking happens on the server. Therefore, you'll need commandToServer so your clients can tell the server they want to be cloaked. There are some examples of this in default.bind, and have a look at commands.cs

Then, on the server, in your server command function, you can cloak the Player.
#2
03/03/2009 (8:31 am)
Thanks Daniel,

I'll have a look and try to figure out what I'm doing wrong.

I don't remember that the GettingStarted.pdf had any info on CommandToServer or CommandToClient, hmm.. *Goes to re-read*

But I think you might just have explained all I need to know :D
I know the commands, just not how to get them to work ;)

*Wanders off to try some things*

--Edit--

Wow I feel stupid..
Yep, the getting started pdf has examples of CommandToServer..

--Second Edit--

It's alive! It's alive!

It works now, I can't de-cloak but that's not an issue now that I know how to set the commands (And make them work)

Thanks for the help Daniel.

Here's what I did (If someone else has a problem with it).

In /client/scripts/default.bind.cs I added this at the bottom of the file:

function cloakingDevice(%val)
{
commandToServer('cloakme');
}

moveMap.bind(keyboard, "c", cloakingDevice);

And then in /server/scripts/commands.cs I added this at the bottom:
function serverCmdcloakme(%client)
{
%client.player.setCloaked(true);
}

And it works, but can only cloke, not de-cloak yet ;)
#3
03/03/2009 (10:50 am)
Ok, I decided to make a function that only sends one command from the client, it sends the command "cloak", like this:
commandToServer('cloak');
And for the server to make a check to see if the player already is cloaked, using isCloaked.
But errm, this is where my utter newbieness comes into play and I can't figure out how to make the "if" work.

IE, how do I check what value was returned by isCloaked?
#4
03/03/2009 (11:01 am)
isCloaked should return a boolean value - that is a switch with two states, 1 or 0, on or off, true or false. In TorqueScript you could say
if(isCloaked() == false)
   cloak();
else
   uncloak();
That's totally pseudocode, I'm sure you're aware ;). You could also evaluate like this:
if(isCloaked() == 0)
If you're looking for when the character IS cloaked, then evaluate against '1' or 'true'.

Here's a more advanced technique:
setCloaked(!isCloaked());
Here we use the result of asking whether we're cloaked to set whether we're cloaked. But the exclamation mark negates the value. So say our player is currently cloaked. We ask whether it's cloaked, and it retuns 'true'. The opposite of true (!true) is false, so we setCloaked(false) and the player uncloaks.

Of course, I don't know if that's correct syntax for the cloaking functions :P.

EDIT: It's correct - just remember your object references:
%player.setCloaked(!%player.isCloaked());

EDIT: Sorry about chaiging my original response ;P. I suggested gettingstarted.pdf, then realised there were better examples more suited to your needs already in the scripts (like those for changing, I think, the camera behaviour).
#5
03/03/2009 (11:10 am)
Daniel I will send you a cookie.
That almost worked, I kept getting an error about isCloaked command not found but I figured that hey, maybe it needs to know *what* to check if it's cloaked, so I just added it like this:
function serverCmdCloak(%client)
{
	%client.player.setCloaked(!%client.player.isCloaked());
}

That works, now my player can cloak and de-cloak, with a small problem.
Apparently, it does this check every time the button is pressed, and released.

So I can now cloak as long as I hold down the button, which incidently is a good thing for an idea I was thinking about, but anyways, any short pointers on how to keep the effect once the button is released, so that it takes press&release to initiate/remove the effect?

(I hope I make some kind of sense and promise to send cookies to anyone that helps out) ;)
#6
03/03/2009 (11:25 am)
Haha, sorry anyone else who wants a cookie ;)

In the function bound to the keyboard (in default.bind, right?), you should have added %val as an argument. Like:
function myFunction(%val)
{
   ...
}
%val is set to 1 (or 'true') when the key is pressed down, and to 0 (false) when the key is released. So you're actually getting two calls of your function over a keypress cycle - the key is depresed, the function is valed and %val == 1. Then the key is released, the function is called and %val == 0.
#7
03/03/2009 (11:32 am)
I tried just changing %client to %val but it didn't work at all.
Are you sure though that you're thinking the same way I am?

Right now I cloak when the key is pressed, and decloak when the key is released.

I want the key to work as a switch instead, press it once and cloak, press it again to decloak.
#8
03/03/2009 (11:51 am)
We're thinking of different functions. You should have written two functions to accomplish cloaking - one is a serverCmd, and one is a regular function. That regular function is the one you bound to the keyboard button press, and should have an argument %val. Have a look at the functions in default.bind.cs for some examples.

EDIT: Just went back and realised that the function is the one you named 'cloakingDevice'. That %val argument is the one I was referring to.
#9
03/03/2009 (12:59 pm)
Ok, I can get cloaking/uncloaking working like I wanted, now I'm looking at something similar, cloaking when pressing the button and decloaking when pressing it again, with a twist.

Right now, while the button is pressed, I'm cloaked, the energybar drains, when released it is recharging and I decloak.

What I'm trying to do is incorporate a check to see if the player is cloaked, if so, then decloak and restore the energy recharge.
If not cloaked, then cloak and start the energy drain.

--Edit--

I think my main problem is understanding how to work the if/else functions properly.
How to read if a value is true, if so then do this, otherwise do this.
I can then hook that up to the %val and run it once per keypress, not once when pressed and then again when released.
#10
03/03/2009 (3:20 pm)
This is what I have right now, I don't understand why it's not working..

I'm trying to make a toggle, press the button and cloak + drain energy, press it again to decloak and start recharging energy.

I have this in commands.cs
function serverCmdCloak(%client)
{
	%client.player.setCloaked(true);
	%client.player.setRechargeRate(-0.6);
}

function serverCmdDecloak(%client)
{
	%client.player.setCloaked(false)
	%client.player.setRechargeRate(0.3);
}

And I have this in default.bind.cs

function cloakingDevice(%val)
{
if (%val)
{
	commandToServer('Cloak');
}
else
{
	commandToServer('Decloak');
}
}

moveMap.bind(keyboard, "c", cloakingDevice);

Checking the console when I press the c button, it states
Mapping string: Cloak to index: 3
serverCmdCloak: Unknown command.
Mapping string: Decloak to index: 4
serverCmdDecloak: Unknown command.
serverCmdCloak: Unknown command.
serverCmdDecloak: Unknown command.

If some merciful scripter would have a look at that and tell me how stupid I am and how to do it right, I'd be very grateful indeed.

I've tried a lot of things, looked all over the place, but not found anything to help me.

Daniel you've been very helpful so far, maybe you have an idea about this? :)

--Edit--

I've been trying to look for a function I know works that way, the toggleconsole function, but I can't find it XD

And it's so simple, it's just an if/then thing, but I can't get it to work! I feel like an illiterat trying to read the illiad. :P
#11
03/04/2009 (9:51 am)
Sorry I didn't see this last night... that's a tricky problem. Your functions seem to be completely correct, so that's not the issue. Your conole indicates that for some reason, Torque isn't finding the server commands. What file did you put your serverCmdCloak and serverCmdDecloak in? Are there any script compilation errors in the log? (There'll be a message in the top of the console and some red text.)
#12
03/04/2009 (9:55 am)
There's a typo in the pasted code, a missing ; but I put that in, still the same problem though.

The functions are in /server/scripts/commands.cs and the calls are made from /client/scripts/default.bind.cs

With a little tweaking I can make it cloak while the button is pressed, and decloak when the button is released, and with the energybar drain working.

Or, I can make it work as a toggle, but only for the cloaking, as soon as I try to implement the energybar using if/then types, I get the result above here. Commands not found etc.
#13
03/04/2009 (10:37 am)
Oh man, I can't believe I didn't notice that :P. Okay, I really can't see what could be your problem... I'm going to try to add your functions to my own project and see if they work for me. I'll be a little while on that, though.
#14
03/04/2009 (10:43 am)
Thanks so much Daniel, I'll have to owe you for this.
Yeah, I copied those lines right out of the scripts, they're exactly like that (Except for some weird spacing thing, or maybe that's just to make it easier to read here on the website).

I can't figure it out, at least they should do something, or break the engine when used, but I'm utterly puzzled when getting the 'command not found' thing...

For the time being I'm trying to implement some other things, adding a weapon and getting that to work, might as well get my pretty 3d stuff into a DTS format and figure out how to mount them and blow them up and whatnot.

I'll send you a whole tray of cookies for this Daniel ;)

It's very appreciated though, really, thank you.
#15
03/04/2009 (11:29 am)
I've pasted in the functions exactly as you had them above (after fixing the semicolon) and they work for me. When I press the buton down I cloak, and when I release it I decloak. To make it a toggle function, I used these functions:
function serverCmdCloak(%client)
{
   if(%client.player.isCloaked())
   {
      %client.player.setCloaked(false);
      %client.player.setRechargeRate(0.3);
   }
   else
   {
      %client.player.setCloaked(true);
      %client.player.setRechargeRate(-0.6);
   }
}
function cloakingDevice(%val)
{
   //Only do this when we push the button
   if (%val)
   {
      commandToServer('Cloak');
   }
}

moveMap.bind(keyboard, "c", cloakingDevice);
This works for me. Just have to check: are you deleting your config.cs and .dso after changing default.bind.ce? Changes there my not be applied while the config file exists. Also see if deleting all your .dsos helps - they should be recompiled when the .cs files have changed, but I've heard it can solve some issues.
#16
03/04/2009 (11:58 am)
Daniel, you're a saint.

I just tried what you posted just now, and it works.
It toggles just fine with your code.

Yep, I always run the DeleteDSOs.bat and DeletePrefs.bat (Just modded the deletedsos to also remove the log file, making sure I get a fresh log every run)

Looking at it, I don't see anything different from how I've tried before..
Damn.. Must be something small, a blank space, wrong ', or *something* in my code..

Thanks so much for bearing with me :D

Cookies on the way ;)

Now I'm just going to implement a check in the code to make sure the player has energy left. I think I saw something about that around the forum somewhere... *Goes hunting*

Thanks again Daniel!