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
2 registered members (Grant, AndrewAMD), 911 guests, and 9 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
Page 1 of 2 1 2
Problem with creating struct object dynamically #218897
07/31/08 09:45
07/31/08 09:45
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 everybody,

I'm using the following code:

---------------

typedef struct {
ENTITY* entity;
STRING* description;
int isUsable;
STRING* function_use;
int isTakeable;
STRING* function_take;
STRING* canNotBeTakenOrUsedBy;
int canBeCombined;
ENTITY* canBeCombinedWith;
int isPushable;
STRING* function_push;
int isPullable;
STRING* function_pull;
int canBeOpenedOrClosed;
int canBeTalkedTo;
int dialogueID;
STRING* canBeGivenTo;
} USEABLE_OBJECT;

var fhandle;
var tmpObj;

void filereader() {
fhandle = file_open_read("..\\database\\outside.room");
if (fhandle != 0) {
var tempPointer = 0;
str_cpy(delimit_str,";");
STRING* objName = "";
STRING* str = "";
STRING* strTemp = "";
while (file_str_read(fhandle,objName) != -1) {
tmpObj = (USEABLE_OBJECT*)malloc(sizeof(USEABLE_OBJECT));
file_str_read(fhandle,str);
str_cpy(strTemp,str);
((USEABLE_OBJECT*)tmpObj).description = strTemp;
addToGSHashmap(_chr(objName), ((USEABLE_OBJECT*)tmpObj));
file_str_read(fhandle,str);
}
}
file_close(fhandle);
}

---------------

The command addToGSHashmap comes from a DLL I wrote for using a Hashmap in GS.
The content of my file outside.room is:

---------------

trashbin;Dies ist ein Mülleimer;*
fence;Das ist einfach nur ein Zaun. Nicht mehr, nicht weniger;*
test1;Das ist einfach nur ein Zaun. Nicht mehr, nicht weniger;*
test2;Das ist einfach nur ein Zaun. Nicht mehr, nicht weniger;*

---------------

After execution of filereader() my GSHashmap has a size of 4.
So far so good.

When I call my dll function getFromGSHashmap("trashbin"); it returns a pointer to my USEABLE_OBJECT struct, but it's not the one I expect but as it seems it is the last one created in the while-loop.

Some tests showed that it doesn't seem to be a problem in my dll, but that in my while-loop always the same struct is used, overwritten and then the pointer is put into my GSHashmap so that I have always the same pointer in my map.

Can someone help me???

Best regards,
Pegamode.

Re: Problem with creating struct object dynamically [Re: pegamode] #218902
07/31/08 10:00
07/31/08 10:00
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline OP
Serious User
pegamode  Offline OP
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
Sorry, I put it in the wrong forum.

Should be in Lite-C Programming ... maybe one of the moderators can move it ???

Re: Problem with creating struct object dynamically [Re: pegamode] #219037
07/31/08 18:38
07/31/08 18:38
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline OP
Serious User
pegamode  Offline OP
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
No one any idea ???

Re: Problem with creating struct object dynamically [Re: pegamode] #219045
07/31/08 19:04
07/31/08 19:04
Joined: Aug 2004
Posts: 1,345
Kyiv, Ukraine
VeT Offline

Serious User
VeT  Offline

Serious User

Joined: Aug 2004
Posts: 1,345
Kyiv, Ukraine
some time ago somebody give me this example
Code:
typedef struct
{
long body;
char* name;
}PILOT;
 

typedef struct
{
PILOT* driver;
char* name;
}ROACH;
 
PILOT* ivan = 
{
 name = "Ivan";
}
 
ROACH* ignat = 
{
 name = "Ignat";
 driver = ivan;
}
 
void test_structs_startup()
{
 ivan.body = ignat;
 
 wait(-2);
 ROACH* temp_roach;
 temp_roach = (ROACH*)ivan.body;
 error(temp_roach.name);
}


maybe it can be usefull...


1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy

