Passing a class name as a method parameter

Monkey Forums/Monkey Programming/Passing a class name as a method parameter

Gil(Posted 2012) [#1]
Here's the situation...

I have a method called 'MakeParticles() which creates particle objects of class 'simpleParticle'.

I have a custom class 'specialParticle' that is derived from the 'simpleParticle' class.

I want to pass the name of my custom class as a parameter to MakeParticles(), and if that parameter is not null, then MakeParticles() should make particles of type 'specialParticle' instead of the default 'simpleParticle'.

Thanks in advance for the help!


Jesse(Posted 2012) [#2]
The problem would be if you are using field variables from the derived class in the SimpleParticle class which is not going to happen. the base class doesn't recognize extend object fields or variables. if not then it should be pretty straight forward.

what you can do is use a "new" in the base class and in the derived class and create them correspondingly and use polymorphism. it would make it more viable.


Gil(Posted 2012) [#3]
Jesse: Nope, the other way around. I will only be using field variables from the base class at the point of object creation and instantiation. Once the derived class object is created, it will be running it's own Methods which can safely access its unique field vars.

My problem is I don't know the syntax to pass a classname into a Method parameter and then use that parameter variable in a 'New() ClassNameParameter' format.

my pseudo-code for how I would expect it to look would be:

[monkeycode]

Method MakeParticles(x:Float, y:Float, className)

Local particle:className

particle = New() className
particle.x = x
particle.y = y

End Method

[/monkeycode]


marksibly(Posted 2012) [#4]
Hi,

One way to do this is to use a 'factory' class, eg:

Class Particle
	Field x#,y#
End

Class ParticleFactory
	Method CreateParticle:Particle() Abstract
End


You then pass ParticleFactory objects to MakeParticle, eg:

Method MakeParticle(x:Float, y:Float, factory:ParticleFactory )

  Local particle:Particle

  particle = factory.CreateParticle()
  particle.x = x
  particle.y = y

End Method


You then write a new factory class for each particle class, eg:

Class RedParticle Extends Particle
	Global Factory:=New RedParticleFactory
End

Class RedParticleFactory Extends ParticleFactory
	Method CreateParticle:Particle()
		Return New RedParticle
	End
End


So to make a RedParticle...

MakeParticles( 10,20,RedParticle.Factory )


Another way is to use reflection and ClassInfo objects.


Jesse(Posted 2012) [#5]
I doubt that is possible in Monkey. But I might be wrong.

[edited]
after seeing Mark's post , yea, that would do it. but I am sure the idea is to reduce the amount of code which make the whole thing pointless IMO.


Gil(Posted 2012) [#6]
Mark,

Thanks a bunch for that reply. I've learned something valuable and you elegantly solved my problem in the process. :)

BTW - I've been a silent fan of yours for decades when I first came across your work when I bought Blitz something or another for the Commodore Amiga many, many years ago. I credit that language with pushing me into better understanding object oriented coding and shortly thereafter I moved on to C and assembly on a PC and wrote my first 'true' videogame. That game landed me a game programmer position with a California game developer.

So that's my long winded thanks for your work and how it helped shape me as a programmer!

Cheers!


therevills(Posted 2012) [#7]
Could this work using generics:

[monkeycode]Method MakeParticles(x:Float, y:Float, p:T)

Local particle:className

particle = New T
particle.x = x
particle.y = y

End[/monkeycode]

?