entitys and structures

Posted By: er_willy

entitys and structures - 02/22/07 10:09

Any know some mode for what one entity pointer and one structure using the same name/pointer. Is it possible?

crapy example:
typedef struct
{
int height;
int weigh;
char name;
......
}PLAYERS;

PLAYERS* player_01_stru;

ENTITY* player_01_ent;

//unknow code what make it, what player_01_stru and player_01_ent become
//in player_01 and can us make this:

player_01.pan = 90
player_01.weigh = 45;
Posted By: Excessus

Re: entitys and structures - 02/22/07 10:39

I don't think that's possible.

You could, however, put an entity in the struct and refer to it like this:
player_01_str.ent.pan = 90;

I'm not sure it's possible to add member functions to structs in lite-c, but if you can, you could also implement a method setPan() that sets the pan of the entity that is inside the struct.
Posted By: er_willy

Re: entitys and structures - 02/22/07 10:46

thaks, testing...
Posted By: er_willy

Re: entitys and structures - 02/22/07 11:20

ok many thanks, work!!!

sorry can you expand this ?
you could also implement a method setPan()



for the lovers of copy&paste
//-------------------
typedef struct
{
int height;
int weigh;
char name;
ENTITY* player_01_ent;
}PLAYERS;

//Reset variable from structures
PLAYERS* player_01_str = { height = 0; weigh = 0; name = "NULL"; }

//testing entitys on structures
function ball_life()
{
player_01_str.player_01_ent = my;

player_01_str.player_01_ent.flags |= TRANSLUCENT;//work!!!
}


//in main function

ent_create("earth.mdl",vector(0,0,100),ball_life);
Posted By: Excessus

Re: entitys and structures - 02/22/07 11:25

Code:

//-------------------
typedef struct
{
int height;
int weigh;
char name;
ENTITY* player_01_ent;
void setPan(var newPan) {player_01_ent->pan = newPan;}
} PLAYERS;

//Reset variable from structures
PLAYERS* player_01_str = { height = 0; weigh = 0; name = "NULL"; }

//testing entitys on structures
function ball_life()
{
player_01_str.player_01_ent = my;

player_01_str.setPan(90);
}



This works in C++ (not tested, but it should work), but I'm not sure this is allowed in lite-C or even C..

You might think the syntax of struct.entity.pan = value is easyer, but personally I prefer the struct.setMember(value) syntax because it allows you to check the input. Basically you can set rules INSIDE the object (not everywhere you use it) how it may be used. For example, if you only want pan values 0 to 180 to be allowed, you could add a check for that in the setPan() function.
Posted By: er_willy

Re: entitys and structures - 02/22/07 11:29

many many thaks, testing...
Posted By: er_willy

Re: entitys and structures - 02/22/07 11:38

function on structures... I thik what not work on c, only work in class(c++). not?

Lite-c get error.

I test this. but same error
void setPan(var newPan) {player_01_str.player_01_ent.pan = newPan;}

I agree with you, function on structures is a good idea.
Posted By: Excessus

Re: entitys and structures - 02/22/07 12:11

In C++, you can also add methods (member functions) to structs (not just classes) but I think this is indeed a C++ addition to structs, and not a C feature..

One thing that you could probably do, but would make it a little uglyer:
Add a function pointer to your struct. Always initialize this pointer to point to a global function, that takes an instance of that struct, and the parameter to set.
Code:

// In the struct, named "somestruct":
void (*setPan) (somestruct, var);

// Global function:
void somestructSetPan(somestruct thisStruct, var newPan)
{
thisStruct.ent.pan = newPan;
}

// Before you can use the function pointer, you must initialize it:
somestructInstance.setPan = somestructSetPan;

// Now just call it:
somestructInstance->setPan(someStructInstance, 90);



Note that all this takes quite a bit of setting up, and you might forget to do something. So what you should do is create a function that sets all members to an initial value (and sets the function pointers correctly). Basicly the equivalent of a C++ constructor.

You could also leave out the function pointer, and just call the global function with the struct and the new pan value.

Again, all this is not needed because the struct.ent.pan also works, but in large projects with large and complex structs, doing object oriented programming is really beneficial.
Posted By: jcl

Re: entitys and structures - 02/22/07 17:28

You can have methods in structs, but only for interfacing external APIs. You can not define your own methods, although this could relatively easily be added in a future lite-C version.
Posted By: FBL

Re: entitys and structures - 02/22/07 17:45

That would be pretty cool.
Would it also be easy to implement a this pointer like in C++ then?
Posted By: HeelX

Re: entitys and structures - 02/22/07 17:57

Quote:

You can have methods in structs, but only for interfacing external APIs. You can not define your own methods, although this could relatively easily be added in a future lite-C version.




Oh, please do that, please add methods. I am longing for this..
© 2024 lite-C Forums