How can I set the entity name in script?

Posted By: pegamode

How can I set the entity name in script? - 08/30/08 07:26

Hi everybody,

when I set an entity in WED it gets a name by WED.

How can I set this entity name when I create the entity in script by ent_create ???

I tried it as follows:

player1 = ent_create (str_create("giana.mdl"), vector(489 , -71 , -150), NULL);
wait(1);
player1.link.name = "giana.mdl_02";

That causes no compiler problem at all, but doesn't set the name either :-(

Regards,
Pegamode.
Posted By: MadJack

Re: How can I set the entity name in script? - 08/30/08 10:18

What about :

player1.name = "giana.mdl_02";

Or maybe:

str_cpy(player1.name ,"giana.mdl_02");
Posted By: pegamode

Re: How can I set the entity name in script? - 08/30/08 10:49

I tried both already ... with no success:

both result in : name is not a member of ENTITY

I also tried:

str_cpy(player1.link.name,"giana.mdl_02");

compiles, but doesn't set entity's name.
Posted By: EvilSOB

Re: How can I set the entity name in script? - 08/30/08 14:25

Errr, whats wrong with calling it by the name you created
it under, as in "player1" from your examples?

Where is there a diffeerence?
Posted By: pegamode

Re: How can I set the entity name in script? - 08/30/08 14:45

If an entity is set in WED and you call (entity->link).name the name is returned.

If an entity is created by ent_create and you call the same NULL is returned.

So an entity created by ent_create has no name ...
Posted By: sadsack

Re: How can I set the entity name in script? - 08/30/08 15:43

It should have an entity number.
Posted By: pegamode

Re: How can I set the entity name in script? - 08/30/08 20:33

As a workaround I'm currently using entity.type which works fine enough in my case ... but I would prefer to set the entity's name.
Posted By: sadsack

Re: How can I set the entity name in script? - 08/30/08 21:08

pegamode this may help you


