1 registered members (TipmyPip),
18,513
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Functions in structs?
#179212
01/22/08 02:12
01/22/08 02:12
|
Joined: Jan 2008
Posts: 49 Sweden
Kenchu
OP
Newbie
|
OP
Newbie
Joined: Jan 2008
Posts: 49
Sweden
|
I just started learning litec. Im abit confused about how youre supposed to structuralize everything youre scripting. Im used to object orientation (java), and litec feels to me very procedural. So is it somehow possible to have functions inside the objects im creating? Are structs the only thing you can use within litec, or is there some kind of class feature aswell? Or maybe youre supposed to modularize everything in another way? How do you do it?  And btw. How does digits function work? And string? For example, if i define a PANEL*, I can use digits(..) to get some digits going there. .. how does that work exactly? I tried initializing a property of a struct thorugh a function invocation that returned a var, but that didnt work. (Kinda like MYSTRUCT* lol = { func(); } Thx
Last edited by Kenchu; 01/22/08 02:24.
|
|
|
Re: Functions in structs?
[Re: Kenchu]
#179213
01/22/08 10:37
01/22/08 10:37
|
Joined: Jul 2001
Posts: 6,904
HeelX
Senior Expert
|
Senior Expert
Joined: Jul 2001
Posts: 6,904
|
Quote:
Im used to object orientation (java), and litec feels to me very procedural.
Actually, Lite-C is kinda ANSI C standard and since C is procedural, Lite-C is procedural, too.
Quote:
So is it somehow possible to have functions inside the objects im creating? Are structs the only thing you can use within litec, or is there some kind of class feature aswell? Or maybe youre supposed to modularize everything in another way? How do you do it?
Well, I know what you mean. Actually, you cant store functions in structs. Thats why they are called structs: "data structures". Though, the developers said, that they will eventually add functions to structs (which seems odd to me, but, well... okay).
As a solution, you can save a function ptr into an instance of a struct. By this you could write your own routine to execute those functions by passing their pointer which is saved inside the instance.
Quote:
And btw. How does digits function work? And string? For example, if i define a PANEL*, I can use digits(..) to get some digits going there. .. how does that work exactly?
There are good examples in the manual - try them first!
Quote:
I tried initializing a property of a struct thorugh a function invocation that returned a var, but that didnt work. (Kinda like MYSTRUCT* lol = { func(); }
This is an example struct:
Code:
typedef struct TRACKPOINT { VECTOR pos; struct TRACKPOINT *parent, *child; } TRACKPOINT;
which saves a vector and two pointers two other instances. As you can see, you can only store data.
There is a good online book that explains C-programming:
http://publications.gbdirect.co.uk/c_book/
or specifically on structrues: http://publications.gbdirect.co.uk/c_book/chapter6/structures.html
Go for it! - Cheers, Christian
|
|
|
Re: Functions in structs?
[Re: HeelX]
#179214
01/22/08 12:00
01/22/08 12:00
|
Joined: Jan 2008
Posts: 49 Sweden
Kenchu
OP
Newbie
|
OP
Newbie
Joined: Jan 2008
Posts: 49
Sweden
|
Quote:
Well, I know what you mean. Actually, you cant store functions in structs. Thats why they are called structs: "data structures". Though, the developers said, that they will eventually add functions to structs (which seems odd to me, but, well... okay).
As a solution, you can save a function ptr into an instance of a struct. By this you could write your own routine to execute those functions by passing their pointer which is saved inside the instance.
Yes I know you can do that somehow, but not how exactly. I tried looking for it in the manual, but without success. Also looked in that online book you linked too, which indeed explains how to do it in C. However, when I tried doing it in lite-c, it would complain about syntax errors. I tried having these declarations inside a struct:
Code:
function (*myfunk); function (*myfunk)(); function (*myfunk)(void); int (*myfunk); int (*myfunk)(); int (*myfunk)(void);
Only one at the time of course to try it out. None of the above was allowed.
Quote:
There are good examples in the manual - try them first!
I dont think you understood what I meant. I know how to use them, not how they work. Exactly what do they return? Usually, you'd have to do something like this:
member of struct equals something/function return value i.e. for example
Code:
PANEL* my_panel = { pos_x = 10; pos_y = func_that_returns_a_var(); }
But then we have these string() and digits(), which you dont use in that way. You just invoke them inside an object initialization:
Code:
PANEL* my_panel = { digits(10,10,2,*,1,some_var); }
Why exactly is that allowed? Is it possible for me to define a function that is allowed within an object initialization like that?
Quote:
Code:
typedef struct TRACKPOINT { VECTOR pos; struct TRACKPOINT *parent, *child; } TRACKPOINT;
which saves a vector and two pointers two other instances. As you can see, you can only store data.
About that. Are there any data structures implemented in lite-c already? Kinda like STL in C++ so you wont have to implement your own vectors, lists and maps?
Last edited by Kenchu; 01/22/08 12:16.
|
|
|
Re: Functions in structs?
[Re: Kenchu]
#179215
01/22/08 12:30
01/22/08 12:30
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
This thread might be of interest to you: http://www.coniserver.net/ubbthreads/sho...true#Post809004excessus: Quote:
Since Lite-C is a little weird about function pointers (they're not actually types, I think), you can't have a function pointer type in your struct. But you can have a void* in which you can store (but not call) a function pointer (manual suggests you do this for function pointer arrays, so it's not THAT unsafe). You can then assign the void* to a function pointer (Lite-C casts automatically), and call the function from the function pointer.
This basicly gives you member functions, but the syntax is quite ugly. Maybe you can fix that with a macro. CALL_MEMBER or something..
He provides an example as well.
Greetz
Last edited by Joozey; 01/22/08 12:33.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: Functions in structs?
[Re: Joozey]
#179216
01/22/08 12:41
01/22/08 12:41
|
Joined: Jan 2008
Posts: 49 Sweden
Kenchu
OP
Newbie
|
OP
Newbie
Joined: Jan 2008
Posts: 49
Sweden
|
Quote:
This thread might be of interest to you: http://www.coniserver.net/ubbthreads/sho...true#Post809004
excessus:
Quote:
Since Lite-C is a little weird about function pointers (they're not actually types, I think), you can't have a function pointer type in your struct. But you can have a void* in which you can store (but not call) a function pointer (manual suggests you do this for function pointer arrays, so it's not THAT unsafe). You can then assign the void* to a function pointer (Lite-C casts automatically), and call the function from the function pointer.
This basicly gives you member functions, but the syntax is quite ugly. Maybe you can fix that with a macro. CALL_MEMBER or something..
He provides an example as well.
Greetz
Hm thats weird. It feels like im doing the same thing but it wont work. Take a look at this:
Code:
#include <acknex.h> #include <default.c>
#define new(TYPE) malloc(sizeof(TYPE)) #define delete(OBJECT) free(OBJECT)
typedef struct { int id; var coolness; STRING* name; function lolz(); } ITEM;
ITEM* obj1;
function init_item(ITEM* item, var coolness, STRING* name) { static int count = -1; item.id = ++count; item.coolness = coolness; item.name = name; }
TEXT* pnl = { pos_x = 10; pos_y = 10; flags = VISIBLE; }
function test() { (pnl.pstring)[0] = "performed"; }
function perform() { obj1.lolz(); }
function main() { screen_color.blue = 155;
obj1 = new(ITEM); init_item(obj1, 2, "Hej"); obj1.lolz = test; on_a = perform; }
|
|
|
Re: Functions in structs?
[Re: Excessus]
#179220
01/22/08 13:32
01/22/08 13:32
|
Joined: Jan 2008
Posts: 49 Sweden
Kenchu
OP
Newbie
|
OP
Newbie
Joined: Jan 2008
Posts: 49
Sweden
|
Quote:
Yes it's extremely ugly, that's why I prefer not to use this. As far as I know there is no other way. It has to be done like this to work around the limitations of Lite-C (methods and function pointer members are not implemented in Lite-C).
You'll just have to accept that Lite-C is not object-oriented. That's the way it is, but it doesn't mean you can't program in an object oriented language with gamestudio. You can use the engine in C++, or create plugins in C++. There is also a C# wrapper, I think.
I guess thats fine with me anyways. I just havent programmed big apps in a procedural way before, so Im not sure how to modularize everything.
Are there any open source 3dgs apps out there which you can have a look at? Think that would be awsome.
|
|
|
|