Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (Akow, 1 invisible), 1,404 guests, and 9 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19055 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
how do you implement complex entity properties? #238884
11/30/08 22:55
11/30/08 22:55
Joined: Apr 2005
Posts: 69
G
Goodberry Offline OP
Junior Member
Goodberry  Offline OP
Junior Member
G

Joined: Apr 2005
Posts: 69
Hey,

I keep running into this same problem and I'd like to know how this is best addressed:

Entities in GS come with skills and flags; flags are basically booleans and skills are var's. Those are the only options you have if you want to store information with an entity. What do you do when you want something a bit more complex, like an array or a list, that is a property of an entity?

For example, let's say I want to make an adventure game and I have NPC's that can give out quests. Each NPC has a different list of quests to give out, so this is not a universal list. In an object-oriented language I could create a public property for my NPC class that is a list of the quests each NPC has to give out (a list of quest ID's, for example). Then I could access that list with something like

currentQuest = NPC.questlist[i];

Lite-C isn't object oriented and so I can't do this. How do you work around this limitation? I seem to keep running into a situation where I create more and more global variables (and the associated ugly code to manage them), and the code becomes hard to deal with after a while.

I'm not looking for code examples, just general advice on how to structure things so this gets addressed more elegantly.

Re: how do you implement complex entity properties? [Re: Goodberry] #238885
11/30/08 23:43
11/30/08 23:43
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Structures. And make the entity property of the structure. But there are many ways to handle them. This is the architecture that I use for handling a "complex" data structure (keep in mind some data-types and functions are fictional, you'll have to make these yourself):

Code:
//complex structure
typedef struct {
  
  BOOL alive; //indicates if the NPC is alive
  BOOL freeze; //indicates if the NPC is frozen
  
  ENTITY* appearance; //the model for the NPC
  unsigned short npc_type; //what kind of NPC
  unsigned int anim_state; //animation state

  VECTOR* pos;     //current position (passed on to ENTITY*
  VECTOR* target;  //target to move to

  DIALOGUES*  dialoges; //NPC's dialogues
  LINKEDLIST* questlist; //NPC's questlist
  
} NPC;



//npc function (called by create_npc)
void handle_npc(NPC* npc) {
  while(npc->alive == true) {
    //do everything here
    wait(1);
  }

  //npc is dead, apparantly... remove the object!
  remove_npc(npc);
}



//creator function (called by game-logic)
NPC* create_npc(STRING* npc_model, VECTOR* start_pos) {

  //allocate memory for a new NPC!
  NPC* npc = (NPC*)malloc(sizeof(NPC));

  npc->alive = true;
  npc->freeze = false;
  
  npc->appearance = ent_create(npc_model, start_pos, NULL);
  npc->npc_type = NPC_VILLAGER; //regular villager
  npc->anim_state = 0; //start at idle animation

  npc->pos = pos;
  npc->target = NULLVECTOR;

  npc->dialogues = get_dialogues(DIALOGUE_VILLAGER); //get dialogue of the villager
  npc->questlist = create_linkedlist();

    //give the NPC two quests at creation
    add_linkedlist_item( npc->questlist, "quest_1"); //add new item to the linkedlist
    add_linkedlist_item( npc->questlist, "quest_2"); //add new item to the linkedlist

    handle_npc(npc); //start it's main function

    return(npc); //return pointer to the calling function for possible further process
}



//removing function (called by handle_npc())
void remove_npc(NPC* npc) {
  //remove all the stored objects that are only stored as pointer first before removing the npc itself.
  //else we'd lose spaces of memory because we throw away the only pointer available to the object.
  ptr_remove( npc->appearance );
  ptr_remove( npc->pos );
  ptr_remove( npc->target );
 
  //ptr_remove can't throw away our own created structures, we need to call their remove functions to prevent memory gaps.
  //just like we do with this npc remove function.
  //note that we don't use remove_dialogues( npc->dialogues ); because more NPC's make use of the same dialogue. This data array is shared.
  remove_linkedlist( npc->questlist ); //this function will then throw away all the objects stored in the linked list (the two quests we assigned earlier).

  //at last, we can remove the npc object safely
  ptr_remove(npc);
}


And this is, then, the code collection of one object in your game. I put each and every object in a separate file to keep things clear.

simply call create_npc("npc1.mdl", NULLVECTOR); and tatam! You'll get a self-functional NPC.
Of course, my code wont work unless you also create all the other missing structures and functions, but it's just an example.


Last edited by Joozey; 11/30/08 23:50.

Click and join the 3dgs irc community!
Room: #3dgs
Re: how do you implement complex entity properties? [Re: Joozey] #239043
12/02/08 02:14
12/02/08 02:14
Joined: Apr 2005
Posts: 69
G
Goodberry Offline OP
Junior Member
Goodberry  Offline OP
Junior Member
G

Joined: Apr 2005
Posts: 69
awesome, thank you! That's really helpful smile

Re: how do you implement complex entity properties? [Re: Goodberry] #239044
12/02/08 02:21
12/02/08 02:21
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Note that I ended with ptr_remove (npc); in it's removal function, but that will not work. ptr_remove is only removing engine structures, and NPC is a user defined structure. You may have seen that malloc() allocates memory of our structure, free() will remove this memory block again.

So ptr_remove(npc) should have been free(npc);


Click and join the 3dgs irc community!
Room: #3dgs
Re: how do you implement complex entity properties? [Re: Joozey] #239844
12/07/08 18:35
12/07/08 18:35
Joined: Apr 2005
Posts: 69
G
Goodberry Offline OP
Junior Member
Goodberry  Offline OP
Junior Member
G

Joined: Apr 2005
Posts: 69
So, sorry to be a bother, but I have a further question.

The built-in functions in Gamestudio, like collision, scan etc all return pointers to entities, in the code above the "appearance" member of the NPC struct. How do I get from this to the NPC struct?

I was thinking maybe you would write a function for the appearance entity that would store and return a pointer to its parent NPC struct. Would that be the best way to do this?

Re: how do you implement complex entity properties? [Re: Goodberry] #239853
12/07/08 19:59
12/07/08 19:59
Joined: Apr 2005
Posts: 69
G
Goodberry Offline OP
Junior Member
Goodberry  Offline OP
Junior Member
G

Joined: Apr 2005
Posts: 69
..and another probably stupid question:

Why wouldn't you hack the atypes.h file and just modify the Entity struct itself?

Re: how do you implement complex entity properties? [Re: Goodberry] #239878
12/07/08 22:52
12/07/08 22:52
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Five, you're not a bother at all, plus it's your own thread wink.
Four, there are no stupid questions, only stupid people.

Three, there are many ways and methods to manage your objects, and sometimes you just need to try something and see if it works out. I'd say that writing a function such as you propose may be quite a good idea, but it depends on how you wrote, and intend to write your program design. At least it's a great learning course to try this wink. Nothing to lose

Two, I strongly advise you not to do so. Not only will it reset after an update, you might mess up the internal code and cause untraceable bugs. You'll break your head and never solve the issue. It gets even more terrible when you forgot you adapted atypes.h and start a new project, which fails too.

One...

Lift off!


Click and join the 3dgs irc community!
Room: #3dgs

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1