About Store Forum Documentation Contact
Donations:
309$/mo



Post Reply 
Iterate over object in code
Author Message
mixpro Offline
Member

Post: #1
Iterate over object in code
Hi ,
seems like a silly question but i can't find answer for it .
In code how do iterate over object that are created in the editor .
for example i have created/imported 3d objects into the editor, lets say for each one i defined couple of parameters one of which is type, is there an option to do something like this in the code ?
Code:
Memc<ObjPtr> mylist;
foreach obj in editor :
     if (Compare(ObjPtr(obj)->getParam("type").asText(),"type1") {
          mylist.New() = ObjPtr(obj);
     }
(This post was last modified: 06-12-2016 02:35 PM by mixpro.)
06-12-2016 02:35 PM
Find all posts by this user Quote this message in a reply
Amnedge1331 Offline
Member

Post: #2
RE: Iterate over object in code
The simple way to do this is to put in your editor object an ObjClass like OBJ_ITEM for exemple (you can create ObjClass on left of editor).

Then you need to create a list of YourCustomObject like this:

Code:
Game.ObjMap<YourCustomObject > objItem;

Then when you load your world in your code like this:

Code:
Game.World.setObjType(objItem, OBJ_ITEM).New(world.id());

You need to add setObjType(objItem, OBJ_ITEM) to indicate that the world is loading that you want to use the creation of your objects with the class YourCustomObject.

Now your class YourCustomObject can be simple as:
Code:
class YourCustomObject : Game.Item
{
   Str       name;
  
    virtual void create(Object &obj)
   {
         super.create(obj);
         //get some custom parameters of your obj:
         if(Param *p=obj.findParam("name"))name=p.asText();
   }
  
}

And then you can iterate over your OBJ_ITEM like this:
Code:
REPA(objItem){
   //get the name of your object
   Str name=objItem[i].name;
}

Hope this help you to understand smile
(This post was last modified: 06-12-2016 09:10 PM by Amnedge1331.)
06-12-2016 09:08 PM
Find all posts by this user Quote this message in a reply
mixpro Offline
Member

Post: #3
RE: Iterate over object in code
thanks Amnedge1331 for the reply .
(06-12-2016 09:08 PM)Amnedge1331 Wrote:  
Code:
Game.World.setObjType(objItem, OBJ_ITEM).New(world.id());

You need to add setObjType(objItem, OBJ_ITEM) to indicate that the world is loading that you want to use the creation of your objects with the class YourCustomObject.

However it seems i'm doing something wrong as it didn't work .
first i had a problem world isn't defined so i assumed it is Game.World.id() .
for the sake of trial .
This is what i tried after :

a global :
Code:
Game.ObjMap<PathPiece > myPathPieces;

in bool Init()

Code:
Game.World.setObjType(myPathPieces, OBJ_PATH_PIECE).New(Game.World.id());

Code:
class PathPiece : Game.Static
{
   Str       name;
  
    virtual void create(Object &obj)
   {
         super.create(obj);
         //get some custom parameters of your obj:
         if(Param *p=obj.findParam("name"))name=p.asText();
   }
}
to test it :
in void Draw() :
Code:
Str name = Str("erer");
   REPA(myPathPieces){
      //get the name of your object
      name += myPathPieces[i].name+" ";
      
   }
   D.text(0, 0.7, name);

i had main two problems :
1 - the myPathPieces is empty (although i added to the editor object with class OBJ_PATH_PIECE).
2 - all other OBJ aren't showing anymore (those OBJ aren't related to this change and worked before the added code).
(This post was last modified: 06-13-2016 07:55 PM by mixpro.)
06-13-2016 07:49 PM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Bronze Supporter

Post: #4
RE: Iterate over object in code
well do you set the world ? this way will only works if Game.World.id() is defined
06-13-2016 08:49 PM
Find all posts by this user Quote this message in a reply
mixpro Offline
Member

Post: #5
RE: Iterate over object in code
(06-13-2016 08:49 PM)RedcrowProd Wrote:  well do you set the world ? this way will only works if Game.World.id() is defined

