[SOLVED]empty pointer error

Posted By: JerahYalyn

[SOLVED]empty pointer error - 04/20/15 19:03

I made a simple 3x3 ocean tile by creating a main hmp terrain and adding the action water_prop()

Code:
//create main body of water in the center
ENTITY* water = ent_create("map_models\\water.hmp",vector(0,0,0),water_prop);



and the water_prop action
Code:
action water_prop()
{
        Water_shaders
        //create tile water terrains
	VECTOR* temp1,temp2;
        var water_len;
	//get the length of the 32x32 vertex hmp
	vec_for_vertex(temp1, me, 1);
	vec_for_vertex(temp2, me, 32);
	water_len = vec_dist(temp1,temp2);

	//create 8 more terrain to tile
	ENTITY* water1 = ent_create("map_models\\water.hmp",vector(0,water_len,0),Water_shaders);
	ENTITY* water2 = ent_create("map_models\\water.hmp",vector(water_len,0,0),Water_shaders);
	ENTITY* water3 = ent_create("map_models\\water.hmp",vector(0,-water_len,0),Water_shaders);
	ENTITY* water4 = ent_create("map_models\\water.hmp",vector(-water_len,0,0),Water_shaders);
	
	ENTITY* water5 = ent_create("map_models\\water.hmp",vector(water_len,water_len,0),Water_shaders);
	ENTITY* water6 = ent_create("map_models\\water.hmp",vector(-water_len,water_len,0),Water_shaders);
	ENTITY* water7 = ent_create("map_models\\water.hmp",vector(water_len,-water_len,0),Water_shaders);
	ENTITY* water8 = ent_create("map_models\\water.hmp",vector(-water_len,-water_len,0),Water_shaders);
}



It returns error W1501 Empty pointer in water_prop.

I run the code line by line by adding the line one by one and I get the error when I add vec_for_vertex(temp2, me, 32).
however when I one or two water hmp error doesn't show unless I add another hmp. Which kinda confuses me. without adding hmp error is with vec_for_vertex and when I add more than 2 error is on the next ent_create. Can anyone enlighten me?

thanks!
Posted By: Superku

Re: empty pointer error - 04/20/15 19:10

You are using invalid VECTOR* pointers, i.e. temp1 and temp2. Just remove the * and you will be fine, and make sure you understand why and where the difference lies!
Posted By: JerahYalyn

Re: empty pointer error - 04/20/15 19:28

Woops, forgot it was local. Sorry about that. XD
Posted By: Superku

Re: empty pointer error - 04/20/15 19:38

