1 registered members (AndrewAMD),
1,187
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Struct Komponenten Problem
#399570
04/17/12 11:26
04/17/12 11:26
|
Joined: Apr 2008
Posts: 139 Japan, Osaka
Tails01
OP
Member
|
OP
Member
Joined: Apr 2008
Posts: 139
Japan, Osaka
|
Hallo Game-Studio Community ich hoffe ihr könnt mir bei meinem Problem weiterhelfen. GER: Meine Problemstellung ist es einige structpointer des typ HERO und einige structpointer des typ MONSTER im Heap zu allocieren. ich hab es so gelöst:ENG: My problem is to allocate a few structpointer from type of HERO and a few from type of MONSTER in the heap. I've solved it this way:
typedef struct MONSTER
{
...
var INIT_M;
...
}MONSTER;
typedef struct HERO
{
...
var INIT;
...
}HERO;
//initialisierung über enmies_create() und hero_create()
MONSTER* create_enemies()
{
MONSTER* enemy = (MONSTER*)malloc(sizeof(MONSTER));
...
enemy->INIT_M = 14;
...
return enemy;
}
int hero_order[3] = {0,1,2};
int cur_hr = 0;
HERO* create_hero()
{
HERO* hero = (HERO*)malloc(sizeof(HERO));
...
switch(hero_order[cur_hr])//hero_order enthält die werte 0,1,2
{
case 0:
...
hero->INIT = 16;
case 1:
...
hero->INIT = 12;
case 2:
...
hero->INIT = 15;
}
...
cur_hr++;
return hero;
}
//direkte übergabe des pointer an andere HERO pointer
void create_battle_startup()
{
HERO* hero0 = create_hero();
HERO* hero1 = create_hero();
HERO* hero2 = create_hero();
MONSTER* monster0 = create_enemies();
MONSTER* monster1 = create_enemies();
MONSTER* monster2 = create_enemies();
i= hero1.INIT; //hier entsteht das Problem
// i = 408
...
GER: nachdem ich also die structs initialisiert habe und übergeben hab, liest er einen falschen Wert für Init aus. theorethisch müsste hero1.INIT die 12 beinhalten, zum Zeitpunkt der initialisierung tut sie das auch nur am Zeitpunkt nach der Übergabe enthält sie einen anderen Wert. ich habe es auch bereits kontrolliert, ob der Pointer "hero1" vieleicht eine andere adresse enthält aber er hat genau die adresse welche er bekommen soll nur ist einzig und allein der INIT wert fehlerhaft. Es ist nun ziemlich schwer dafür noch eine sinnvolle erkläung zu finden und ich würde mich freuen wenn mir jemand erklären könnte was ich falsch mache.ENG: after I've initialize and handed over the structs to the other pointer hero0,hero1 which works fine the compiler read out a wrong content of hero1.INIT. In theory hero1.INIT should content the 12 instead of 408. it's really hard for me to understand this matter because the transfer of other components like ATTACK, DEFENSE works fine only the INIT component gives me headache. I'd be very grateful if there is someone how could explain what is wrong with my considerations.
Gruß Tommy
Last edited by Tails01; 04/17/12 13:22.
An implemented second-rate idea is better than a good idea, which gathering dust in your brain.
|
|
|
Re: Struct Komponenten Problem
[Re: Tails01]
#399577
04/17/12 12:21
04/17/12 12:21
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Cant sprechen Deutsch leid, Übersetzen von Google, aber versuchen Sie dies.
switch(hero_order[cur_hr])//hero_order enthält die werte 0,1,2
{
case 0:
...
hero->INIT = 16;
break;
case 1:
...
hero->INIT = 12;
break;
case 2:
...
hero->INIT = 15;
break;
default:
hero->INIT = 99;
break;
}
Oder wenn dies nicht mehr benutzen, sicher, dass Sie "cur_hr" zu sein beim Start NULL gesetzt. Hope this helps.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Struct Komponenten Problem
[Re: EvilSOB]
#399579
04/17/12 13:02
04/17/12 13:02
|
Joined: Apr 2008
Posts: 139 Japan, Osaka
Tails01
OP
Member
|
OP
Member
Joined: Apr 2008
Posts: 139
Japan, Osaka
|
thanks for the quick answer, but the advise didn't solve the problem. sicher, dass Sie "cur_hr" zu sein beim Start NULL gesetzt. yes I set it to "0" at the beginning.. The switch loop I think doesn't pose the problem, because there are other statements in the jump marks, which works fine like "ATTACK", "DEFENSE" ect.
An implemented second-rate idea is better than a good idea, which gathering dust in your brain.
|
|
|
Re: Struct Komponenten Problem
[Re: Tails01]
#399582
04/17/12 13:54
04/17/12 13:54
|
Joined: Mar 2012
Posts: 927 cyberspace
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
Hero[curhero].init=whatever Im on my cellphone so cant sroll down your code but curhero must be increased i only write hero[] if you think of making array
Last edited by Wjbender; 04/17/12 14:35.
Compulsive compiler
|
|
|
Re: Struct Komponenten Problem
[Re: Tails01]
#399583
04/17/12 13:55
04/17/12 13:55
|
Joined: Feb 2010
Posts: 320 TANA/Madagascar
3dgs_snake
Senior Member
|
Senior Member
Joined: Feb 2010
Posts: 320
TANA/Madagascar
|
Hi, I tested with the following code and it is working fine for me.
typedef struct MONSTER
{
var INIT_M;
}MONSTER;
typedef struct HERO
{
var INIT;
}HERO;
//initialisierung über enmies_create() und hero_create()
MONSTER* create_enemies()
{
MONSTER* enemy = (MONSTER*)sys_malloc(sizeof(MONSTER));
enemy->INIT_M = 14;
return enemy;
}
int hero_order[3] = {0,1,2};
int cur_hr = 0;
HERO* create_hero()
{
HERO* hero = (HERO*)sys_malloc(sizeof(HERO));
switch(hero_order[cur_hr])//hero_order enthält die werte 0,1,2
{
case 0:
hero->INIT = 16;
break;
case 1:
hero->INIT = 12;
break;
case 2:
hero->INIT = 15;
break;
}
cur_hr++;
return hero;
}
//direkte übergabe des pointer an andere HERO pointer
void create_battle_startup()
{
HERO* hero0 = create_hero();
HERO* hero1 = create_hero();
HERO* hero2 = create_hero();
MONSTER* monster0 = create_enemies();
MONSTER* monster1 = create_enemies();
MONSTER* monster2 = create_enemies();
var i = hero0.INIT; //hier entsteht das Problem
int j = hero0.INIT;
printf("i = %d, j = %d", (int)i, j);
// Free heroes
sys_free(hero0);
sys_free(hero1);
sys_free(hero2);
// Free monstaers
sys_free(monster0);
sys_free(monster1);
sys_free(monster2);
}
Best regards.
|
|
|
Re: Struct Komponenten Problem
[Re: 3dgs_snake]
#399601
04/17/12 15:32
04/17/12 15:32
|
Joined: Apr 2008
Posts: 139 Japan, Osaka
Tails01
OP
Member
|
OP
Member
Joined: Apr 2008
Posts: 139
Japan, Osaka
|
@3dgs_snake yeah I get the same result out of the first code, but let me emphasize that you read out the value for hero0. hero0 is coincidentally the only struct which works... just try it out for "hero1" thats the case where the trouble starts.
An implemented second-rate idea is better than a good idea, which gathering dust in your brain.
|
|
|
Re: Struct Komponenten Problem
[Re: Tails01]
#399609
04/17/12 17:22
04/17/12 17:22
|
Joined: Mar 2012
Posts: 927 cyberspace
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
typedef struct MONSTER
{
var INIT_M;
}MONSTER;
typedef struct HERO
{
var INIT;
}HERO;
MONSTER* create_enemies()
{
MONSTER* enemy = malloc(sizeof(MONSTER));
if(enemy)
{
enemy.INIT_M = 14;
return enemy;//or return &enemy but dont thinks so since its a pointr
}
return NULL;
}
int hero_order[3] = {0,1,2};
//if using example a then
int cur_hr = -1;
//if using axample b then uncomment this and comment above
//int cur_hr = 0;
HERO* create_hero()
{
//example A:
//from here->
HERO* hero = malloc(sizeof(HERO));
if(hero)
{
cur_hr+=1;
if(cur_hr==0) hero.INIT = 16;
if(cur_hr==1) hero.INIT = 12;
if(cur_hr==2) hero.INIT = 15;
return hero;//or return &hero but dont thinks so since its a pointr
}
return NULL;
//to here<-
//---------------------------------------------------
//example B:
//from here->
/*
HERO* hero = malloc(sizeof(HERO));
if(hero)
{
int cur_hero_order=hero_order[cur_hr];
switch (cur_hero_order)
{
case 0:
hero.INIT=16;
break;
case 1:
hero.INIT=12;
break;
case 2:
hero.INIT=15;
break;
}
cur_hr+=1;
return hero;
}
return NULL;
*/
//to here<-
}
//direkte übergabe des pointer an andere HERO pointer
void create_battle_startup()
{
HERO* hero0 = create_hero();
HERO* hero1 = create_hero();
HERO* hero2 = create_hero();
MONSTER* monster0 = create_enemies();
MONSTER* monster1 = create_enemies();
MONSTER* monster2 = create_enemies();
int i=hero1.INIT;
}
its untested i dont have gamestudio installed rite now but hope it helpes you good luck.
Last edited by Wjbender; 04/17/12 17:30.
Compulsive compiler
|
|
|
Re: Struct Komponenten Problem
[Re: Wjbender]
#399621
04/17/12 19:13
04/17/12 19:13
|
Joined: Apr 2008
Posts: 139 Japan, Osaka
Tails01
OP
Member
|
OP
Member
Joined: Apr 2008
Posts: 139
Japan, Osaka
|
I've included your code, but the desired effect was not achieved instead of this the program assign new INIT values:
(WATCHED VARIABLES) i1 = hero0.INIT = 16 // right value i2 = hero1.INIT = 16 // wrong value (before = 408) i3 = hero2.INIT = 19 // wrong value (before = 411)
for comparison //these are the desired values 1. hero = 16; 2. hero = 12; 2. hero = 15;
An implemented second-rate idea is better than a good idea, which gathering dust in your brain.
|
|
|
Re: Struct Komponenten Problem
[Re: Tails01]
#399625
04/17/12 20:02
04/17/12 20:02
|
Joined: Mar 2012
Posts: 927 cyberspace
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
have you try "return &hero;" ? and does both examples give that result?
int hero_order[3] = {0,1,2};
int cur_hr = 0;
HERO* create_hero()
{
HERO* hero = malloc(sizeof(HERO));
if(hero)
{
int cur_hero_order=hero_order[cur_hr];
cur_hr+=1;
switch (cur_hero_order)
{
case 0:
{
hero.INIT=16;
return hero;//or return &hero
}
case 1:
{
hero.INIT=12;
return hero;//or return &hero
}
case 2:
{
hero.INIT=15;
return hero;//or return &hero
}
}
}
return NULL;
}
Last edited by Wjbender; 04/17/12 20:13.
Compulsive compiler
|
|
|
Re: Struct Komponenten Problem
[Re: Wjbender]
#399626
04/17/12 20:15
04/17/12 20:15
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Have you tried changing the struct entry 'INIT' to something else? As a test...
Say for example H_INIT?
Just in case INIT is a "secret" reserved word or something?
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
|