Game Development Community

Is this the correct way to write a function?

by Mike Rowley · in Torque Game Engine · 03/02/2007 (6:14 pm) · 9 replies

I've been looking thru the code in the examples, but haven't found a function that calls a datablock when used. What I'm doing is an attempt to make player selection simpler than changing a whole bunch of script files. There will be checkboxes giving the player a choice of genders. Each checkbox calls 1 of 3 datablocks depending on their choice.
Here's the function I wrote. (haven't tested it becouse it isn't finished yet.) What I'm looking for is simple:
Is this a correct function that would work?

//playerSelect.cs
//---------------------------------------------------------------
function selectPlayer(%myPlayer)
{
if %myPlayer == Male;
     PlayerBodyMale;  //datablock for male player
if %myPlayer == Female;
     PlayerBodyFemale; //datablock for female player
else
     PlayerBody;     //default player datablock
}

My thoughts are to have a checkbox call Male or Female. If neither is checked, it defaults to the orc.
Checkbox Male would call the male part of the function which would call the PlayerBodyMale datablock which has all the information for the male player. Would this work? or have I gone totally astray?

#1
03/02/2007 (6:28 pm)
I'd do something more along these lines:

//playerSelect.cs
//---------------------------------------------------------------
function selectPlayer(%myPlayer, %armor)
{
   switch$(%myPlayer)
   {
      case "Male":
         %armor = "PlayerBodyMale";  
      case "Female":
         %armor = "PlayerBodyFemale";   
      Default:
         %armor = "PlayerBody";    
}

Then you would need to adjust your createPlayer method accordingly.

function GameConnection::createPlayer(%this, %spawnPoint)
{

   // Create the player object
   %player = new Player() {
      dataBlock = %this.armor;
      client = %this;
   };
   MissionCleanup.add(%player);
}

N.b. The code I provide as it is won't work, it's just a guide to get you going in the right direction.
#2
03/02/2007 (6:32 pm)
Thanks Tim.
I tried a switch statement before, but couldn't get it to work. Now I see why.
I was writing it: $switch and you have it switch$
Is that correct? I've read lately that the switch statement has the $ sign on the wrong side for some reason, but can't recall.
Thanks. That gives me a better direction. (the if statements didn't seem too good) :)
#3
03/02/2007 (6:42 pm)
To further answer your question, your function is incorrect in a few areas.

You're declaring %myPlayer as a string and trying to compare it with an integer. Oh, and your syntax is a little off.

Example:

if(%myPlayer $= "Male") // COMPARING A STRING

   if(%myPlayer == 12)     // COMPARING AN INTEGER

You are not storing your value / variable correctly.

Example:

%someValue = PlayerBodyMale; // STORE A LOCAL VARIABLE

   $someValue = PlayerBodyMale; // STORE A GLOBAL VARIABLE
#4
03/02/2007 (6:51 pm)
Ok. I can see where I'm not storing my variable/value, but I don't see where i'm comparing a string with an int. But then, I can see there is more missing from my function too. :blush:
Thanks.
#5
03/02/2007 (7:47 pm)
Quote:
I don't see where i'm comparing a string with an int.

I'll try to explain it in an easy fashion for you.

A string can contain alpha numeric characters (both numbers and letters.)

An integer can only hold numerical data (mainly used for mathematical calculations.)

Let's look at your "if" statement:
if(%myPlayer == Male)

Consider the variable %myPlayer as a bucket in which we want to store things in. In this case, we are checking if the stored value is "Male". The value "Male" is a string therefore we should use $= and not ==

Example:
// use $= to compare strings
   if(%myPlayer [b]$=[/b] "Male")

If our variable were to hold an integer than we would use ==

Example:
// use == to compare ints
   if(%playerCount [b]==[/b] 13)

Make sense?
#6
03/02/2007 (7:49 pm)
Ok, that makes since. Thankyou. I need to do some more studying. :/
I'll get it tho. 1 step at a time. Thanks for your help Tim. :-)
#8
03/02/2007 (11:14 pm)
The only problem with using the TDN wiki is that there is TOO MUCH information :)
#9
03/03/2007 (5:48 am)
No I hadn't. Thanks. :)