pointer problems.

Posted By: FoxHound

pointer problems. - 10/05/12 21:28

I have created a dashboard which can display the same type of information about different objects when they are selected.

Imagine a board that show the 0-60 and top speed of cars, and when you selected that car it would pop up on that board.

I do this by when you push a button it calls a function with calls another function which puts the proper vars to be displayed and those are ran through pointers of the function.

Ex

void create_information_display_fastcars(var* 060speed, var* topspeed, var* value)

Now the function works just fine and the information displays shows the correct information. Now I want to mod that information. I create a few vars pointers that I want to mod in another function when i push the up and down arrows. However this is where my function starts working.

I took a look at and went along the same lines as this
http://rapidpurple.com/blog/tutorials/c-...-of-a-variable/

but I can not get it to work. I thought it might be the digits that displays the number so i put up another to show the actual value of that number without any pointer action but nothing seems to change.

Anyone have any idea?
Posted By: Superku

Re: pointer problems. - 10/05/12 22:03

It's obviously difficult to guess your problem without having seen any code, but the following example works fine (I assume it's something you are aiming for):

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
///////////////////////////////

var speed = 50;

void change_speed(var* spd, var value)
{
	*spd = value;
}

void main()
{
	fps_max = 60;
	while(1)
	{
		if(key_space) change_speed(&speed, total_ticks);
		DEBUG_VAR(speed,20);
		wait(1);
	}
}

Posted By: FoxHound

Re: pointer problems. - 10/05/12 22:53

Here is the main function that does most of the work
Code:
void create_information_display_seller(var* speed_v, var* health_v, var* damage_v, var* money_v, BMAP* bmap_b)
{
	
	if(information_display_seller)pan_remove(information_display_seller);
	
	//////////////////////////////
	// now we use the pointers to use the buttons on
	//////////////////////////////
	seller_health 	= *health_v;
	seller_damage 	= *damage_v;
	seller_money  	= *money_v;
	seller_speed 	= *speed_v;
	//in another function seller_health, damge,money and
 //speed will be altered and should affects the totals of the
 //original vars that were passed into this function
	
	


	information_display_seller = pan_create("",5);
	information_display_seller.pos_x = 672;
	information_display_seller.pos_y = 5;
	
//displays the image of the item selected
	pan_setbutton(information_display_seller,0,1,
	0,
	0,
	bmap_b,
	bmap_b,
	bmap_b,

	NULL,
	NULL,//function goes here
	NULL,
	NULL);
	
	

	

	pan_setdigits(information_display_seller,0,150,115,"%2.f",font_create("Arial#25"),1,seller_speed); 
	
	pan_setbutton(information_display_seller,0,1,
	235,
	115,
	UPARROW,UPARROW,UPARROW,

	NULL,
	alter_seller_speed_up,//function goes here
	NULL,
	NULL);
	
	
	pan_setbutton(information_display_seller,0,1,
	270,
	115,
	DOWNARROW,DOWNARROW,DOWNARROW,

	NULL,
	alter_seller_speed_down,//function goes here
	NULL,
	NULL);
	

	
	
	
	set(information_display_seller,SHOW);
	
}



What I want to do is pass a variable into this function and assign that variable to a pointer so that variable can be manipulated later on.
Posted By: Superku

Re: pointer problems. - 10/05/12 23:04

I'd say you scrap this approach and define a struct for your purpose that holds all the necessary information. Then use a linked list or an array of your struct instances and draw the information dynamically without panels using draw_text or draw_obj + text object.
Posted By: FoxHound

Re: pointer problems. - 10/05/12 23:08

problem solved.

seller_health = health_v;
seller_damage = damage_v;
seller_money = money_v;
seller_speed = speed_v;

fixed my problem. So there did not need to be a * in front of health_v and the others.
Posted By: FoxHound

Re: pointer problems. - 10/05/12 23:15

Originally Posted By: Superku
I'd say you scrap this approach and define a struct for your purpose that holds all the necessary information. Then use a linked list or an array of your struct instances and draw the information dynamically without panels using draw_text or draw_obj + text object.


Every last bit of that will take a lot more resources (during game play) and coding then my method. I knew it was a simple error on my part somewhere in my code as altering a number through a pointer is on almost every "how to learn c" site out there, my only thing is I wanted to put a middle man in there and messed up something simple, which was trying to move pointer value instead of pointer address.
Posted By: Superku

Re: pointer problems. - 10/05/12 23:20

Quote:
Every last bit of that will take a lot more resources (during game play) and coding then my method.

It probably wouldn't, draw_quad is f.i. faster than using panels, but good that you've found the solution to your problem.
Posted By: FoxHound

Re: pointer problems. - 10/05/12 23:27

not draw_quad but

Code:
define a struct for your purpose that holds all the necessary information. Then use a linked list or an array of your struct instances



would vs using basic vars and some pointer fun. And yes the problem is solved. I put it together for everything and it works just fine. I'm using this for a tool to make my real game. Wow I never knew I would need to make so many tools.
Posted By: Kartoffel

Re: pointer problems. - 10/06/12 10:18

if you want to change a var in another function do it like this:

Code:
void set_a_to_b(var* a, var b)
{
	a[0] = b;
}


You have to give an array index. (the var a which is passed to the function doesn't have to be an array!)
I'm not sure why it works or either how I foud out how It works - but it does. grin
Posted By: txesmi

Re: pointer problems. - 10/06/12 10:56

crazy
[offtopic]
it would be better treating the pointer as a pointer

Code:
*a = b;


[/offtopic]
Posted By: Kartoffel

Re: pointer problems. - 10/06/12 11:58

well.. I have never really understood what pointers are and how to use them properly... smirk
Posted By: FoxHound

Re: pointer problems. - 10/06/12 16:13

There are not a lot of reasons you would want to use a variable as a pointer. In fact this is the first time I have ever needed to and this is getting on 10 years of programming for me. However now that I know I might find it more useful and use it far more often.
Posted By: WretchedSid

Re: pointer problems. - 10/06/12 17:51

There are tons of reasons to use a var as pointer, function calls by ref or by value are just one of them.
Guys, pointers are your bread and butter when working with C, and they will break your neck when you don't get them right (they will still do when you can get them right, but at least you can debug it then). So learn your goddamn pointers!
Posted By: FoxHound

Re: pointer problems. - 10/06/12 18:43

Other than the two examples provided, what else could they help with? Even the function call could be replaced with having the function return the value. I am being serious as I have not had a need for them before now.

It is quite helpful with my dashboard as it allows me to save about 1000 lines of code and makes it far more dynamic when using other items.
Posted By: Sajeth

Re: pointer problems. - 10/06/12 19:03

When you need to change two variables with a single function call, you're forced to use pointers.
Posted By: FoxHound

Re: pointer problems. - 10/07/12 17:16

That is true. I was not thinking of vars passed into functions, there are dozens of built in functions in 3dgs that we use all the time. I am more thinking of odd ways to use them, such as my dashboard example.
© 2024 lite-C Forums