C-trace, click to place an object

Posted By: binsky33333

C-trace, click to place an object - 07/18/09 03:17

Hi everyone, i just have a quick question about using c_trace to place an object where i click. Ok basically i have a crosshair at the center of the screen. Basically what i need is when i click, a crate spawns on the ground, where the crosshair was pointed. Here is what i came up with so far.

Code:
temp.x = 399;
temp.y = 299;
temp.z = 100;
vec_for_screen(temp, view_build);

if(c_trace(temp, vector(0,0,-10000), NULL))
{
	ent_create("crate.mdl", temp, NULL);
}



Its kinda working out. Though it will only go 100 quants for the temp.z If i make it any more it becomes very inaccurate. Can someone help me?

BTW view_build is a camera.

Thanks!
Posted By: EvilSOB

Re: C-trace, click to place an object - 07/18/09 05:45

Im not crash-hot with c-script, but here goes...
Code:
vec_set(temp, vector(1000,0,0));    //1000 is max "depth"  to look
vec_rotate(temp, camera.pan);
if(c_trace(camera.x, vec_add(temp, camera.x), NULL))
{
	ent_create("crate.mdl", target.x, NULL);
}


Posted By: Claus_N

Re: C-trace, click to place an object - 07/18/09 14:02

I'd do it like this smile

Code:
temp.x = crosshair_x;
temp.y = crosshair_y;
temp.z = 0;
 
vec_set(target,temp);
target.z = 10000;
 
vec_for_screen(temp,view_build);
vec_for_screen(target,view_build);
 
if(c_trace(temp,target,IGNORE_ME) > 0)
{
	ent_create("crate.mdl",target,null);
}


Posted By: binsky33333

Re: C-trace, click to place an object - 07/18/09 14:06

wow cool! That works, but i cant place an object while my camera is on an angle?
Posted By: EvilSOB

Re: C-trace, click to place an object - 07/18/09 14:30

Which one... Mine should.
Posted By: Claus_N

Re: C-trace, click to place an object - 07/18/09 14:41

Mine should as well :p

Maybe you just forgot to replace "camera" with "view_build" in EvilSOB's code, if that is the view you're using. Other than that, both code examples should probably do it, though I'm not familiar with vec_rotate smile
Posted By: binsky33333

Re: C-trace, click to place an object - 07/18/09 14:48

Thank you guys sooo much everything works now! I really appreciate it!
Posted By: binsky33333

Re: C-trace, click to place an object - 08/04/09 20:10

Is there anyway where like i can make it so it displays the model and you can move your mouse around and the model will move with it. Kind of like a ghost so the player can see where they're putting the object. Then when you click it places the object?
Posted By: Claus_N

Re: C-trace, click to place an object - 08/05/09 09:00

To move the object with the mouse, you just need to keep tracing (tracing like in the code sample in my post above). Here's an example (not tested):

Code:
ENTITY* ghostObject;	// Global entity pointer
var isPlacingObject = 0;	// Global variable
...

function PlaceObject(var _objectNum)
{
	wait(1);
	proc_kill(4);
	
	isPlacingObject = _objectNum;
	
	while(isPlacingObject != 0)
	{
		normal.x = crosshair_x;
		normal.y = crosshair_y;
		normal.z = 0;
		
		vec_set(target,normal);
		target.z = 10000;
		
		vec_for_screen(normal,view_build);
		vec_for_screen(target,view_build);
		
		if(c_trace(normal,target,IGNORE_ME + IGNORE_PASSABLE) > 0)
		{
			if(mouse_left)
			{
				ent_create("crate.mdl",target,NULL);
				
				if(ghostObject != NULL) {ptr_remove(ghostObject);}
				isPlacingObject = 0;
				
				break;
			}
			
			if(ghostObject == NULL)
			{
				ghostObject = ent_create("crate.mdl",target,NULL);
				ghostObject.flags |= TRANSLUCENT;
				ghostObject.flags |= PASSABLE;
				ghostObject.alpha = 30;
			}
			else
			{
				vec_Set(ghostObject.x,target);
			}
		}
		
		wait(1);
	}
}


Posted By: binsky33333

Re: C-trace, click to place an object - 08/05/09 17:43

Here is what i have. This is WDL script.
Code:
entity ghostobject
{
	flags = TRANSLUCENT;
	alpha = 30;
}
var isplaceobject = 0;
function spawn()
{
	while(isplaceobject == NULL)
	{
	temp.x = mouse_pos.x;
	temp.y = mouse_pos.y;
	temp.z = 0;
	
	vec_set(target,temp);
	target.z = 10000;
	
	vec_for_screen(temp,view_build);
	vec_for_screen(target,view_build);
	
	if(c_trace(temp,target,IGNORE_ME | IGNORE_PASSABLE) > 0)
	{
		if(mouse_left)
		{
			ent_create("crate.mdl",target,null);
			if(ghostobject == NULL)
			{
				ent_remove(ghostobject);
				isplaceobject = 0;
			}
			
		}
		if(ghostobject == NULL)
		{
			ghostobject = ent_create("crate.mdl",target,NULL);
		}
		else
		{
			vec_set(ghostobject.x,target);
		}
		wait(1);
	}	

	}
	

}