sorry i don't understand , i'm new to the engine , this is the code relating to world in my app , is this what you mean :
Code:
Game.World.activeRange(D.viewRange())
             .setObjType(pathCreator.Items, OBJ_ITEM)
             .setObjType (pathCreator.Chrs, OBJ_CHR)
             .setObjType(pathCreator.ObjParticles  , OBJ_PARTICLES)
             .setObjType(pathCreator.ObjLightPoints , OBJ_LIGHT_POINT)            
             .New        (UID(1499022699, 1542674969, 3460522410, 584747989)) // load "path" world
             //.New        (UID(3199326727, 1331546700, 2335466931, 1420158981)) // load "path" world
             .update     (Cam.at);
   if(Game.World.settings().environment)Game.World.settings().environment->set();
  
   //Game.World.setObjType(pathCreator.powerUps.powers, OBJ_POWERUR).New(world.id());
   Game.World.setObjType(myPathPieces, OBJ_PATH_PIECE).New(Game.World.id());
06-13-2016 09:05 PM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Bronze Supporter

Post: #6
RE: Iterate over object in code
yeah you dont call more than once the .new else i bet its gonna erase the previous one.
Code:
Game.World.activeRange(D.viewRange())
             .setObjType(pathCreator.Items, OBJ_ITEM)
             .setObjType (pathCreator.Chrs, OBJ_CHR)
             .setObjType(pathCreator.ObjParticles  , OBJ_PARTICLES)
             .setObjType(pathCreator.ObjLightPoints , OBJ_LIGHT_POINT)
             .setObjType(myPathPieces, OBJ_PATH_PIECE)
             .New        (UID(1499022699, 1542674969, 3460522410, 584747989)) // load "path" world
            
             .update     (Cam.at);
   if(Game.World.settings().environment)Game.World.settings().environment->set();
(This post was last modified: 06-14-2016 12:37 AM by RedcrowProd.)
06-14-2016 12:36 AM
Find all posts by this user Quote this message in a reply
mixpro Offline
Member

Post: #7
RE: Iterate over object in code
yeah the new() was the problem ,
but i think there is a misunderstanding here , as i already use setObjType() for other object .
but the container which in my case is Game.ObjMap<PathPiece > myPathPieces; gets filled only when adding objects to the world . thats not what i wanted .
what i wanted is to fill the container with objects that i have under Objects directory (as in load all the assets from Objects directory).
(This post was last modified: 06-14-2016 06:08 AM by mixpro.)
06-14-2016 05:39 AM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Bronze Supporter

Post: #8
RE: Iterate over object in code
well the objmap is only being populated by item already in the world that you have set up.

do you mean you want to have like an enum with all your items in it ? and populate your container with all those items once ?
06-14-2016 06:36 AM
Find all posts by this user Quote this message in a reply
mixpro Offline
Member

Post: #9
RE: Iterate over object in code
(06-14-2016 06:36 AM)RedcrowProd Wrote:  well the objmap is only being populated by item already in the world that you have set up.

do you mean you want to have like an enum with all your items in it ? and populate your container with all those items once ?

yeah , so i don't have to do this for each object i have:
Code:
path_1 = ObjectPtr(UID(2100919809, 2727852046, 389261100, 3283549145));
   path_2 = ObjectPtr(UID(3943458353, 3613519865, 264939655, 1198447635));
   path_1_2 = ObjectPtr(UID(3049310036, 2401826366, 1581994773, 2552875492));
   path_2_1 = ObjectPtr(UID(2087514144, 1375119068, 161974145, 644244022));
  
   obs_1 = ObjectPtr(UID(2974921294, 1562076937, 2171427043, 2778642899));
06-14-2016 06:51 AM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Bronze Supporter

Post: #10
RE: Iterate over object in code
something like this might work:
Code:
class ClassInfo
{
   enum AllyourEnumhere
   {
      Spook,
   }
   AllyourEnumhere MyEnum;
  