Newton2 videos: http://tinyurl.com/NewtonVideos
LiteC+Newton2 discussion: http://tinyurl.com/NewtonWrapperDiscussion
Latest LiteC+Newton2 version(v23, from 29.10.2009): http://depositfiles.com/files/ae1l0tpro
Re: Problem with creating struct object dynamically [Re: VeT] #219049
07/31/08 19:38
07/31/08 19:38
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline OP
Serious User
pegamode  Offline OP
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
Unfortunately that doesn't help. In this example the struct objects are just created the "normal" way.

In my case the struct objects don't have their own name, because they need to be created dynamically.

I think one of my mistake was that I declared tmpObj outside the loop, so I changed the code as follows:

while (file_str_read(fhandle,objName) != -1) {
var tmpObj = (USEABLE_OBJECT*)malloc(sizeof(USEABLE_OBJECT));
file_str_read(fhandle,str);
str_cpy(strTemp,str);
((USEABLE_OBJECT*)tmpObj).description = strTemp;
addToGSHashmap(_chr(objName), ((USEABLE_OBJECT*)tmpObj));
file_str_read(fhandle,str);
}

But it still doesn't work ... I have something in my mind that I have to declare something static, but I can't remember what it was exactly ... hmmm ... maybe the weather is too hot out there.

Re: Problem with creating struct object dynamically [Re: pegamode] #219183
08/01/08 13:01
08/01/08 13:01
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline OP
Serious User
pegamode  Offline OP
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
No more ideas ??? Anyone ???

Re: Problem with creating struct object dynamically [Re: pegamode] #219189
08/01/08 13:15
08/01/08 13:15
Joined: Aug 2004
Posts: 1,345
Kyiv, Ukraine
VeT Offline

Serious User
VeT  Offline

Serious User

Joined: Aug 2004
Posts: 1,345
Kyiv, Ukraine
maybe you'd upload example?


1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy

Newton2 videos: http://tinyurl.com/NewtonVideos
LiteC+Newton2 discussion: http://tinyurl.com/NewtonWrapperDiscussion
Latest LiteC+Newton2 version(v23, from 29.10.2009): http://depositfiles.com/files/ae1l0tpro
Re: Problem with creating struct object dynamically [Re: VeT] #219200
08/01/08 13:40
08/01/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
Hmm ... it's part of a bigger project so that it's not so easy to cut it out.

But maybe it helps if I'd share my GSHashmap DLL with you, so that it might be easy to try yourself.

You can download it here:

GSHashmap

It's not final, but works fine in my tests so far.

All you have to do is create objects dynamically in a loop and add their pointers to the GSHashmap under a certain key.
(use char* as key).

Then try to get one of them (not the last one) back from the hashmap and look if it's the right one.

If I do something like this:

ENTITY* ent1 = ent_create(...);
ENTITY* ent2 = ent_create(...);

addToGSHashmap("entity1", ent1);
addToGSHashmap("entity2", ent2);

ENTITY* ent3 = (ENTITY*)getFromGSHashmap("ent1");

Everything worked fine ... but it does not if I do it dynamically in a loop.

Regards,
Pegamode.

Re: Problem with creating struct object dynamically [Re: pegamode] #219231
08/01/08 14:55
08/01/08 14:55
Joined: Aug 2004
Posts: 1,345
Kyiv, Ukraine
VeT Offline

Serious User
VeT  Offline

Serious User

Joined: Aug 2004
Posts: 1,345
Kyiv, Ukraine
i'd look at it this evening smile


1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy

Newton2 videos: http://tinyurl.com/NewtonVideos
LiteC+Newton2 discussion: http://tinyurl.com/NewtonWrapperDiscussion
Latest LiteC+Newton2 version(v23, from 29.10.2009): http://depositfiles.com/files/ae1l0tpro
Re: Problem with creating struct object dynamically [Re: VeT] #219341
08/02/08 07:18
08/02/08 07: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
Hi VeT,

thanks for helping ...

Did you find the time to take a look?

Page 1 of 2 1 2

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