ENTITIES into array

Posted By: JHA

ENTITIES into array - 09/26/10 14:01

Hello
Is it possible to put ENTITY(s) to array, something like

ENTITY BULLET[8]

and then have access to them like any array:

BULLET[1]= ent_create("ball2.mdl",BulletP,DestroyBulletIfOutsite);

Or is there some vector where engine keeps the pointers. Because when pressing F11 is possible to see number of entities?
JHA
Posted By: Superku

Re: ENTITIES into array - 09/26/10 14:10

Why don't you just try it?

ENTITY* BULLET[8];
Posted By: JHA

Re: ENTITIES into array - 09/28/10 15:53

This works ok. But doing coding sometime, I have learned that with pointers and dynamic memory one has to be pretty carefull. Anyway so far so good. Thanks
Posted By: EvilSOB

Re: ENTITIES into array - 09/29/10 07:31

I cant see any dynamic here.

The pointer-array (Superku's example) is a static array, not dynamic.
And entities are static in memory once created.

Just remember to fill your array with NULLs at startup time.
eg
Code:
#define MAX_BULLET 10
ENTITY* BULLET[MAX_BULLET];

void BULLET_startup()
{  int i;   for(i=0; i<MAX_BULLET; i++)   BULLET[i] = NULL;   }


Posted By: JHA

Re: ENTITIES into array - 10/03/10 06:18

OK, and thanks again, I will add this NULL filling of vector into initilization
Posted By: miez

Re: ENTITIES into array - 10/04/10 10:02

What is happening, if the Arrayparts arenīt filled with NULL at the beginning?
Posted By: WretchedSid

Re: ENTITIES into array - 10/04/10 10:06

nothing
Posted By: EvilSOB

Re: ENTITIES into array - 10/04/10 10:20

Nothing "happens", but if you dont put null in you get random numbers in them.
That then makes it more difficult to know which ones are used or not.

If you NULL them all, you can then use this to see what elements are in use.
Code:
if(BULLET[idx]) { bullet-IS-being-used     }
else            { bullet-is-NOT-being-used }

//or

if(BULLET[idx]==NULL) { bullet-is-NOT-being-used     }
else                  { bullet-IS-being-used }


Just remember to set them to NULL when youve finished using them...
Code:
ent_remove(BULLET[idx]);
BULLET[idx] = NULL;


Posted By: miez

Re: ENTITIES into array - 10/04/10 16:42

The array get filled with random Numbers?...
wow...
1. why (would Auto-Null not be more elegant?)
and
2. Didnīt know that. This explains a lot of problems I encountered. Will keep it in Mind for Future things.

Thereīs always quiet a bit to learn, when reading here.
Posted By: WretchedSid

Re: ENTITIES into array - 10/04/10 16:59

Originally Posted By: miez
The array get filled with random Numbers?...
wow...
1. why (would Auto-Null not be more elegant?)

It would cost more CPU cycles and would be slower. The reason is simple: You just get a random chunk of memory and everything what was before you in this chunk of memory is still there.

But I know of one exception that at least exist on iOS and Mac OS X: When you get a new memory page, it is nulled by the OS for security reasons. However, as long as this page is mapped into your application (or vice versa), this won't happen again.
Posted By: EvilSOB

Re: ENTITIES into array - 10/04/10 17:22

You CAN turn the auto-nulling ON, buts as JustSid said, it costs CPU.
Because then ANY new variable (both global and locals) will then requires CPU cycles to auto-null it.

To turn it on, insert this line VERY early in your script. By preferance, just after your #include "acknex.h" or "lite-c.h".
Code:
#define PRAGMA_ZERO

(Check under "#define" in the manual)


... hmmm ... looks like its only for local variables, and not globals as I thought ...
Ah well, give it a try anyway, it may just be a glitch in the manual regarding globals...
Posted By: WretchedSid

Re: ENTITIES into array - 10/04/10 18:04

Wow, I didn't knew that Lite-C has a solution for this. Amazing.
Posted By: Bunsen

Re: ENTITIES into array - 10/04/10 19:28

Global and static variables are nulled automatically by the compiler. This does
not cost any CPU cycles because they are stored as placeholders within the executable. The more global variables the bigger the EXE.

Local variables on the other side are created at runtime.
Posted By: EvilSOB

Re: ENTITIES into array - 10/04/10 19:54

Kewl, thanks Bunsen.

That explains why the manual says "PRAGMA_ZERO" is for locals.

But does that also apply to an array of pointers, as this thread is about?

ie a global array of pointers declared with
Code:
ENTITY* BULLET[...];

I honestly dont know, so I await your answer....
Posted By: Bunsen

Re: ENTITIES into array - 10/04/10 20:38

Yes EvilSOB, it does also apply to an array of pointers.
You may try this:

ENTITY* BULLET[10];

void main()
{
int i;
for (i=0; i<10; i++)
printf("Bullet[%d] = %d", i, BULLET[i]);
}

By the way, what is the meaning of "Kewl"?
Posted By: EvilSOB

Re: ENTITIES into array - 10/04/10 21:23

Thanks, but no need to try.
I had noticed this result, but never trusted it till now.

And "kewl" == "Cool" spelt really badly...
© 2024 lite-C Forums