handle (object);
Returns a handle of an engine object. A handle is a unique object number that can be assigned to any variable, and identifies that object.
Parameters:
object - ENTITY*, STRING*, BMAP*, VIEW*, PANEL*, TEXT*, FONT*, SOUND*, or MATERIAL* pointer.
Returns:
Handle of object.
Speed:
Fast
Remarks:
A handle is similar to a pointer, but is just a number and therefore can be assigned to any variable or skill.
For getting a handle to a lite-C struct that is not an engine object, use the pVars pointer.
After saving and loading a game, a handle generated by this instruction is guaranteed to reference always the same object - this is not guaranteed for pointers that are just references to memory areas.
On a single player system, the entity handle is valid immediately after entity creation ( (► ent_create). On multiplayer systems, it can take up to 0.5 seconds until the entity is created on all connected machines and the handle becomes valid. During that time the entity handle can not be used.
For using a handle to get the referenced object, it can be converted to a pointer through the ptr_for_handle instruction.
The handle is the first integer of an object. It consists of a unique index number plus an object type identifier taken from the following table, and left shifted by 24. The handle can be used to identify the type of the object. For converting the handle to a var that contains the index number, right shift it by 24, then left shift the resulting int by 10.
INVALID -1
ENTITY 4 (global)
ENTITY 5 (local)
ENTITY 8 (layer)
STRING 10
FONT 11
BMAP 12
MATERIAL 13
SOUND 14
STREAM 15
TEXT 20
PANEL 21
VIEW 22

Example:
if(entity)
{
my.skill48 = handle(entity); // store entity pointer
}
you = ptr_for_handle(my.skill48); // restore YOU;
See also:
ptr_for_handle, pVars

► latest version online
Posted By: EvilSOB

Re: How can I set the entity name in script? - 08/30/08 22:35

Originally Posted By: pegamode
If an entity is set in WED and you call (entity->link).name the name is returned.
Correct.
Originally Posted By: pegamode
If an entity is created by ent_create and you call the same NULL is returned.
Correct, but un-important.
Originally Posted By: pegamode
So an entity created by ent_create has no name ...
Incorrect, true it has no name in link, but using link as a name is a BAAD habit.
The link info really only tells you about how it was created in WED or 'originally' as a global object.

The name you 'should' be using, youve already supplied in your examples. -> player1
Because you will use something like the following to create your entity,
player1 = ent_create (str_create("giana.mdl"), vector(489 , -71 , -150), NULL);
you can from here on refer to this entity as "player1", if you want to rename it
on-the-fly eek then you can always do something like this...
Code:
   ENTITY* player1;         //outside all functions - global pointer is available to all
   ENTITY* giana_mdl_02;    //outside all functions - global pointer is available to all
   ...

   ...
   // inside some function 
   //
   if( player1 != NULL )    // make sure player has been created
   {
      giana_mdl_02 = player1;   // 'aim' new name at our entity        (this line perfectly fine)
      player1 = NULL;           // 'remove' old name from our entity   (try to avoid this line, can get messy)
   }
   ...

from now on you access the same entity, by the new name of "giana_mdl_02". Is this what you're after?

So what do you need the link name for? How do you plan on using it?
If we knew that we would be better able to suggest a better way of doing what you want,
rather than messing around with un-trustworthy INTERNAL data structures.

Posted By: pegamode

Re: How can I set the entity name in script? - 08/31/08 07:16

Hi everybody,

thanks for your answers, but I think I have to explain why I wanted to get / set the entity's name.

I use it as a kind of primary key to store the pointers to my structs that are attached to some of my entities in a hashmap.

For sure it's just one of many things I can use as a primary key, for example the entity's ID, but I liked it because (for debugging reasons) it's more readable than an ID. For that reason I'm currently using entity.type + "_" + ID. That's unique and readable.

One more reason was that I use MadJacks Pathfinding DLL that crashed every time I attached it to an entity created by ent_create ... after debugging the code it turned out that he uses also the entity's name.

Currently I don't have any more issues on that, but as there is not really much information on the entity's name in the manual (or I didn't find it) and I asked myself how I could set that name or why it should be reserved for WED's used only ?!?

Best regards,
Pegamode
Posted By: EvilSOB

Re: How can I set the entity name in script? - 08/31/08 07:57

Aha, I see. Ive never played with hashmaps and havent really tried to understand the concepts yet,
thats a job for later...

BUT, here is an exceptionally ugly and mem-leaky little piece of code that I use in a tool Im building
that creates light entities and 'gives' them unique names.
Code:
   LightEnt = ent_create(Globe_MDL, nullpointer, NULL);
   STRING* tmp = str_create("GlobeLight_01");
   LightEnt.link.name = tmp.chars;
Its dirty but it works.
The concept may give you ideas and if you come up with something similar it may solve MadJacks crashes.

I'll be redoing my above snippet in an estimated few days to be 'clean' by using malloc instead
of a sacrificial string. If you want it, I'll post it in here when its done.
Posted By: MadJack

Re: How can I set the entity name in script? - 08/31/08 09:51

Just a precision, in my DLL I assumed that each entity had a name. My mistake blush
Since Pegamode has pointed that it was not true using ent_create, we have corrected the DLL code.

But for general purpose, it might be interesting to set the entity name by script...

Pegamode, did you test EvilSOB's solution ?
Posted By: pegamode

Re: How can I set the entity name in script? - 08/31/08 12:19

I tried it that way:

STRING* tmp = str_create("GlobeLight_01");
LightEnt.link.name = tmp;

With no compiler error, but still NULL in link.name in the DLL.

I'll test

LightEnt.link.name = tmp.chars;

as soon as possible.
Posted By: pegamode

Re: How can I set the entity name in script? - 08/31/08 12:28

hmmm ...

very strange, I checked it again and now both variants are running well ?!?

Also LightEnt.link.name = "GlobeLight_01"); runs well.

I don't know why, but as I checked it yesterday I got NULL ... now it's working. Don't know where's the difference.

So, to sum it up:

The entity's name can be set using entityName.link.name and also string1 / string2 can be set with entityName.string1 although they are read-only.

Best regards,
Pegamode.
Posted By: EvilSOB

Re: How can I set the entity name in script? - 09/01/08 11:27

Yeah, I tried it this way to start too, but
LightEnt.link.name = tmp;
wont work cause the link.name REQUIRES its data to be JUST
a CHAR array, not a whole STRING structure. Big difference.

By using LightEnt.link.name = tmp.chars;, I am
only using the CHAR array element of the string structure.


Posted By: EvilSOB

Re: How can I set the entity name in script? - 09/02/08 04:50

OK guys, heres the Renamer snippet I promised.

Tweaked it a bit so now it will work on almost any object type,
BMAP,TEXT, ENTITY, etc.

Code:
   ...
   // example usages
   Rename_Link(Model.link, "New_Name1");
   ...
   STRING* tmpName = str_create("Test_Name_two");
   Rename_Link(Model2.link, tmpName);
   ...

function Rename_Link( C_LINK* Link, STRING* NewName )
{  // Either INSERTS or REPLACES the C_LINK.name field with supplied NewName.chars
   // If C_LINK* is NULL,  function will just exit
   // If STRING* is NULL,  function will replace C_LINK.name with NULL
   //
   if(Link==NULL)      return;      //Cant process UNDEFINED object
   if(Link.name!=0)    free(Link.name);   //Erase existing
   Link.name=0;                           //   Link Name
   if(NewName==NULL)   return;   //Leave Link name empty as requested
   Link.name = (char*)malloc((long)str_len(NewName)+1);   //create Name space
   memcpy(Link.name,_chr(NewName),str_len(NewName)+1);    // & copy Name into space
}

Any questions or problems, let me know.
Enjoy.
© 2024 lite-C Forums