Dynamic variables/structs/entintys/etc names

Posted By: Carlos3DGS

Dynamic variables/structs/entintys/etc names - 12/09/08 20:42

I was wondering if this could be done...
There are some cases when I dont know on start how many "copies" of a certain item I want to have.
Sometimes I even want tons of them! But I dont want to have to declare tons of variables that I dont know if all are going to be used (or mabe even more than i declare will be needed).

I was wondering if there was any way to create a string (changing the string for each new entity) and using that for its name?

instead of having to use pre-defined names like I do now:
ENTITY* soldier1;
ENTITY* soldier2;
ENTITY* soldier3;
etc...
ENTITY* soldier100;

I just hate doing that!
And also its 100 corresponding actions to attack each different pointer (or 100 ifs in the action to decide which pointer to make = me)

example of what im asking:
Code:
 
int number_of_soldiers;

action Create_Soldier_Name()
{
 STRING* My_ID_Number;
 STRING* New_Entity_Name;

 //set the number
 My_ID_Number = number_of_soldiers;

 //create a string with that number atttacked to it
 New_Entity_Name = "Soldier" & My_ID_Number;
 
 //Somehow use that string as the name/pointer for that entity
 New_Entity_Name = me;
}

void main()
{
 number_of_soldiers = 0;

 while(1)
 {
  if(create_soldier_button == TRUE)
  {
   //update number of soldiers
   number_of_soldiers ++;

   //create a new soldier and go to action to give it its' unique name
   ent_create("grunt.mdl", vector(0,0,0), Create_Soldier_Name)
  }
 }
}


I know that code will not work, but I hope you get what I intend to ask by that example.
Is it posible to do something similar to achieve this?
Mabe also for structs or variables or whatever?
Posted By: Uhrwerk

Re: Dynamic variables/structs/entintys/etc names - 12/09/08 21:08

What you're asking for is an array. Read the manual under topic "variables". If that is not what you're looking for search for programming lists.
Posted By: Carlos3DGS

Re: Dynamic variables/structs/entintys/etc names - 12/09/08 22:30

I thought of using arrays at first, but was told that I cannot change the size of the array after it is created and used.

Is it true, or can I cange the size to make an array bigger when it fills up?
Posted By: EvilSOB

Re: Dynamic variables/structs/entintys/etc names - 12/09/08 23:15

Create an extendable array. But it does come with its own little problems unless you are tidy with your code.
Heres an example, complete (except for model name), compiled & tested.

Code:
#include <acknex.h>
#include <default.c>
//
//
//
ENTITY** soldier;
var max_soldiers;
//
//
function main()
{
   level_load(NULL);      camera.x = -400;   camera.z = -300;   camera.tilt = 45;
   //
   //
   // CREATE initial soldier array
   max_soldiers = 5;                                                 //set initial number of soldiers
   soldier = (ENTITY**)malloc(sizeof(ENTITY*)*max_soldiers);         //create array to hold this many soldier-entities
   soldier[0] = ent_create("cube.mdl", vector(0,10,0), NULL);                //create solder 1
   soldier[1] = ent_create("cube.mdl", vector(0,50,0), NULL);                //create solder 2
   soldier[2] = ent_create("cube.mdl", vector(0,100,0), NULL);               //create solder 3
   soldier[3] = ent_create("cube.mdl", vector(0,150,0), NULL);               //create solder 4
   soldier[4] = ent_create("cube.mdl", vector(0,200,0), NULL);               //create solder 5
   wait(-3);   //pause demo for dramatic effect
   //
   // EXTEND soldier array
   max_soldiers = 10;                                                      //increase number of soldiers
   soldier = (ENTITY**)realloc(soldier, sizeof(ENTITY*)*max_soldiers);     //extend array to hold aditional soldier-entities
   soldier[5] = ent_create("cube.mdl", vector(0,-10,0), NULL);        //create solder 6  in new space
   soldier[6] = ent_create("cube.mdl", vector(0,-50,0), NULL);        //create solder 7  in new space
   soldier[7] = ent_create("cube.mdl", vector(0,-100,0), NULL);       //create solder 8  in new space
   soldier[8] = ent_create("cube.mdl", vector(0,-150,0), NULL);       //create solder 9  in new space
   soldier[9] = ent_create("cube.mdl", vector(0,-200,0), NULL);       //create solder 10 in new space
   //
   wait(-1);   //pause demo for dramatic effect
   soldier[0].x -= 10;   //move old soldier 1 to prove he is still accessable under old name
   soldier[1].x -= 20;   //move old soldier 2 to prove he is still accessable under old name
   soldier[2].x -= 30;   //move old soldier 3 to prove he is still accessable under old name
   soldier[3].x -= 40;   //move old soldier 4 to prove he is still accessable under old name
   soldier[4].x -= 50;   //move old soldier 5 to prove he is still accessable under old name
   //
   //
   wait(-10);
   //flush and reset soldier array for next level.
   int i;   for(i=0; i<max_soldiers; i++)   { ent_remove(soldier[i]); }     //see - old soldiers DO die
   free(soldier);   //now as if it had never been used...
   //
   //
   //
   beep();
}
Any questions, fire away...
Posted By: testDummy

