1 registered members (AndrewAMD),
1,089
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Usage of structs?
#145397
08/03/07 05:55
08/03/07 05:55
|
Joined: Mar 2007
Posts: 677 0x00000USA
MrCode
OP
User
|
OP
User
Joined: Mar 2007
Posts: 677
0x00000USA
|
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?
Last edited by MrCode; 08/03/07 06:03.
void main()
{
cout << "I am MrCode,";
cout << "hear me roar!";
system("PAUSE");
}
|
|
|
Re: Usage of structs?
[Re: MrCode]
#145398
08/03/07 06:28
08/03/07 06:28
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
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...4550db041639079i hope this is helpful, julz
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Usage of structs?
[Re: JibbSmart]
#145399
08/03/07 07:06
08/03/07 07:06
|
Joined: Mar 2007
Posts: 677 0x00000USA
MrCode
OP
User
|
OP
User
Joined: Mar 2007
Posts: 677
0x00000USA
|
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.
void main()
{
cout << "I am MrCode,";
cout << "hear me roar!";
system("PAUSE");
}
|
|
|
Re: Usage of structs?
[Re: MrCode]
#145400
08/03/07 07:21
08/03/07 07:21
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
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
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Usage of structs?
[Re: MrCode]
#145402
11/02/07 17:27
11/02/07 17:27
|
Joined: Nov 2005
Posts: 94 Texas
3Dski
Junior Member
|
Junior Member
Joined: Nov 2005
Posts: 94
Texas
|
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. : )
|
|
|
|