AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [Solved] Saving entities (https://forums.alliedmods.net/showthread.php?t=93654)

Xellath 05-31-2009 12:15

[Solved] Saving entities
 
Lets say that we spawned a entity on a map, how can we save it to a file?
Could I do something like this?

PHP Code:

  new g_File[128];
 
  [...]
 
  new 
file fopen(g_File"wt");
  new 
szData[128];
  new 
Float:vOrigin[3];
 
  new 
classname[32];
  
pev(entpev_classnameclassname31);
 
  while((
ent engfunc(EngFunc_FindEntityByStringentclassname) != ))
  {
      
pev(entpev_originvOrigin);
 
      
formatex(szData128"%f %f %f^n"vOrigin[0], vOrigin[1], vOrigin[2]);
      
fputs(fileszData);
  } 

Yet, if that saves the origin, how can I retrieve the origin again and load it?

Arkshine 05-31-2009 12:21

Re: Saving entities
 
Using fopen( file, "rt" ) + fgets() + parsing the string with parse() for example and you could save in a var.

Hunter-Digital 05-31-2009 12:21

Re: Saving entities
 
with parse()
it takes fragments from lines sepparated by spaces with max length and places them into variables

and see this tutorial: http://forums.alliedmods.net/showthread.php?t=46218

Xellath 05-31-2009 12:34

Re: Saving entities
 
Something like this then..

PHP Code:

   new szData[128];
   new 
sz1[16], sz2[16], sz3[16];
   new 
Float:vOrigin[3];
   new 
file fopen(g_File"rt");
 
   while (!
feof(file))
   {
       
fgets(fileszData128);
       
parse(szDatasz116sz216sz316);
 
       
vOrigin[0] = str_to_float(sz1);
       
vOrigin[1] = str_to_float(sz2);
       
vOrigin[2] = str_to_float(sz3);
   } 

Correct? Thanks a bunch, ark and digital.

EDIT:

How can I remove an entity? Could I use get_user_aiming and get the classname of the ent and remove it?

Hunter-Digital 05-31-2009 12:54

Re: Saving entities
 
That code should work I think, try it :P

aaand... if you want to remove entities when you look at them... yeah, that's a way... depends on what you really wanna do

Xellath 06-01-2009 10:15

Re: Saving entities
 
Everything works fine, atleast the saving/loading, but when it's loading the entities, it just.. goes bad. Could I make a custom stock or something that spawns the ent at that origin, something like createEnt(vOrigin)?

Hunter-Digital 06-01-2009 10:20

Re: Saving entities
 
What do you mean by "it goes bad" ? what happens ?

PHP Code:

stock create_entity_at_origin(szClass[], Float:fOrigin[3])
{
      new 
ent create_entity(szClass)
      
entity_set_origin(entfOrigin)
 
      return 
ent


not tested but should work, returns the entity's index

Xellath 06-01-2009 10:37

Re: Saving entities
 
Quote:

Originally Posted by Hunter-Digital (Post 839517)
What do you mean by "it goes bad" ? what happens ?

PHP Code:

stock create_entity_at_origin(szClass[], Float:fOrigin[3])
{
      new 
ent create_entity(szClass)
      
entity_set_origin(entfOrigin)
 
      return 
ent


not tested but should work, returns the entity's index

It spawns all the ents at the same place, and you can't load. But I remade it now, I'm going to test it after dinner, I'll report back later.

EDIT: Quick test. I saved 5 ents, when I loaded them, 1 ent was loaded and spawned in the middle of the map. Something is not right. Dinner now, more testing later.

EDIT2: The number of spawned ents is now fixed, the only problem is the origin where they're spawning.

EDIT3: Never mind, solved. Thanks for the help, digital!


All times are GMT -4. The time now is 13:51.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.