[help] with functions

Posted By: DLively

[help] with functions - 07/30/14 05:20

Code:
function basic_effect(var* var_num){
	VECTOR effect_spot;
	vec_for_vertex(effect_spot, me, var_num);
	effect(wings_Effect_base, 2, effect_spot, nullvector);	
	return(var_num);
}
action tempact(){
	while(1){wait(1);
		basic_effect(21);
		my.pan += 15 * time_step;
	}
}



Why doesn't this work? If I just place the effect in the action, it obviously works - but not if I place it in a function...

laugh
Posted By: Kartoffel

Re: [help] with functions - 07/30/14 08:09

because 'me' get's lost?
pass it to the function as pointer.
Posted By: Superku

Re: [help] with functions - 07/30/14 10:14

You don't want to have a var POINTER as a function argument but just a regular var, so remove the *.

The me is "transmitted" to the called function, that's not a problem. However, for a cleaner code you should use an ENTITY* pointer and replace me/ my as Kartoffel suggested.
Posted By: Kartoffel

Re: [help] with functions - 07/30/14 10:15

Originally Posted By: Superku
You don't want to have a var POINTER as a function argument but just a regular var, so remove the *.

The me is "transmitted" to the called function, that's not a problem. However, for a cleaner code you should use an ENTITY* pointer and replace me/ my as Kartoffel suggested.
Oh I didn't know that me is passed automatically, thanks.
Posted By: DLively

Re: [help] with functions - 07/30/14 15:53

THANKS GUYS laugh

Quote:
pass it to the function as pointer.

Thats a good idea - I didnt see that. Although as Superku said its not required, it will be usefull later on down the road wink

Quote:
You don't want to have a var POINTER as a function argument but just a regular var, so remove the *.

Thank you! That solved it right there laugh

A quick read in the manual has helped me distinguish the difference between the two laugh

Again, thanks guys!
Posted By: Quad

Re: [help] with functions - 07/30/14 17:22

Lite-c actually causes people to confuse the two in the long run because it has automatic pointer detection stuff, like you do not have to use & operator the get the address while passing a parameter, lite-c does that for you etc. Better use #define PRAGMA_POINTER after some point(once you are familiar with pointers) to have cleaner code(by cleaner i mean more explicit) and actually have better understanding of what you are doing.

from manual:
Quote:
#define PRAGMA_POINTER
Switches off the lite-C pointer autodetection, and treats pointers as in C/C++. The address operator (&) must be used for passing addresses to functions, and the -> operator must be used for elements of a struct pointer. Otherwise a syntax error will be issued.

Posted By: DLively

Re: [help] with functions - 07/30/14 18:28

The problem with #define PRAGMA_POINTER is that it causes the includes to have pointer issues such as mtlfx.c

Furthermore, i get an issue with: vec_set(mouse_pos,mouse_cursor); (If I comment out the mtlfx.c since im not currently using it)

I'd like to indulge into this more, thus some further help with this would be appreciated.

What should I be replacing the 'comma' with to correct this error?
Posted By: Quad

Re: [help] with functions - 07/30/14 20:32

Okay. That's because the signature of the vec_set is:

vec_set ( VECTOR* vector1, VECTOR* vector2)

You see, vec_set expects both parameters to be VECTOR*(VECTOR pointer). But mouse_pos and mouse_cursor are defined as VECTOR and not VECTOR* in avars.h (which is included in acknex.h)

This means they are the VECTOR structs themselves and not pointers to these structs. You need pointers to these structs so you use the reference operator which is &.

&mouse_pos means "address of mouse_pos" which gives you what you need = a pointer to mouse_pos.

so you do

vec_set(&mouse_pos,&mouse_cursor);


#define PRAGMA_POINTER makes pointers in lite-c more or less work as the same way as in c/c++.

I will give you the short version but read up on pointers in C for further explanation as to why this is required or why it works this way in C:

--
I actually wrote some stuff there but i realized it was more confusing than helping without more background knowledge on C and pointers and stack and heap and how memory works. So, read up on them if you want more details. In fact do read about them, it will almost definitely be helpful in the future.

--

EDIT: not sure about the mtlfx part, it could be that it's written with the assumption that PRAGMA_POINTER is not defined.




Posted By: DLively

Re: [help] with functions - 07/30/14 21:12

Thanks for your help, Quad laugh

This is a very interesting side of programming. I've got a book on my desk for c++ that I've been meaning to read laugh

I can see I am going to run into many script 'errors' on my way into this, and the current project I am working on already has 10000+ lines of code - thus I am not sure I want to experiment with this, with my project without proper knowledge or background.

However, I do intend on using this method with my next project (not a clue what it is yet XD) But I'll feel safer doing it that way, rather than mucking up my current project, or losing time on it.

Thanks again, friend laugh
© 2024 lite-C Forums