Raised This Month: $ Target: $400
 0% 

[Solved] Saving entities


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 05-31-2009 , 12:15   [Solved] Saving entities
Reply With Quote #1

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?
__________________
Achievements API - a simple way for you to create your OWN custom achievements!

Last edited by Xellath; 06-01-2009 at 11:23.
Xellath is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 05-31-2009 , 12:21   Re: Saving entities
Reply With Quote #2

Using fopen( file, "rt" ) + fgets() + parsing the string with parse() for example and you could save in a var.
__________________
Arkshine is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 05-31-2009 , 12:21   Re: Saving entities
Reply With Quote #3

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
__________________

Last edited by Hunter-Digital; 05-31-2009 at 12:23. Reason: arkshine's post was faster but I spend extra time searching for tutorial :P
Hunter-Digital is offline
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 05-31-2009 , 12:34   Re: Saving entities
Reply With Quote #4

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?
__________________
Achievements API - a simple way for you to create your OWN custom achievements!

Last edited by Xellath; 05-31-2009 at 12:38.
Xellath is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 05-31-2009 , 12:54   Re: Saving entities
Reply With Quote #5

That code should work I think, try it

aaand... if you want to remove entities when you look at them... yeah, that's a way... depends on what you really wanna do
__________________
Hunter-Digital is offline
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 06-01-2009 , 10:15   Re: Saving entities
Reply With Quote #6

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)?
__________________
Achievements API - a simple way for you to create your OWN custom achievements!
Xellath is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 06-01-2009 , 10:20   Re: Saving entities
Reply With Quote #7

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
__________________

Last edited by Hunter-Digital; 06-01-2009 at 10:23.
Hunter-Digital is offline
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 06-01-2009 , 10:37   Re: Saving entities
Reply With Quote #8

Quote:
Originally Posted by Hunter-Digital View Post
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!
__________________
Achievements API - a simple way for you to create your OWN custom achievements!

Last edited by Xellath; 06-01-2009 at 11:22.
Xellath is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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