Re: Dynamic variables/structs/entintys/etc names - 12/09/08 23:53

Removal of elements may leave free slots that can be reused.
A linked listed can be used to manage free slot indices, so that the first available free slot index is found easily and quickly.

Also a hash map/table plug-in was offered in Lite-C contributions, 'I' believe.
Posted By: EvilSOB

Re: Dynamic variables/structs/entintys/etc names - 12/10/08 01:03

True, my above code it a teaching example of a simple but "dirty" extendable array,
rather than learning the whole linked lists concept.

Linked lists IS the preferred way to go though, just a steeper learning curve.
Posted By: Espér

Re: Dynamic variables/structs/entintys/etc names - 12/10/08 08:40

I don't think he will use more than 999999999 ents on one map...
So why not setting the array on that count at start?
Posted By: JibbSmart

Re: Dynamic variables/structs/entintys/etc names - 12/10/08 12:08

that'll eat insane memory.

linked lists ftw! sorry can't say more... gotta run!

julz
Posted By: Uhrwerk

Re: Dynamic variables/structs/entintys/etc names - 12/10/08 21:23

That depends on the purpose. In some cases array lists are to be preferred.
Posted By: Carlos3DGS

Re: Dynamic variables/structs/entintys/etc names - 12/15/08 02:27

Thanks for that example! That is more like what im trying to do.
I have found a tutorial on linked lists and will start to learn that also as was sugested.
Thanks to all
Posted By: Carlos3DGS

Re: Dynamic variables/structs/entintys/etc names - 12/15/08 02:45

I have been reading about the malloc() and free() instructions and I am kind of worried of "forgetting" to free() memory I have reserved. It sounds very easy to create programs with memory leaks when you start using those instructions!

I have a couple of questions:

If I forget to free() memory allocated with malloc()...
When the engine shuts down will all that reserved memory that still has not been released with "free()" stay "blocked" forever untill I turn off my computer?
Or when the program is closed, will the engine automatically "free()" all the memory that was still reserved because I forgot to release it?
Posted By: Carlos3DGS

Re: Dynamic variables/structs/entintys/etc names - 01/04/09 19:57

Anyone know?
Posted By: Carlos3DGS

Re: Dynamic variables/structs/entintys/etc names - 01/04/09 19:58

Does anyone know a good beginner's tutorial forlinked lists?
And also a more advanced one for when I finish learning a basic one?
Posted By: EvilSOB

Re: Dynamic variables/structs/entintys/etc names - 01/04/09 20:33

I cant help with linked lists, but apparently any memory you malloc
WILL be cleanly "free'd" with the acknex engine closes.

The biggest fear I find is free'ing a block thats already free,
as this causes intermittant crashes, so I wrote this malloc
replacement code. Hope it helps...
Malloc replacement contribution
Posted By: Mr Wurm

Re: Dynamic variables/structs/entintys/etc names - 01/05/09 00:12

Theres a lovely tut on precisely what you're searching for in the tut section of acknex.

linked lists

Have been playing with malloc myself lately, fun stuff, haven't managed to crash my 'puter with it yet though...but i'm trying smile
Posted By: pegamode

Re: Dynamic variables/structs/entintys/etc names - 01/05/09 08:29

Maybe you should take a look at my GSHashmap ...

In many cases it can replace "linked lists" and it can be handled much easier.

I use it in my click&point adventure to handle all useable objects (some hundreds) and save their properties over the whole game.

One of many advantages:

You don't need to do something like this:
Code:
ENTITY* soldier1;
ENTITY* soldier2;
ENTITY* soldier3;
etc...
ENTITY* soldier100;

You just need one:
Code:
ENTITY* soldier;

You can use this pointer x-times for all your soldiers and store each of them under a unique key in the hashmap. This key can be generated automatically for example in a for-loop just by converting the counter to a string and use this number as key. With those keys you can access your soldiers anytime you want.

Best advantage you from the GSHashmap if you use structs ... with the hashmap you can link entities with structs in a very easy way and you can still access the hashmap even if you change the level.

Regards,
Pegamode.
Posted By: Carlos3DGS

Re: Dynamic variables/structs/entintys/etc names - 01/11/09 09:23

Thanks everyone for your help. I will take a look closely at each approach before choosing one, but at a first glance they all look great!

Thanks!
© 2023 lite-C Forums