Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
3 registered members (AndrewAMD, The_Judge, Grant), 898 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
get struct by name??? #218284
07/27/08 16:21
07/27/08 16:21
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline OP
Serious User
pegamode  Offline OP
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
Hi,

how can I access a struct by name?

For example:

typedef struct {
ENTITY* entity;
STRING* displayName;
char* playerType;
} PLAYER;

Is there a way to get a pointer to a PLAYER struct by its displayName?
In Java I would store the object into a Hashmap or Hashtable with its displayName as the key ... how would I do that in lice-c? Or do I have to write a c++-plugin?

If there is a way ... can someone give me a short code example?

Best regards,
Pegamode

Re: get struct by name??? [Re: pegamode] #218288
07/27/08 16:53
07/27/08 16:53
Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
T
TWO Offline

Serious User
TWO  Offline

Serious User
T

Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
Code:
typedef struct {
ENTITY* entity;
STRING* displayName;
char* playerType;
} PLAYER;

typedef struct {
PLAYER* data;
void* next;
} PLAYER_LL;

PLAYER_LL* First = 0;
PLAYER_LL* Last = 0;

PLAYER* createPlayer()
{
PLAYER* player = (PLAYER*)malloc(sizeof(PLAYER));

// Add to LinkedList
PLAYER_LL* llItem = (PLAYER_LL*)malloc(sizeof(PLAYER_LL));
llItem->data = player;
player->next = 0;

if( !Last ) {
First = Last = llItem;
}
else {
Last->next = llItem;
Last = llItem;
}

return player;
}

PLAYER* getPlayerForDisplayName( STRING* displayName )
{
PLAYER_LL* llItem = First;

while( llItem ) {
if( str_cmpi( llItem->data->displayName, displayName ) )
return llItem->data;

llItem = llItem->next;
}

return 0;
}


This technique is called linked list.

Re: get struct by name??? [Re: TWO] #218289
07/27/08 17:18
07/27/08 17:18
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline OP
Serious User
pegamode  Offline OP
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
Thanks for your answer, but I don't think that a linked list is what I need.

The displayName is dynamic so I can't create a linked list item.

For example I have a struct that holds a lot of additional information for some model entities in my levels. I have a data file that holds all those additional information and I build the reference from those data to my entities by starting each line in my data file with the entity name as it was set in WED. When starting the game I'd like to create a struct object for each line in my data file and store its pointer into something like a hashtable. When a level is loaded I'd like to get the referring struct objects for my entities using the entity name.

I don't see how I could do that with a linked list ... if I could, how ???

Re: get struct by name??? [Re: pegamode] #218305
07/27/08 19:47
07/27/08 19:47
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
make two linked lists and combine them to a hashmap.

Re: get struct by name??? [Re: Joey] #218306
07/27/08 19:49
07/27/08 19:49
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline OP
Serious User
pegamode  Offline OP
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
Can you give a short code example?

Re: get struct by name??? [Re: pegamode] #218308
07/27/08 20:02
07/27/08 20:02
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
look under lite-c contributions. i've written a linked list implementation which is suitable for hashmaps. i guess i've also written down how to do it there, if there's no complete code sample for a hashmap.

Re: get struct by name??? [Re: Joey] #218314
07/27/08 20:58
07/27/08 20:58
Joined: Jul 2008
Posts: 894
T
TechMuc Offline
User
TechMuc  Offline
User
T

Joined: Jul 2008
Posts: 894
engine_getobj("name");

Re: get struct by name??? [Re: TechMuc] #218531
07/29/08 07:13
07/29/08 07:13
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline OP
Serious User
pegamode  Offline OP
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
Hi TechMuc,

for using engine_getobj you need to know the object name ... I'd like to find a struct by some of it's internal veriables, for example displayName which is part of the struct itself.

Best regards,
Pegamode

Re: get struct by name??? [Re: pegamode] #218765
07/30/08 13:40
07/30/08 13:40
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline OP
Serious User
pegamode  Offline OP
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
I wrote a DLL that lets me use a hashmap. So I have a fast and direct way to access my struct objects.

Currently my DLL has just two functions : add and get, but it's working fine. I'm storing a pointer to my struct object under a certain key.

I'll share that DLL when I added some more usefull functions and if someone is interested in it.

Best regards,
Pegamode.


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