|
The question for thee with not so descriptive title
#322943
05/10/10 17:04
05/10/10 17:04
|
Joined: Mar 2009
Posts: 88
Walori
OP
Junior Member
|
OP
Junior Member
Joined: Mar 2009
Posts: 88
|
Didn't get any better titles for this.... Anyway these two questions have bugged me for a time and would like to have a clarification. 1)Is there a macro or an easy way to COPY structs? For an example I have array of structs and I want to create a new replicate of it. Now I have to do it "manually"; initializing every component on creation. Wouldn't be a problem if I could tell for sure that my structs didn't change. 2)How does if((total_frames%80) == X) work? I found it in one of the AUM's but never could really figure out how does 80 percent of frames since start return X amount? Also if I put there 10 will it be allways 10 seconds no matter which wait time I have at the end of the loop? 3)How do struct sizes go? If I leave let's say struct's text component with 21 strings (100 characters each) unitialized will it consume memory still the same amount as it'd when it had been initialized? And which one of the below consumes less memory. I'm pretty sure it's the lower one, but want to be sure.
STRUCT*stru = new(STRUCT);
stru->text = txt_create(21,0);
for(i=0;i<21;i++)
{
//So on
...
STRUCT* stru = new(STRUCT);
stru->text = text_initalized_before;
...
(Note with memory I mean RAM usage... or nexus.) Thanks in advance; Walori
|
|
|
Re: The question for thee with not so descriptive title
[Re: Walori]
#322954
05/10/10 17:26
05/10/10 17:26
|
Joined: Oct 2007
Posts: 5,210 İstanbul, Turkey
Quad
Senior Expert
|
Senior Expert
Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
|
1)http://www.cplusplus.com/reference/clibrary/cstring/memcpy/
2) % is modular operator it does not means "percent"
total_frames%80 means divide total_frames by 80 and return the remains: f.i. if total_frames is 139, total_frames%80 will be equal to 59, if total_frames is 160, total_frames%80 will be equal to 0, if total_frames is 162, total_frames%80 will be equal to 2 ...
3) explain better, how does your struct looks?
3333333333
|
|
|
Re: The question for thee with not so descriptive title
[Re: Walori]
#322955
05/10/10 17:29
05/10/10 17:29
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
1.) I don't know a macro for this, but you can just use memcpy. Create a new struct at runtime and the copy the whole stuff over with memcpy.
2.) The % operator is the so called modulo operator. It returns the rest of a division by the second operand. " if((total_frames%80) == X)" will become true whenever the rest of the division of total_frames by 80 is exactly x. In other words, the condition becomes true every Xth frame.
3.) I guess it's the second one. But I am not sure how new works. Is this some new macro? However, if you allocate memory it is always allocated from RAM, not from Nexus. Exception: if you use sys_nxalloc.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: The question for thee with not so descriptive title
[Re: Uhrwerk]
#323092
05/11/10 15:50
05/11/10 15:50
|
Joined: Mar 2009
Posts: 88
Walori
OP
Junior Member
|
OP
Junior Member
Joined: Mar 2009
Posts: 88
|
thank you for the answers. About the total_frames%80, let's say I want to call printf every 5th second, do I only put it like below, I've not got accurate measurements to verify this
while(1)
{
if((total_frames%80) == 5)
printf("Hello world");
wait(1);
}
3) I meant if above way consumes more memory than below one.
//Defines
typedef struct
{
TEXT* text;
}STRUCT;
//I've defined macro for struct creation
void main()
{
var i;
STRUCT* stru = new(STRUCT);
stru->text = txt_create(21,0);
for(i=0;i<text.strings;i++)
{
str_cpy((text.pstring)[i],"TEXT HERE TO DEMONSTRATE!");
}
//And create another one with same way.
STRUCT* stru_2 = new(STRUCT);
stru->text = txt_create(21,0);
for(i=0;i<stru->text->strings;i++)
{
str_cpy((stru->text->pstring)[i],"TEXT HERE TO DEMONSTRATE!");
}
}
void main()
{
var i;
STRUCT* stru = new(STRUCT);
stru->text = txt_create(21,0);
for(i=0;i<stru->text.strings;i++)
{
str_cpy((stru->text.pstring)[i],"TEXT HERE TO DEMONSTRATE!");
}
//Now let's not create another text but put a created text
//To another struct's pointer
STRUCT* stru_2 = new(STRUCT);
stru_2->text = stru->text;
}
The question really is does a initialized pointer (like text) consume memory how much when compared to object that was created. If that makes any sence.
|
|
|
Re: The question for thee with not so descriptive title
[Re: Walori]
#323094
05/11/10 16:17
05/11/10 16:17
|
Joined: Feb 2009
Posts: 3,207 Germany, Magdeburg
Rei_Ayanami
Expert
|
Expert
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
|
thank you for the answers. About the total_frames%80, let's say I want to call printf every 5th second, do I only put it like below, I've not got accurate measurements to verify this
while(1)
{
if((total_frames%80) == 5)
printf("Hello world");
wait(1);
}
Hiya, This is not what you want... If you want it every 5th _SECOND_ put a wait(-1) cause this is a second, and frames change... And, not %80, %5 and check if it is 0 -> if((total_ticks/16)%5 == 0)
|
|
|
Re: The question for thee with not so descriptive title
[Re: Rei_Ayanami]
#323101
05/11/10 16:48
05/11/10 16:48
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
For a delay of 5 seconds a wait(-5) would totally suffice.
To your third question: In terms of memory efficiency your last solution consumes less memory. But! Don't waste your time with this stuff. Reduce one texture from 128x128 to 64x64 and you have won a hundred times the memory you do with optimizing your structs. Choose the more elegant solution for your code. You'll drive better with this on the long run, unless your dealing with really memory hungry things. A programmers wisdom that cannot be underestimated: Don't optimize things that already run sufficently fast.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: The question for thee with not so descriptive title
[Re: Uhrwerk]
#323107
05/11/10 17:19
05/11/10 17:19
|
Joined: Mar 2009
Posts: 88
Walori
OP
Junior Member
|
OP
Junior Member
Joined: Mar 2009
Posts: 88
|
Thanks for the answers. The third question is now settled, I will just copy everything to another struct when created as it's better solution in long run. However wait(-5) really doesn't meet my needs as I need to check time within a loop that holds several other thing at the same time (maybe the example was not really good). For an example (At the bottom of the code):
while(my.status == attacking)
{
trace_result = c_trace(vector(my.x,my.y,my.z),vector(my.x,my.y,my.z-20),IGNORE_ME | IGNORE_PASSABLE | IGNORE_SPRITES | IGNORE_FLAG2 | IGNORE_CONTENT | IGNORE_MODELS);
if(trace_result == 0)
{
c_move(my,nullvector,vector(0,0,(-1*mod->zone_VAR_gravity) + my.gravity_mod),IGNORE_ME | IGNORE_PASSABLE | IGNORE_SPRITES | IGNORE_FLAG2 | IGNORE_CONTENT);
}
else
{
if(trace_result >= 2)
{
c_move(my,nullvector,vector(0,0,(-1*mod->zone_VAR_gravity) + my.gravity_mod),IGNORE_ME | IGNORE_PASSABLE | IGNORE_SPRITES | IGNORE_FLAG2 | IGNORE_CONTENT);
}
}
if(main_tar == NULL)
{
//If main_tar is null this NPC is attacked before it scanned
//Adn we pass pointer to attacker by combat skill
main_tar = my.combat;
my.combat = 0;
}
//if(c_trace)
vec_set(temp,me.x);
//We have a clear look to the player
if(vec_dist(my.x,main_tar.x) > 100)
{
my.status = evade;
break;
}
if(vec_dist(my.x,main_tar.x) > 6)
{
you = main_tar;
if(c_trace(vector(my.x,my.y,my.z),vector(my.x+10*cos(my.pan),my.y+10*sin(my.pan),my.z),IGNORE_ME | IGNORE_SPRITES | IGNORE_PASSABLE | IGNORE_FLAG2 | IGNORE_YOU | IGNORE_WORLD | IGNORE_MAPS | IGNORE_CONTENT) == 0)
{
if(walk_anim >= 100)
walk_anim = 0;
else
walk_anim +=4.5*time_step;
ent_animate(me,"walk",walk_anim,ANM_CYCLE);
c_move(me,vector(3*time_step,0,0),nullvector,GLIDE | IGNORE_SPRITES | IGNORE_PASSABLE | IGNORE_FLAG2 | IGNORE_CONTENT);
vec_set(temp,main_tar.x);
vec_sub(temp,my.x);
vec_to_angle(my.pan,temp);
}
else
{
vec_set(target_pos,vector(main_tar.x,main_tar.y+50,main_tar.z));
my.status = re_routing;
break;
}
}
else
{
//main_tar = you;
if(u_hea > 0 && (total_frames %80) == chr->atk_speed)
{
AI_hit_system(mod,chr,chr_you,me,main_tar);
}
}
wait(1);
}
as the same loop holds also movemtn waith -5 would mean the movement becoming laggier. It's not really a big deal yet, one while loop at the beginning of the development is just a small program slower. However when I consider that the plan was / is to have over 20-60 enemies on the screen max then that one loop would mean a lot more. Right?
|
|
|
|