Usage of structs?

Posted By: MrCode

Usage of structs? - 08/03/07 05:55

I know what a struct is, I just don't know how I would use it in a script. Can someone point me in the right direction here?

Well, I did write this small test code to test my knowledge of the syntax:

Code:

#include <acknex.h>
#define PRAGMA_ZERO

var showint1;
var showint2;

typedef struct
{
int test1;
int test2;
char test3[10];
} TEST;

TEST* here_we_go=
{
test1= 7;
test2= 7;
test3= "Hello, World!";
}

FONT* sys_font= "Terminal#12";

PANEL* show_struct_values=
{
digits (320,240,"Struct value 1= %9.0f",sys_font,1,showint1);
digits (320,250,"Struct value 2= %9.0f",sys_font,1,showint2);
flags= visible;
}

void exit_()
{
sys_exit(NULL);
}

function main()
{
screen_color.red= 1;
video_mode= 8;
video_screen= 1;
on_esc= exit_;
showint1= here_we_go.test1;
showint2= here_we_go.test2;
}



but you could just as easily define the vars and display them with the panel.

EDIT: Oops! Sorry, wrong forum! I thought I was in Lite-C Scripting! Could someone move this?
Posted By: JibbSmart

Re: Usage of structs? - 08/03/07 06:28

i used structs to create a "QUATERNION" type which can be passed to quaternion functions that i wrote. it's much easier than handling var array's, and much more intuitive to work with (if you know what you're doing with quaternions, my_quat.w/x/y/x makes much more sense than my_quat[0/1/2/3]).

i also used structs to create a "PHYS_ENT" type. these contained a pointer to an entity; two quaternions (containing orientation and angular velocity); a velocity vector; floats for mass, elasticity, coefficient of friction, moment of inertia; and lastly a PHYS_ENT pointer called "next_phys".

this helped in making my own custom physics engine. the pointer to "next_phys" allowed me to use linked lists to keep track of which PHYS_ENT's were enabled and which were disabled. it also meant i could create force-applying functions (and similar) that only need to be passed a force, position, and pointer to a PHYS_ENT, and then the function knows how to use and change each of the parameters within the PHYS_ENT struct.

additionally, every object in lite-C -- ENTITY, VIEW, PANEL, MATERIAL, PARTICLE, VECTOR, ANGLE etc -- is a struct. ENTITY contains things like var skill[100], ANGLE pan, VECTOR x, and all those other things that belong to an entity. if you need other stuff, create a struct. i created PHYS_ENT because you can't access local variables (or local structs) from one action in another, so i gave PHYS_ENT everything extra that other PHYS_ENTs would need access to.

linked lists are very useful. they are a bit like arrays except they can change in length, and new members can be stuck in anywhere without affecting others.

for example, i had a PHYS_ENT called phys_enabled, and another called phys_disabled. i also had an array of 128 global "phys_ent"s. at game start, phys_disabled's pointer "next_phys" was pointed to the "phys_ent[0]". a loop made each phys_ent point to the next one (don't confuse phys_ent, the array, with PHYS_ENT, the type of the array). any PHYS_ENT in the "phys_disabled" list could be found by checking a PHYS_ENT's next_phys, and then checking their next_phys, and so on and so forth.

i had a function that gets called to enable a physics entity, and that would do so by just grabbing the first PHYS_ENT in the phys_disabled list (after the actual "phys_disabled" PHYS_ENT, of course), pointing it to the ENTITY i wanted to enable and putting it in the phys_enabled list. a separate function which ran 60 times a second would cycle through every PHYS_ENT in the phys_enabled list and allow them to move, react to collisions, etc.

more info on linked lists can be found in this example: http://www.coniserver.net/ubbthreads/sho...4550db041639079

i hope this is helpful,

julz
Posted By: MrCode

Re: Usage of structs? - 08/03/07 07:06

Quote:


ENTITY contains things like var skill[100], ANGLE pan, VECTOR x, and all those other things that belong to an entity.




