Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamicaly create object 1

Status
Not open for further replies.

IPGuru

Vendor
Jun 24, 2003
8,391
GB
I am sure this yould be simple but I wnat to dynamicaly create objects at run time
in psuedo code

a="object name"
createobject(a,class)

which would then give me a new object of class

simmilar to the wy setattr can be used to dynamicaly create attibutes

(I am probably using incorrect terms here somewhere)
 
Hi

I see no reason for your request. The logical question would be the reversed one - the class name is stored as string :
Code:
a[teal]=[/teal][green][i]"[red]class[/red]_name"[/i][/green]
[COLOR=darkgoldenrod]createobject[/color][teal]([/teal]a[teal],[/teal][red]object[/red][teal])[/teal]
Because if you have the class as bareword, so you know it in coding time, you can instantiate it directly :
Code:
a[teal]={}[/teal]
a[teal][[/teal][green][i]'object name'[/i][/green][teal]]=[/teal][COLOR=darkgoldenrod]class_name[/color][teal]()[/teal]
To instantiate an object from a class who's name is stored as string, first [tt]import[/tt] the module with the given class definition, then :
Code:
a[teal]=[/teal][COLOR=darkgoldenrod]globals[/color][teal]()[[/teal][green][i]'class_name'[/i][/green][teal]]()[/teal]
I suppose this is what you need. Sorry if I am wrong.

Feherke.
 
This is mainly a learning exercise as oop is still new to me
And has only just started to click.

The current scenario is to create a simple tex adventure game.
I have a room class and though it wolud be usefull to create
All the rooms from a definition file rater than haard coding them
Hence the need to create each object on the fly.

Obviously a simpler solution is to store the data in
One or moor lists but I am experimenting to see if it can be
Achieved in a moore oop style.
 
Hi

Then I suppose you will have one room class and many room objects. So the class will be known from the beginning and the objects will be stored in a list ( or list of lists ). In which case my first solution code will be just fine.


Feherke.
 
Thanks
It wasn't exactly what I had in mind but it is actually a better solution as it eliminates some of the other problems I had for seen & make the data file stucture much simpler.

I was looking for an object equivalent of setattrs() & getattrs() but now like you I cant think of any examples where they be necessary.

1 star coming up
 
Hi

While thinking again to your problem, some circumstances could be better handled if you use both ways I mentioned before : both dictionary keys and classes identified with strings.

For example at the beginning you decide to have rooms with either solid or fluid floor. Your room class will handle those two situations accordingly.

Later you decide to add some rooms with special behaviors,like the floor changing after each step towards the fluid state. Instead of rewriting the base room class, you subclass it and add the new behavior only to the new class, leaving the base class untouched. Then you only load the module with the new class and edit your data :

[tt]-- room table/dbf/csv/etc --
id | name | class | floor ( 1 solid .. 0 fluid )
----+-------+----------+-------
1 | start | room | 1
2 | pool | room | 0
3 | trap | traproom | 1[/tt]

Code:
[b]class[/b] room[teal]:[/teal] [gray]# base room class[/gray]

  floor[teal]=[/teal]position[teal]=-[/teal][purple]1[/purple]

  [b]def[/b] [COLOR=darkgoldenrod]move[/color][teal]([/teal]self[teal],[/teal]direction[teal]):[/teal]
    self[teal].[/teal]position[teal]+=[/teal]direction

[b]class[/b] [COLOR=darkgoldenrod]traproom[/color][teal]([/teal]room[teal]):[/teal] [gray]# room subclass with altered behavior[/gray]

  [b]def[/b] [COLOR=darkgoldenrod]move[/color][teal]([/teal]self[teal],[/teal]direction[teal]):[/teal]
    self[teal].[/teal]floor[teal]-=.[/teal][purple]1[/purple]
    room[teal].[/teal][COLOR=darkgoldenrod]move[/color][teal]([/teal]self[teal],[/teal]direction[teal])[/teal]

[b]def[/b] [COLOR=darkgoldenrod]createobject[/color][teal]([/teal]name[teal],[/teal]clas[teal],[/teal]floor[teal]):[/teal] [gray]# function to create a room of any (sub)class[/gray]
  new[teal]=[/teal][COLOR=darkgoldenrod]globals[/color][teal]()[[/teal]clas[teal]]()[/teal]
  new[teal].[/teal]name[teal]=[/teal]name
  new[teal].[/teal]floor[teal]=[/teal]floor
  [b]return[/b] new

building[teal]={}[/teal]
building[teal][[/teal][green][i]'start'[/i][/green][teal]]=[/teal][COLOR=darkgoldenrod]createobject[/color][teal]([/teal][green][i]'start'[/i][/green][teal],[/teal][green][i]'room'[/i][/green][teal],[/teal][purple]1[/purple][teal])[/teal]
building[teal][[/teal][green][i]'pool'[/i][/green][teal]]=[/teal] [COLOR=darkgoldenrod]createobject[/color][teal]([/teal][green][i]'pool'[/i][/green][teal],[/teal][green][i]'room'[/i][/green][teal],[/teal][purple]0[/purple][teal])[/teal]
building[teal][[/teal][green][i]'trap'[/i][/green][teal]]=[/teal] [COLOR=darkgoldenrod]createobject[/color][teal]([/teal][green][i]'trap'[/i][/green][teal],[/teal][green][i]'traproom'[/i][/green][teal],[/teal][purple]1[/purple][teal])[/teal]
( Of course, ideally [tt]createroom()[/tt] would receive a single parameter ( the id or the name of the room ), then extract the rest of data from the storage itself. )


Feherke.
 
Some interesting Ideas
If I ever get a complete adventure I willlet you know whre to find it
however as this is realy just a self education process I suspect I will be done with it long beofre a develope a real plot.

Many thanks again
As languages go this is certainly no Dead Parrot :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top