Now the thing is when i click, it creates a huge line of boxes from the target to the camera. Also no ghost box appears, and when you click the engine just crashes from overwhelming boxes.
Posted By: Claus_N

Re: C-trace, click to place an object - 08/05/09 20:34

Ah sorry, forgot you're using C-script instead of Lite-C smile

First of all:
Code:
entity ghostobject
{
	flags = TRANSLUCENT;
	alpha = 30;
}


This creates a view entity, but you want a "normal" entity , not a view entity, so replace that code with this:
Code:
entity* ghostobject;


Change the while condition ( "while(isplaceobject == NULL)" ) to "while(isplaceobject != 0)", as it's a variable, not a pointer, and set this variable to something else than 0 before entering the while loop - this way you can always cancel the loop by setting the global variable to 0 anywhere in your code.

In this code:
Code:
if(mouse_left)
		{
			ent_create("crate.mdl",target,null);
			if(ghostobject == NULL)
			{
				ent_remove(ghostobject);
				isplaceobject = 0;
			}
			
		}


You need to change the nested if-condition to "ghostobject != NULL".

And now a very important part:
Code:
if(ghostobject == NULL)
		{
			ghostobject = ent_create("crate.mdl",target,NULL);
		}
		else
		{
			vec_set(ghostobject.x,target);
		}


You need to change it to this:
Code:
if(ghostobject == NULL)
		{
			ghostobject = ent_create("crate.mdl",target,NULL);
			ghostobject.transparent = on;
			ghostobject.passable = on;
			ghostobject.alpha = 30;
		}
		else
		{
			vec_set(ghostobject.x,target);
		}


You really need to make it passable in order to let the trace ignore the ghostobject.

Tell me if it's still not working smile
Posted By: binsky33333

Re: C-trace, click to place an object - 08/06/09 04:15

Wooo its works... though there is 1 tiny problem, you can only do it once. It only spawns 1 ghost and can only spawn 1 crate, and then you can never spawn anything else...
Posted By: Claus_N

Re: C-trace, click to place an object - 08/06/09 09:24

Maybe you just forgot to set 'isplaceobject' to something else than 0 when you want to create a new object? Otherwise please post your full code again smile
Posted By: binsky33333

Re: C-trace, click to place an object - 08/06/09 16:15

Here is it:
Code:
entity* ghostobject;
var isplaceobject = 1;
function spawn()
{
	while(isplaceobject != 0)
	{
	temp.x = mouse_pos.x;
	temp.y = mouse_pos.y;
	temp.z = 0;
	
	vec_set(target,temp);
	target.z = 10000;
	
	vec_for_screen(temp,view_build);
	vec_for_screen(target,view_build);
	
	if(c_trace(temp,target,IGNORE_ME | IGNORE_PASSABLE) > 0)
	{
		if(mouse_left)
		{
			ent_create("crate.mdl",target,null);
			if(ghostobject != NULL)
			{
				ent_remove(ghostobject);
				isplaceobject = 0;
			}	
		}
		if(ghostobject == NULL)
		{
			ghostobject = ent_create("crate.mdl",target,NULL);
			ghostobject.transparent = on;
			ghostobject.passable = on;
			ghostobject.alpha = 30;
		}
		else
		{
			vec_set(ghostobject.x,target);
		}
		wait(1);
	}	

	}
	

}



That only works once... then the ghost disappears and you cant spawn another crate.
Posted By: Claus_N

Re: C-trace, click to place an object - 08/06/09 16:28

You need to set isplaceobject to 1 inside the function smile

Otherwise it will remain 0, and the code in the loop will never be reached anymore, as the while-condition will then be false.
Posted By: binsky33333

Re: C-trace, click to place an object - 08/06/09 16:32

Ok so i actually put. var isplaceobject = 1; inside the actual function. Ok now the crate will stay a ghost forever, and the ghost never disappears. But now instead of placing just 1 crate when i click it places a huge line of like 50 crates coming up all the way towards the camera, not just 1 like its supposed to be doing.
Posted By: Claus_N

Re: C-trace, click to place an object - 08/06/09 16:49