(Writing VECTOR* locally or globally, meaning outside of the function, doesn't make any difference here, if that is what you mean.)
Posted By: JerahYalyn

Re: empty pointer error - 04/21/15 02:28

tongue then what is the difference of pointer VECTOR and normal vector. Strange because even though the pointer is invalid it was able to align the hmp for the first two entity before realizing the pointer was invalid.
Posted By: Anonymous

Re: empty pointer error - 04/21/15 02:46

I will take a guess, VECTOR* is a point to a struct VECTOR is a copy of a struct and vector() is a engine function.

Also VECTOR is not a object, I think like a ENTITY* or PANEL*
Posted By: JerahYalyn

Re: empty pointer error - 04/21/15 04:08

Nevertheless it works now. Except for a small issue that the terrain disapear when I leave the main water hmp.
Posted By: Redeemer

Re: empty pointer error - 04/21/15 05:53

Originally Posted By: JerahYalyn
Strange because even though the pointer is invalid it was able to align the hmp for the first two entity before realizing the pointer was invalid.

This is not anything you should read into. Passing uninitialized references (pointers) as value arguments is a terrible mistake that can result in all kinds of unusual behavior, though it usually manifests as a simple crash like what you experienced. Also, any better compiler would've smacked you for that, but Lite-C doesn't because it's a bit shite.

And if you don't understand the difference between a normal variable and a pointer, I implore you to read about it and figure it out, not only because pointers are extremely important and useful in their own right, but because the difference between VECTOR and VECTOR*, var and var*, and int and int* is huge and you will run into countless problems if you use them interchangeably like that.
Posted By: Florastamine

Re: empty pointer error - 04/21/15 13:04

A pointer is not the same as a normal variable. A pointer points to something (variables). You can reference to the variable's value through that pointer.

A pointer contains the address of the variable it points to. While a variable contains the actual value.

So, your VECTOR * points to nothing, and thus yield "W1501 Empty pointer". You have to create a VECTOR struct and make the pointer points to that struct:
Code:
VECTOR v;
v.x = 5;
v.y = 2;
v.z = 1;

VECTOR *vp = &v; /* makes vp points to v */
<...>
vec_set(vp, ..);
vec_add(vp, ..);
<...>



Or, use the pointer directly:
Code:
VECTOR *vp = malloc(sizeof(VECTOR)); /* Allocates the memory needed for a VECTOR struct where vp will point to */
vec_set(vp, vector(5, 2, 1));



grin
Posted By: JerahYalyn

Re: empty pointer error - 04/23/15 19:26

I don't want to open another topic for another problem. Any one can tell me what I'm doing wrong in using c_scan? Basically I run c_scan to and try to get the nearest vertex to the contact point and get its coordinates. I'm scanning a terrain hmp.

my code for scanning terrain with c_trace
Code:
var vi;
void camera_scan()
{
        
	VECTOR tempv,temp;
	while(1)
	{
                
		if(mouse_left) //scan while left mouse if pressed
		{
		   tempv.x = mouse_pos.x;
		   tempv.y = mouse_pos.y;
		   tempv.z = 2000;
		   vec_for_screen(tempv,camera);
		   c_trace(camera.x, tempv, IGNORE_ME);
                   //get vertex coordinate with vec_for_vertex
		   vec_for_vertex(temp,hit.entity,hit.vertex); //if fails to return hit.vertex
	           vi = hit.vertex; //store it for panel
                  //Debugging draw point of contact and nearest vertex then draw a line between.
                  draw_point3d(temp.x,vector(0,0,255),100,300);
	          draw_point3d(vector(hit.x,hit.y,hit.z),vector(0,0,255),100,300);
                  draw_line3d(vector(hit.x,hit.y,hit.z),vector(0,0,255),100);
                 draw_line3d(temp.x,vector(0,0,255),100);
			
		}
		wait(1);
	}
}



The problem is with hit.vertex returning nothing and vec_for_vertex always returns the coordinate of the first vertex regardless of the hit position. What am I doing wrong?

here is how it look in my test. Ignore the float in vertex index, I forgot to change the format. But clearly vertex index never updated.

Posted By: Anonymous

Re: empty pointer error - 04/23/15 19:41

c_trace(......,IGNORE_ME | USE_POLYGON);
is my first idea. You're hitting the BBOX
Posted By: rayp

Re: empty pointer error - 04/23/15 19:43

Try2 trace with
Code:
USE_POLYGON

flag set ( maybe activate POLYGON flag of your terrain / model ) and / or set the
Code:
SCAN_TEXTURE

flag in the c_trace command too.
But if iam right now, SCAN_TEXTURE is only need if u want2 add decals 4ex.

Greets
Posted By: JerahYalyn

Re: empty pointer error - 04/23/15 20:05

I actually use USE_POLYGON first and set terrain flags to POLYGON before changing my code since it still returns the sams result. In the image I provided, the hit position is actually flat in the terrain surface. Thd only thing I haven't tried is SCAN_TEXTURE. I shut the PC down after 2hours of trying.
Posted By: Anonymous

Re: empty pointer error - 04/23/15 20:29

HI, Shouldn't you use vec_rotate(tempv,camera.pan) then c_trace. I think you are tracing straight down.

But I will stop guessing.
Posted By: 3run

Re: empty pointer error - 04/23/15 21:29

You can trace, then loop through all terrain vertices and get closest one to the hit position, or you can use 'ent_getvertex' with 'hit.vertex', but it won't be accurate. Anyway if you want to access 'hit' parameters you need to use SCAN_TEXTURE, it won't work other ways. You don't need to use USE_POLYGON and POLYGON flag for terrain, it's not a model and it will work correctly without them! Terrain already has POLYGONAL hull, it doesn't have BBOX or elliposid hull.

I'll post working example tomorrow, I can't do that right now via my cellphone.

Problem with 'hit.vertex' returning zero in your example only related to SCAN_TEXTURE, you can't access 'hit' parameters without using it!

Best regards
Posted By: rayp

Re: empty pointer error - 04/23/15 21:49

Quote:
HI, Shouldn't you use vec_rotate(tempv,camera.pan) then c_trace. I think you are tracing straight down.
Good idea. Maybe c_trace vectors are messed, right...did not look how he calculated the from and to vectors of the trace.

Try this ( just written not tested )
Code:
var hitvertex_global = 0; // global var holding last hit vertexnr
/*
// not sure if a terrain sets the YOU pointer when hit by c_trace. if not use something like this ( or hit.entity )
// and simply change "YOU" below to "terrainEnt"
ENTITY* terrainEnt;
action myTerrain_WED(){ // apply2 terrain in WED
   terrainEnt = me;
}
*/
action / function whatever(){
        ...
        ...
        hitvertex_global = 0;
	VECTOR _temp;
	_temp.x = mouse_pos.x;
	_temp.y = mouse_pos.y;
	_temp.z = 0;
	vec_set (target, _temp);
	target.z = 10000;
	vec_for_screen (_temp,  camera);
	vec_for_screen (target, camera);
        trace_mode = IGNORE_ME | IGNORE_PASSABLE;
	c_trace (_temp, target, trace_mode);
        if (HIT_TARGET){                        // sure "hit" pointer was set ( =hit something ) ?
           hitvertex_global = hit.vertex;        
           if (hitvertex_global && you){   // vertex? you?
              VECTOR _p;
              vec_for_vertex (_p, you, hitvertex_global);
              ent_create ("example.mdl", _p, NULL);
           }
         //same as ent_create ("bla.mdl", target, NULL); so theres no need4 vec_for_vertex in my quick example ^^
      }
        
}



Edit:
Besides...
After the c_trace check if you hit something ( entity 4ex ). Dont do:
Code:
c_trace ( ... );
you.health -= 10;

may Crash with "empty pointer". Do it like this instead
Code:
c_trace ( ... );
if (you) you.health -= 10;

In your case "hit" - may cause "empty pointer" error sometime, if i take a wild guess. ^^

Greets
Posted By: JerahYalyn

Re: empty pointer error - 04/24/15 04:15

I think vec_rotate is not needed when using vec_for_screen. If I use it the ray tracer will rotate away from where I'm actually pointing. I tried your code rayp but I doesn't make any difference, there is still no hit.vertex returned.

@3run, Thanks for pointing that out to me man. I was able to fix it by using ent_nextvertex. I need to keep the GS manual open all the time for the list of all the functions.
new code.
Code:
var vert; //to store vertex index next to hit position
void camera_scan()
{
	VECTOR tempv,temp;
	while(1)
	{
		if(mouse_left) 
		{
			tempv.x = mouse_pos.x;
			tempv.y = mouse_pos.y;
			tempv.z = 2000;
			vec_for_screen(tempv,camera);
			c_trace(camera.x, tempv, IGNORE_ME | SCAN_TEXTURE);
			
			if(HIT_TARGET) //needed to prevent crashing when not hitting something :P
			{
				
				vert = ent_nextvertex(hit.entity,hit.x);
				vec_for_vertex(temp,hit.entity,vert);
				
				//for debugging, create a point and line from the hit  position to the nearest index
				draw_point3d(temp.x,vector(0,0,255),100,300);
				draw_point3d(vector(hit.x,hit.y,hit.z),vector(0,0,255),100,300);
				draw_line3d(vector(hit.x,hit.y,hit.z),vector(0,0,255),100);
				draw_line3d(temp.x,vector(0,0,255),100);
			}
		}
		wait(1);
	}
}



thanks for helping me out rayp and malice.
Now I just have to figure how to get all the the vertex near the hit position at a given radius without looping through each indices. I use Markus's real time terrain deforming library in A7 but it no longer work for A8 so I have to make my own function for terraform.
Posted By: 3run

Re: empty pointer error - 04/24/15 07:11

Originally Posted By: JerahYalyn
I think vec_rotate is not needed when using vec_for_screen. If I use it the ray tracer will rotate away from where I'm actually pointing. I tried your code rayp but I doesn't make any difference, there is still no hit.vertex returned.
I don't think 'vec_rotate' is needed for a line tracing from camera to the mouse position too. Cause mouse position will be infront of the camera anyway.

Originally Posted By: JerahYalyn
I actually use USE_POLYGON first and set terrain flags to POLYGON before changing my code since it still returns the sams result.
About USE_POLYGON and POLYGON flag for terrains, I couldn't post this via cellphone, but take a look at this:

So once again, I want to aware you to do not use such SLOW things when they aren't needed.

Originally Posted By: JerahYalyn
@3run, Thanks for pointing that out to me man. I was able to fix it by using ent_nextvertex. I need to keep the GS manual open all the time for the list of all the functions.

Now I just have to figure how to get all the the vertex near the hit position at a given radius without looping through each indices. I use Markus's real time terrain deforming library in A7 but it no longer work for A8 so I have to make my own function for terraform.
There was an example of terrain deforming somewhere around the forum:
Terrain Deform realTime
[Contest entry] DEFORM TERRAIN

Ask people in those threads, maybe someone still have any of those demos and can upload them for you.

About the working example I've made yesterday, here it is:
Code:
#define USE_FOR_LOOP

void main(){
	
	fps_max = 60;
	
	level_load("");
	
	BMAP* skinBmp = bmap_createblack(64, 64, 24);
	bmap_fill(skinBmp, COLOR_WHITE, NULL);
	
	
	ENTITY* terrainEnt = ent_createterrain(NULL, nullvector, 6, 6, 24);
	ent_setskin(terrainEnt, skinBmp, 0);
	
	d3d_lines = 3;
	
	vec_set(camera.x, vector(-180, 77, 166));
	vec_set(camera.pan, vector(339, -49, 0));
	
	mouse_mode = 4;
	
	VECTOR tempVec, vertexVec;
	
	var total_vertices = ent_vertices(terrainEnt);
	var hit_vertex = 0;
	
	int i = 0;
	
	while(terrainEnt){
		
		DEBUG_VAR(total_vertices, 10);
		DEBUG_VAR(hit_vertex, 40);
		
		vec_set(tempVec, mouse_dir3d);
		vec_scale(tempVec, 1000);
		vec_add(tempVec, mouse_pos3d);
		
		if(c_trace(mouse_pos3d, tempVec, IGNORE_PASSABLE + SCAN_TEXTURE) > 0){
			
			hit_vertex = hit.vertex;
			
			draw_point3d(hit.x, COLOR_RED, 100, 3);
		}
		
		if(HIT_TARGET && you && ent_type(you) == 4){
			
			#ifdef USE_FOR_LOOP
				
				for(i = 1; i < total_vertices + 1; i++){
					
					vec_for_vertex(vertexVec, terrainEnt, i);
					
					if(vec_dist(vertexVec.x, hit.x) < 10){
						
						CONTACT* contact = ent_getvertex(terrainEnt, NULL, i);
						contact.z += time_step;
						contact.v = NULL;
						ent_setvertex(terrainEnt, contact, i);					
						
						draw_point3d(vertexVec.x, COLOR_GREEN, 100, 5);
					}
				}
				
			#else
				
				CONTACT* contact = ent_getvertex(terrainEnt, NULL, hit_vertex);
				
				draw_point3d(contact.x, COLOR_GREEN, 100, 5);
				
				contact.z += time_step;
				contact.v = NULL;
				ent_setvertex(terrainEnt, contact, hit_vertex);					
				
			#endif
		}
		
		wait(1);
	}
}



If I remember correctly in most of those terrain deforming demos, they took 'hit' position and the nearest vertex, but then they create a square or circle around it, via some simple math calculations. I can't really help at this point, cause I've never made terrain deformation tools. You can also ask sivan if he is in a good mood, he might help you with this, since he is working on his MapEd for a few years now.

Originally Posted By: JerahYalyn
I use Markus's real time terrain deforming library in A7 but it no longer work for A8 so I have to make my own function for terraform.
BTW, why doesn't it work? Crashes?

Best regards!
Posted By: JerahYalyn

Re: empty pointer error - 04/24/15 09:03

Thanks again man, exactly what I need. I`ll look in to NeoNaper's code to learn more about terraforming. But what I really need was the one Markus made.
The reason why it didn't work is because I used the A7 version of the DLL on A8 not knowing that there is a A8 version. Luckily Sivan uploaded the a mirror of the file.
I search the forum but it didn't showup for some reason. I also want to look into the Terrain Paint by Neo but the link is broken so I have to look somewhere else.

Posted By: 3run

Re: empty pointer error - 04/24/15 09:14

I've looked through my HDD for those terrain deformation and painting demos (from NeoNeper), and luckely I've found them! In the archive you can also find 'saveLoad.c' file, with two function, one to save terrain vertex positions into the .txt file, and another one to load them back (this was done before save hmp file dll was created and contributed by Wjbender).

Best regards!
© 2024 lite-C Forums