Structs?[solved]

Posted By: peteredlin

Structs?[solved] - 12/30/10 13:08

Hello for a project i want to use a struct to load my function.
But how do i make the function accept differrent structs of the (same type of course)

All help is welcome

Posted By: DJBMASTER

Re: Structs? - 12/30/10 13:38

Hi, all you need to do is pass a pointer to a structure into your function...
Code:
typedef struct
{
	var ammo;
	var reload_rate;
} 
Gun;

function ReloadWeapon(Gun* gun)
{
	gun -> ammo = 100;
}

void main()
{
	Gun new_gun;
	ReloadWeapon(&new_gun);
}


Posted By: Rei_Ayanami

Re: Structs? - 12/30/10 13:43

Put a id variable as the first variable of all your different structs.

Than, take the void in the function like this:

void yourFunction(void* structVoid)
{

Copy the the first byte into a temponary struct, to identfy the struct.

INDENTSTRUCT* newIdent = sys_malloc(sizeof(INDENTSTRUCT));

memcpy(newIdent, structVoid, 4); //in case you are using a var, as it has 4 bytes

if(newIdent.id == 1) //Your first Struct
{
YOURFIRSTSTRUCT* tempStruct;
memcpy(tempStruct, structVoid, sizeof(YOURFIRSTSTRUCT));

the same goes for id two and so on laugh

Hope this helps laugh
Posted By: peteredlin

Re: Structs? - 12/30/10 15:36

why does the following code not work then?

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

///////////////////////////////

typedef struct
{
	ENTITY* entity;
	char filename;
	VECTOR* position;
} simple_ent;



function Create_ent(simple_ent* ent)
{
	ent.entity = ent_create(ent.filename,ent.position,NULL);
	

}




ENTITY* box;

function main()
{
 	simple_ent cube;
	cube.filename = "cube.mdl";
	cube.position=vector(0,0,0);
 	
 	physX_open();
 	level_load("level.hmp");
 	Create_ent(cube);
 	//while(1)
 	//{
 	//box.x+=0.001;
 	//wait(1);
 	
 	//}
 	
 	
}



I get an error that the file cant be opened
Posted By: Rei_Ayanami

Re: Structs? - 12/30/10 15:41

it fails here:

cube.position=vector(0,0,0);
Posted By: MasterQ32

Re: Structs? - 12/30/10 16:10

try char* instead of char
Posted By: Lukas

Re: Structs? - 12/30/10 16:11

There are multiple errors in that code.
Firstly, you defined your filename string wrong. "char filename;" defines only one character. You have to use "char* filename;" to create a char array.


Secondly, with "cube.filename = "cube.mdl";" you just create a temporary char array. (This only works when initialising a char array while defining it, but that might not even work in Lite-C either.) It will be invalid in the next line. This is how you should to it:
Code:
cube.filename = malloc(sizeof(char)*number_of_characters_you_need_plus_one);
strcpy(cube.filename,"cube.mdl"); // strcpy, not str_cpy




For your vector, better use VECTOR, instead of VECTOR*. Then you can just set it like this:
Code:
cube.position.x = 0;
cube.position.y = 0;
cube.position.z = 0;


Posted By: peteredlin

Re: Structs? - 12/30/10 20:25

Thank you very much!

Now it works, learned some again blush
Posted By: Lukas

Re: Structs? - 12/31/10 11:02

You're welcome.

But please note that I made a little error in my code. It should be sizeof(char) instead of sizeof(char*). This way four times the nececcary memory was allocated.
© 2024 lite-C Forums