Code:
var isplaceobject;
function spawn()
{
	isplaceobject = 1;

	while(...)
	{
		... // other code



You also need to move the "wait(1);" outside the if-branching, as it will otherwise produce a crash if the trace hits nothing or hits an object from the inside.

I don't know why you're getting more than 1 cubes, maybe you're calling the function more than once at the same time?

Also add a break; or return; here:
Code:
if(ghostobject != NULL)
			{
				ent_remove(ghostobject);
				isplaceobject = 0;
				return;
			}


Posted By: binsky33333

Re: C-trace, click to place an object - 08/06/09 17:23

Here is my code once more.
Code:
entity* ghostobject;
var isplaceobject;
function spawn()
{
   isplaceobject = 1;
	while(isplaceobject != 0)
	{
	temp.x = mouse_pos.x;
	temp.y = mouse_pos.y;
	temp.z = 0;
	
	vec_set(target,temp);
	target.z = 10000;
	
	vec_for_screen(temp,view_build);
	vec_for_screen(target,view_build);
	
	if(c_trace(temp,target,IGNORE_ME | IGNORE_PASSABLE) > 0)
	{
		if(mouse_left)
		{
			ent_create("crate.mdl",target,null);
			if(ghostobject != NULL)
			{
				ent_remove(ghostobject);
				isplaceobject = 0;
				return;
			}	
		}
		if(ghostobject == NULL)
		{
			ghostobject = ent_create("crate.mdl",target,NULL);
			ghostobject.transparent = on;
			ghostobject.passable = on;
			ghostobject.alpha = 30;
		}
		else
		{
			vec_set(ghostobject.x,target);
		}
		
	}	
		wait(1);
	}
	

}



The problem is coming from when i hold down the left mouse buttons they just keep spawning in a huge line all the way up to the camera, then the engine crashes due to too many entities. Is there a way where even if i hold the left mouse button down it will only run that if statement once? Also im running the function by putting spawn(); in another function.
Posted By: Claus_N

Re: C-trace, click to place an object - 08/06/09 17:28

Can you show me the function which calls spawn? smile
Posted By: binsky33333

Re: C-trace, click to place an object - 08/06/09 17:30

Code:
function build_mode()
{
	mouse();
	camera.visible = off;
	view_build.size_x = screen_size.x;
	view_build.size_y = screen_size.y;
	view_build.visible = on;
	player = me;
	view_build.x = -144;
	view_build.y = -144;
	view_build.z = 50;
	view_build.tilt = -30;

	while(1)
	{
		if (mouse_pos.x > screen_size.x - 40 || key_cur == 1 ) 
		{ 
			view_build.y -= 20 * time_step; 
		}
		if (mouse_pos.x < screen_size.x - 740 || key_cul == 1) 
		{ 
			view_build.y += 20 * time_step; 
		}
		if(mouse_pos.y > screen_size.y - 40 || key_cud == 1)
		{
			view_build.x -= 20 * time_step;
		}
		if(mouse_pos.y < screen_size.y - 540 || key_cuu == 1)
		{
			view_build.x += 20 * time_step;
		}
		
	   
		spawn();
		wait(1);
	}
	
}


Posted By: Claus_N

Re: C-trace, click to place an object - 08/06/09 17:38

The problem is, that you're calling the spawn function from within another while loop smile

You either need to call spawn(); from somewhere else (outside a loop), or just change function spawn like this:

Code:
entity* ghostobject;
var isObjectSpawn = 0;

function spawn()
{
	if(isObjectSpawned != 0) {return;}
	
	temp.x = mouse_pos.x;
	temp.y = mouse_pos.y;
	temp.z = 0;
	
	vec_set(target,temp);
	target.z = 10000;
	
	vec_for_screen(temp,view_build);
	vec_for_screen(target,view_build);
	
	if(c_trace(temp,target,IGNORE_ME | IGNORE_PASSABLE) > 0)
	{
		if(ghostobject == NULL)
		{
			ghostobject = ent_create("crate.mdl",target,NULL);
			ghostobject.transparent = on;
			ghostobject.passable = on;
			ghostobject.alpha = 30;
		}
		else
		{
			vec_set(ghostobject.x,target);
		}
		
		if(mouse_left)
		{
			ent_create("crate.mdl",target,null);
			
			isObjectSpawned = 1;
			
			if(ghostobject != NULL)
			{
				ent_remove(ghostobject);
			}
			
			while(mouse_left) {wait(1);}
			
			isObjectSpawned = 0;
		}
	}
}


Posted By: binsky33333

Re: C-trace, click to place an object - 08/06/09 18:22

WOOOO!!!!! smile

Thanks dude soo mucch!!! it finally works! You truly are the best!
Posted By: Claus_N

Re: C-trace, click to place an object - 08/06/09 18:24

I'm glad to hear that it's working now wink
© 2024 lite-C Forums