Game Development Community

Spawning an object at the players location via pressing a button?

by Michael A Bocchi · in Torque 3D Professional · 02/24/2010 (2:50 pm) · 11 replies

Greetings.

I am trying to spawn an object at the players coordinates when a button is pressed. So far im not having much luck.
I have this object as a static shape in its own .cs file. (yes its exe'ed)

datablock StaticShapeData(My_Object)
{
	shapeFile = "art/shapes/myObject.DAE";
	category = "myThingy";

   className = "RedThingy";
    mass = 2;
    friction = 1;
    elasticity = 0.3;
    emap = true;
};

I have this in default.bind.cs
function create_MyObject(%val)
{
	if (%val == 1)
	{
		%s = new StaticShape()
		{
			datablock = my_object;
		};

		%s.setTransform(player.getTransform());
	}
}

moveMap.bind( keyboard, b, create_MyObject );

If some one could let me know if i'm on the wrong path to do this please let me know, But this is how I used to do it in TGE. (long time ago. I know, things have probably changed.)
Or if you know how to do it please share. It would be greatly appreciated.
Thank you.

#1
02/24/2010 (3:21 pm)
Is player actually defined somewhere?
#2
02/24/2010 (3:53 pm)
you know what? I don't think it is. Where is the best place to define player in t3d? In t3d it seems that player is never defined in defaultplayerbody, or am I mistaken? Is it better to try and define it in spawn.cs?
#3
02/24/2010 (5:31 pm)
Try using a Server Command.

Here's what I use for some customized AInodes.

//Key bind
MoveMap.bindCmd(keyboard, "x", "commandToServer(\'DropNode\');", "");

//Create a new node function
function ServerCmdDropNode(%client)
{
   %position=VectorAdd(%client.player.position, "0 0 1");
   %node = new AIPathNode() 
   {
      dataBlock = PathNode;
      position = %position;
   };
}

Also typo which should be giving you a script error in the console (missing % off player.gettransform) and you can just have (%val) no need for ==1.
#4
02/24/2010 (5:49 pm)
Hmm, seems everything I try just gives me this

<input> (0): Unable to find object: : '0' attempting to call function 'performClick'

Also it was never triggering a script error in the console with the missing % if that helps at all. I'm really lost on this one.
#5
02/27/2010 (8:55 pm)
Steve's code looks like it should work. That "performClick" error isn't directly caused by his function.

A quick find in files reveals that x appears to frequently be bound to specific functions in various editor modes, and often works by simulating a button click (hence the call to performclick).

The editor bypasses moveMap when it's open unless you're holding the right mouse... try pressing it outside the editor.

Why this creates a script error, I can't say. No use of x in the editor creates an error in the few modes I just tested in 1.1b, but obviously it never makes it to its movemap bind either. Have you edited the editor scripts at all?
#6
03/01/2010 (8:41 am)
alright, my problem here isn't spawning an object as I took out the player.getTransform and just put in an arbitrary number "35 10 20" and then when I press a button it comes right in at the specified location.

So my problem lies with the player and the engine not being able to find the player object.

Where in the game does it add a player where the object can be named? I have tried %client as well and that didn't work either. So I think I need to find a way to name the client as its created? Does this sound right? But I admit I don't know were to do that or even how to name it. Any help with this one?
#7
03/01/2010 (9:21 am)
Thanks for all the help I appreciat it. I finally figured out what I had done wrong. It was one of those really dumb things too. I had to change %player in spawn.cs to $player. Go figure right?
#8
03/01/2010 (9:56 am)
When you're not certain how to pass the local object (%player) ... CHEAT! ... and use a global ($player).

You didn't use a server command though did you?
#9
03/01/2010 (1:31 pm)
For the record, the server command like in Steve's example is the right way to do this... just making a function and binding the command in a client script (default.bind.cs) will result in adding an object on the client. I have no idea what the end result of that would be, but it sounds bad -- though you might be able to get away with this in singleplayer, I'm not sure.
#10
03/01/2010 (2:34 pm)
yeah, That is good to know. I didn't use a server cmd, and the game is single player. But I might go back and rework it anyways to be multi-player friendly.

The reason I didn't use the serverCmd is that some how all it would do would say: Mapping string: CreateMyObject to index 3.
#11
03/01/2010 (4:20 pm)
Steve wasn't really clear about where that code was supposed to go -- if you just put the bind and the function in default.bind.cs, that's a client script, so the server command doesn't exist. The function from his example needs to go into a server script (commands.cs would be an easy place to slot it in) and the bind can go in default.bind.cs.

Anyway, if your solution works, problem solved. I just wasn't sure if creating an object on the client in singleplayer really would work out, since even singleplayer technically uses a client/server setup to some degree.

Also if you rename the server cmd remember the naming convention there, if you call the command "CreateMyObject" then the function is called ServerCmdCreateMyObject.