oo basics

Monkey Forums/Monkey Programming/oo basics

matty(Posted 2011) [#1]
Hello all, I have been a procedural programmer for many years and have just begun my oo experience with monkey.

I will have many things to learn but my first query is this-
If I have a class which I use as a template for different creatures and a separate class extending that for each instance, is there a way to automatically populate the fields of the instance with those of the template. And am I going about this in the appropriate way in an oo environment. Typically I read in from text files all the creature templates and use these templates as a base to generate the individual instances.

This was sent from my phone so please excuse errors in punctuation.

From Matt.


slenkar(Posted 2011) [#2]
yes, you create a method in creature
Class creature

Method init()
self.hit_points=30
End Method

End Class


and then create individual creatures
Class ork extends creature
Method init()
Super.init()
self.teeth=48
End Method
End Class

local o:ork=new ork
o.init()



JIM(Posted 2011) [#3]
Well, since there's no reflection and no memcpy, there's no way to do this automatically.

However, I would keep a "pointer" (note that I mean a reference by this) to the template in each instance and use those whenever needed. In addition, any variables that may change should be cloned in the instance.

Here's what I mean:

Type CreatureTemplate

Field MaxHP:Int
Field Sprite:Image
Field Name:String
Field IsFlying:Bool

End


Type CreatureInstance

Field Template:CreatureTemplate
Field MyPos:Float[2]
Field MyHP:Int
Field MyPersonalAwesomenessIndex:Float

Method New(templ:CreatureTemplate)

Self.Template = templ

End

End


Now, to create some fierce enemies, you just call:

Global DestroyerOfWorlds:CreatureInstance = new CreatureInstance(BunnyTemplate)
Global AnihilatorOfRealms:CreatureInstance = new CreatureInstance(ButterflyTemplate)


You don't actually copy values from the template, but you keep a reference tot he template which is easier and much more memory-efficient :)


matty(Posted 2011) [#4]
Thanks Jim that is what I am after - that's how I am used to doing it in blitz3d/blitzplus but wasn't sure how to go about it in monkey.


slenkar(Posted 2011) [#5]
JIM - could you post the code for BunnyTemplate? It would help me understand


JIM(Posted 2011) [#6]
Sure, BunnyTemplate is just an instance of CreatureTemplate:

Global BunnyTemplate:CreatureTemplate = new CreatureTemplate
BunnyTemplate.MaxHP = 100
BunnyTemplate.Sprite = LoadImage("amazing_bunny.png")
BunnyTemplate.Name = "Rabbitosaurus"
BunnyTemplate.IsFlying = True 'Hell yeah!


The point is that when you want to update/draw the creatures, you acces the template directly:

Function DrawCreatures()
    For c:CreatureInstance = EachIn MyCreatureList
        DrawImage(c.Template.Sprite, c.MyPos[0], c.MyPos[1])
        'as you can see, I'm using data from both the template AND the instance
    Next
End


@matty Glad to be of assistance. It took me a while to get used to OOP after Blitz3D, and when I went back to it, I couldn't write anything properly :)


slenkar(Posted 2011) [#7]
thanks,
is there an advantage of doing it with templates rather than doing it with 'extends' ?
(greater flexibility?)


JIM(Posted 2011) [#8]
I guess with the template-approach it would be easier to change a whole set of units (say upgrade "Archers" to "Riflemen") by just changing the template.

Also, with the "extends" approach, you still copy the template over all the instances, so it's a waste of memory. I know that memory waste is probably insignificant (even to smartphones), but it's good practice to write efficient code :)