Yes, but how, for example, does ANGLE actually rotate the entity? I understand the concept of a struct being an object consisting of various variables and pointers, but I don't know how you'd use say:

Code:

typedef struct
{
float rotate;
} SPIN;



To actually spin an object.
Posted By: JibbSmart

Re: Usage of structs? - 08/03/07 07:21

ANGLE doesn't rotate the entity. the engine has built in functions which we can't see that go through each entity every frame (probably through a linked list or something similar) and use the contents of the entity's "pan", "tilt" and "roll" to orientate the entity.

my custom physics would cycle through my PHYS_ENTS, check their quaternion "quat" or something, and use my functions to convert that to an euler angle that the engine understands and passing it to the entity's pan/tilt/roll.

julz
Posted By: MrCode

Re: Usage of structs? - 08/03/07 20:21

Quote:

more info on linked lists can be found in this example: http://www.coniserver.net/ubbthreads/sho...4550db041639079<br />
<hr /></blockquote><font class="post">




Aah! Too much code!

Other than that, I get the basic idea.

Perhaps I should read up on some C/C++ programming books (or online) to "educate" myself.
Posted By: 3Dski

Re: Usage of structs? - 11/02/07 17:27

Doesn't look like this thread has been active in awhile, but I'll put in my two cents concerning the general use of structs:

First, they are simply a way of holding realated data to carry out a category of tasks, with the type and name of the sturcture describing the purpose; COLOR, POSITION, etc.

Their are a number of benefits connected to the use of structs. They help organize your data and make your code more readable. Furthermore, they help with function argument passing in that, as opposed to passing a bunch of separate variables to a function, you can pass the a single struct* that contains all the needed items. The Windows API makes heavy use of this, where an API call requires the passing of a specific struct that may it might initialze. Then, other API calls would require the same struct to carry out further processing. Instead of having to examine a large number of separate variables, a structure that contains all the data needed for processing, can have its elements updated and can be examined or used for later processing.

So an advantage of using structs with functions is, you don't to worry about remembering the number of parameters to pass to a function, but only know the type of struct it requires. One could argue that this is alos more efficient, in that you are only pass a single struct*, as opposed to a handful of variables being placed on the call stack when the function is called.

Additionally, functions can only return one value, or left to manipulate global variables. By working with data in a struct, the function can return a single struct, or work with a global struct, to alter mulitple pieces of data, which minimizes the number of global variables and the possibility of name clashes; point.x and position.x will not overwrite one anothers values unexpectedly, thought both structs have an "x" element.

Not the best place to expound on the topic, but the post was here, so I have. : )
Posted By: VeT

Re: Usage of structs? - 11/03/07 07:10

here is a question:
i have structure

typedef struct
{
ENTITY* model;
var keyX;
var keyY;
}ChaR;

ChaR MH,GH;

how could i get keyX of one of MH or CH, if i only have pointer to model?
Posted By: Shadow969

Re: Usage of structs? - 11/03/07 07:47

I'd advice you to store pointer to a structure in entity skill
Posted By: VeT

Re: Usage of structs? - 11/04/07 13:41

typedef struct
{
***
ROACH* body;
}PILOT;


typedef struct
{
***
PILOT* driver;
}ROACH;

any ideas about this? :-)
Posted By: TWO

Re: Usage of structs? - 11/04/07 14:01

In this case work with void* as universal type.
Posted By: Shadow969

Re: Usage of structs? - 11/04/07 14:37

workaround

Code:
typedef struct
{
long body;
char* name;
}PILOT;


typedef struct
{
PILOT* driver;
char* name;
}ROACH;

PILOT* pilot1 =
{
name = "Pilot 1";
}

ROACH* vehicle1 =
{
name = "Vehicle 1";
driver = pilot1;
}

void test_structs_startup()
{
pilot1.body = vehicle1;

ROACH* temp_vehicle;
temp_vehicle = (ROACH*)pilot1.body;
error(temp_vehicle.name);
}


Posted By: VeT

Re: Usage of structs? - 11/04/07 19:47

thanks, i solve that problem in another way, but thank you for help
© 2024 lite-C Forums