Overriding a Global ?
Monkey Forums/Monkey Programming/Overriding a Global ?
| ||
Hi evereyone. I'm creating a platform game and I have a issue with globals... There's a main class 'Brick', and many subclasses that have their own designs and behaviors. In this main class I'd like to put as many methods as possible, which will be inherited by subclasses, so it will avoid to write code many times. Each subclass has a 'img:Image' variable that is global, and loaded at the start to be accessible from each instance. The problem is when I create methods or functions in the main class, I have a error, which is that there's no 'img' variable. I can't put a global 'img' in the main class, as it will be specific to each subclass. Is there a way to make a global and override it in the subclasses ? Thanks very much. |
| ||
In fact I can override a global, but in the inherited method it still refers to the superclass version of 'img', is there a way to resolve it ? Thanks. |
| ||
I don't think so. What you can do is make an abstract GetImage:Image() function which returns a different global image for each subclass. |
| ||
Thanks, than's what I've done, but as a method, since I have a function that loads the image file to be called first. As a function it has resulted a memory access violation, because the 'img' is null at start (I think). Class CannonBulletSwitchBrick Extends Brick Global img:Image Method GetImage:Image() Return Self.img End Function Load:Int() img = LoadImage("monkey://data/brick_cannon.png") Return 0 End End |
| ||
Yes, you can't call it until the images have been loaded. But you shouldn't be trying to draw bricks until then anyway! If you're unsure whether an image will be present at rendering time, check for null whenever you draw it, then you can draw a placeholder if necessary. Your way is fine too - you still have to load the image before you can use it, |
| ||
I load the data at the start anyway, so it works fine. The next step will be to load the level and draw each brick dynamically, probably from a XML file. |
| ||
it looks more like this, so you have no field img inside Class , means Self. is wrong Global img:Image Class CannonBulletSwitchBrick Extends Brick Method GetImage:Image() Return img End |
| ||
You're right, it was juste an attempt to resolve my issue. |
| ||
i not really understand your issue. but you can also share the image object and use a field. this field img should not exist also in brick class because the brick struct will be include in Bla Class Class Bla Extends Brick field img:Image Function Create:Bla(img:Image) local o:Bla = new Bla o.img=img Return o end local img = LoadImage("monkey://data/brick_cannon.png") local x:Bla= Bla.Create(img) |
| ||
By the way, I meant an abstract GetImage() method, as functions cannot be abstract. I have a tendency to use the terms interchangeably as when I started everything was called a function... |
| ||
It was not really a issue, but I wanted to spare code in each subclass, as there will be several types of bricks. |