Posted By: pegamode
Problem with creating struct object dynamically - 07/31/08 09:45
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.
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.
