Help Extending Classes with Methods?
Monkey Forums/Monkey Programming/Help Extending Classes with Methods?
| ||
hi. I have a Class TShaderGLSL which contains Method LoadShader:TShaderGLSL(filename). Note it returns TShaderGLSL. I have another class: Class ShadowTex Extends TShaderGLSL. How do I get LoadShader() to return a class that ShadowTex can use on instanciation? In other words when I do this: shadowtex = ShadowTex.LoadShader(filename) ...I get the error "cannot convert TShaderGLSL to ShadowTex". Using a cast results in segmentation faults when using other methods within the base class. What is the recommended/simplest way to do this? Is there a way without <parameters> or to allow an automatic <parameter> perhaps? |
| ||
Cast it. [monkeycode]shadowtex = ShadowTex(ShadowTex.LoadShader(filename))[/monkeycode] When you use a class name as if it was a function, you're converting a class to another class kind, if they're compatible (that is, is one extends the other). If the cast operation fails (there's no inheritance) the cast operation returns null. In the other hand, it looks like you're calling a function instead of a method in your example... ? |
| ||
Right, sorry it is a function... Casting it results in a segmentation fault (shadowtex is null) when I call another method on the next line: [monkeycode] Class ShadowTex Extends TShaderGLSL Field filter_type:Int =0 End Class ShadowShader Extends DefaultShader Implements TShaderRender Field shadowtex:ShadowTex Method New() shadowtex = ShadowTex(ShadowTex.LoadShader(filename)) shadowtex.LinkVariables() End End [/monkeycode] If I revert shadowtex back to a TShaderGLSL it works. ideas? I'm stumped. It also fails in HTML5. |
| ||
I don't think that could ever work as the class would have to be created with the extension in order for it to be accepted as the extended class. Have you considered making another method that returns a shadowtex instead. I am sure it would be kind of redundant to the loadShader method but I don't see any other way. Or another silly idea would be to create the shadowTex with a field for TshaderGLSL instead of extending it. |
| ||
. |
| ||
the class would have to be created with the extension in order for it to be accepted as the extended class Yes, thank you, that was what I was trying to wrap my head around! So I could do: [monkeycode] shadowtex = LoadShader<ShadowTex>(filename) '' <-- would have to do whole class & all extends ''...or shadowtex = ShadowTex(LoadShader(filename, shadowtex)) '' <-- returns shadowtex:TShaderGLSL [/monkeycode] or your option of placing the TShaderGLSL object in a ShadowTex field, and loading via [monkeycode]shadowtex.shader = new TShaderGLSL shadowtex.shader = LoadShader()[/monkeycode]. Thanks, that helps. |