   UID  myUID;
}
ClassInfo MyClass[]=
{
   {ClassInfo.Spook, UID(4228050782, 1084811514, 2321151892, 3995373297)},

int MyClasses() {return Elms(MyClass);}

and you can just populate it with more, so you have like a class with 1 enum with all the UID in it. that's normaly how i use it.

the same way, like if i want to have dif. trees in a forest, i can simply set the obj to be random between 0 and 4, or like for crafting system, i have a system that take the obj at the same pos as my GUIenum, so if i ever want to have more craft, i can just add a line in there, and that's it smile
(This post was last modified: 06-14-2016 07:03 AM by RedcrowProd.)
06-14-2016 07:00 AM
Find all posts by this user Quote this message in a reply
mixpro Offline
Member

Post: #11
RE: Iterate over object in code
(06-14-2016 07:00 AM)RedcrowProd Wrote:  something like this might work:
Code:
class ClassInfo
{
   enum AllyourEnumhere
   {
      Spook,
   }
   AllyourEnumhere MyEnum;
  
   UID  myUID;
}
ClassInfo MyClass[]=
{
   {ClassInfo.Spook, UID(4228050782, 1084811514, 2321151892, 3995373297)},

int MyClasses() {return Elms(MyClass);}

and you can just populate it with more, so you have like a class with 1 enum with all the UID in it. that's normaly how i use it

i really appreciate your trying to help , i really hope you would understand this time xD .
what you wrote is my problem , i don't want to insert the UIDs manually , let's say i have 200 object , so i have to do ClassInfo.Spook, UID(4228050782, 1084811514, 2321151892, 3995373297) for each one ?
i just want whenever my app starts create in some way a container with all the UIDs of my objects in it.
it would be nice if i can just do :
Code:
foreach obj under Objects dir :
     container[i] = obj.UID()


let's say i have four 3d objects of fruit (apple,orange,banana,kiwi), for each i have variations (like red apple , green ...) would define params like :
str type ,str color, str size, ....
in code (if i have that container) i would do :
Code:
foreach obj in container:
    ObjPtr p = ObjPtr(obj);
    if (Compare(p->getParam("type"),"apple")) {
         if (Compare(p->getParam("color"),"green")) {
               do stuff with ...
         }
    }
(This post was last modified: 06-14-2016 07:18 AM by mixpro.)
06-14-2016 07:14 AM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Bronze Supporter

Post: #12
RE: Iterate over object in code
oh i see, sorry then
Well i'm not sure if that's possible you will need Esenthel to tell, or someone that uses a system like that to comment here then =p

there might be a way to take all the OBJ from a directory but i've never seen someone posting it on this forum using it this way.

by the end of the day, you would need to create a giant if cluster for those items anyway, sometimes planning on simpler architecture is good

you can add different str and int and enum to the class as well.
(This post was last modified: 06-14-2016 07:34 AM by RedcrowProd.)
06-14-2016 07:32 AM
Find all posts by this user Quote this message in a reply
mixpro Offline
Member

Post: #13
RE: Iterate over object in code
(06-14-2016 07:32 AM)RedcrowProd Wrote:  oh i see, sorry then
Well i'm not sure if that's possible you will need Esenthel to tell, or someone that uses a system like that to comment here then =p

there might be a way to take all the OBJ from a directory but i've never seen someone posting it on this forum using it this way.

by the end of the day, you would need to create a geant if cluster for those items anyway, sometimes planning on simpler architecture is good.

Thanks for the help wink any way
06-14-2016 07:34 AM
Find all posts by this user Quote this message in a reply
Pherael Offline
Silver Supporter

Post: #14
RE: Iterate over object in code
You can do what you want using Editor Interface:

I'm made this quick code from tutorial
PHP Code:
ObjectPtr obj;
Memc<ObjPtrmylist;

Str message; if(!EI.connect(message))Exit(message);

   
Memc<Edit.ElmelmsEI.getElms(elms); // get a list of project elements
   
Str data_path=EI.dataPath(); // get game data path folder (which contains all game data files)

   
FREPA(elms)if(elms[i].type==Edit.ELM_OBJ// looks for OBJECT type project elems
   
{
      
Str elm_file=Edit.EditorInterface.ElmFileName(elms[i].iddata_path); // get the element file
      
obj    =elm_file// load obj from that file
      
if(CompareCI(obj->getParam("type").asText(),"type1"))
      {
          
mylist.New() = obj;  
      }  
   } 
But don't do that in actual game, it's only working with esenthel editor open. Still you can for example:
PHP Code:
ObjectPtr obj;
Memc<UIDmylist//<-- geting UID list for objects you want

Str message; if(!EI.connect(message))Exit(message);

   
Memc<Edit.ElmelmsEI.getElms(elms); // get a list of project elements
   
Str data_path=EI.dataPath(); // get game data path folder (which contains all game data files)

   
FREPA(elms)if(elms[i].type==Edit.ELM_OBJ// looks for OBJECT type project elems
   
{
      
Str elm_file=Edit.EditorInterface.ElmFileName(elms[i].iddata_path); // get the element file
      
obj    =elm_file// load obj from that file
      
if(CompareCI(obj->getParam("type").asText(),"type1"))
      {
          
mylist.New() = elms[i].id;  
      }  
   } 
and save 'mylist' to file, then load it in game.

Also, read this before: http://www.esenthel.com/?id=doc#Editor_N..._Interface
(This post was last modified: 06-14-2016 08:05 AM by Pherael.)
06-14-2016 07:58 AM
Find all posts by this user Quote this message in a reply
Post Reply