Game Development Community

Simple Code Help

by Teromous · in Torque Game Engine Advanced · 09/08/2008 (7:25 pm) · 8 replies

I simply want to create an object that appears at a players location and moves forward at the angle he's looking at. I can work out the vector adding later so that it doesn't collide with the player immediately. I've been trying to script this all night...I'm used to TGB where I can simply use things like $Player.getPosition etc but it doesn't seem to work here hehe. I'm also having problems scripting the object...I do want this to be a projectile type object, but it's not coming from a gun or anything. Here's what I've got so far:

function fireAcidSplash(%this) 
{
   echo(%this);
   echo("Acid Splash!");
   %projectile = AcidSplashProjectile;
   %pos = %this.gettransform();
   %rot = %this.getrotation();

   %p = new Item() {
         position = %pos;
         rotation = %rot;
         scale = "1 1 1";
         dataBlock = "AcidSplashProjectile";
         collidable = "1";
         static = "1";
         rotate = "1";
         velocity = "50";
      };
   
   MissionCleanup.add(%p);
   return %p;
}

I have the AcidSplashProjectile already defined with all of the other datablocks, I just can't get it into the game... (by the way this function is called by a keybinding)

#1
09/08/2008 (7:28 pm)
If you look at what "getTransform()" returns, you will notice that it has 7 values. 3 position and 4 rotation.

You can directly set the transform of the new item by going:

%p.setTransform(%this.getTransform());
#2
09/08/2008 (7:45 pm)
It seems to be having a hard time finding the player really. I don't know what global the player is under. I'm using the stronghold demo and I was hoping that when a player hit the key %this would point to the player pushing the key, then .getposition() or .gettransform() would return a value but I always get:

"Unable to find object: '0' attempting to call function 'getTransform'"
"Unable to find object: '0' attempting to call function 'setTransform'"

...etc. How do I point to the players location?
#3
09/08/2008 (7:51 pm)
You can always find where the player is spawned and then create a global variable referencing the object. I can't recall off the top of my head where the player is created, but I think there is a function called spawnPlayer().
#4
09/08/2008 (8:03 pm)
Yeah I was looking in there to do that earlier but couldn't find what to reference the variable to. Sometimes it seems that things are overcomplicated.

Another thing I forgot to mention, is that with multiple players if I put a $player=%player type of statement, then it would change the variable each time a player is spawned right? I guess the best way to do it is to get the location of the person pressing the button, do you know a way to do this?
#5
09/09/2008 (3:22 am)
In game.cs->createPlayer()
set
$player = %this.player;

Use $player as global var. {this is for single player}

For multi-player,
check this thread..
www.garagegames.com/mg/forums/result.thread.php?qt=78780
#6
09/09/2008 (8:16 am)
Thanks for the link to that thread, I'm going to mess around with it a bit to see if I can piece something together. That looks like it would work for finding the location of all players, but is there a way to determine which player hit the button?

i.e. Players one and two enter the game, player one hits the button to shoot the acid, the function that is called by the keybind knows that player one hit the key, then the function would find the location of the player and spawn the projectile at the player's location.

Right now it I'm trying to get the function fireAcidSplash to know which player has pressed the button using:

moveMap.bind(keyboard, "]", fireAcidSplash );

Any tips or ideas?
#7
09/09/2008 (3:19 pm)
Ok so far I have a lot more working than I did before. I had to go into the game.cs and do a
$player = %player;
under
function GameConnection::createPlayer(%this, %spawnPoint)
as a temporary fix until I can figure out how to get the players location in multiplayer working.

The code below is what I ended up with. It creates an object in front of the player and shoots it where they're looking at:
function fireAcidSplash() 
{
   %pos = $player.getTransform();
   %x = getWord(%pos, 0);
   %y = getWord(%pos, 1);
   %z = getWord(%pos, 2);
   %z += 2.0;
   %finalPos = %x SPC %y SPC %z;
   %eye = $player.getEyeVector();
   %vec = vectorScale(%eye, 10);   
   %finalPos = vectorAdd(%finalPos, %eye);
   
   %vec = vectorScale(%eye, 40);
   
   %projectile = "AcidSplashProjectile";
   %p = new Projectile() {
      dataBlock        = %projectile;
      initialVelocity  = %vec;
      initialPosition  = %finalPos;
   };
   MissionCleanup.add(%p);
   return %p;   
}
#8
09/12/2008 (6:51 pm)
I figured out how to do this with a network of players, without needing to make $player. It was made possible by the "gem-a-day" resource in the documentation section for TGEA. Plastic Gem #30 teaches you how to send a request from the client to the server, then the server sends out. It has info on distributing to a single client or even multiple clients. The code above had to be changed from:

%pos = $player.getTransform();

To:

%pos = %client.player.getTransform();

If anyone has any questions on what I did I'd be more than happy to explain.