CONST in a CLASS?
Monkey Forums/Monkey Programming/CONST in a CLASS?
| ||
Is it possible to have a const array inside a class? It doesn't seem to be working for me. The following code works fine:- Const MOO :Float[3] Const PARP :Int = 0 so does:- Const MOO :Float[3] Class WHOOP Const PARP :Int = 0 End however this doesn't work:- Class WHOOP Const PARP :Int = 0 Const MOO :Float[3] End I get the error:- "Compile Error <Expr> cannot be statically evaluated" I am guessing then for whatever reason it isn't possible to have an array of CONST variables inside a class? |
| ||
The problem is the array. You're declaring an empty array. Comment it out, or try adding values to it like: Const MOO:Float[3]=[1.0,2.0,3.0] |
| ||
Hi Wiebo, thanks for the post. I tried that first too before I posted here. If I copy that into a class I get:- "Compile Error Syntax error - expecting class member declaration." |
| ||
This works over here: Const a:Int[] = [1,2,3] |
| ||
Hi Weibo, after much hunting and testing I finally figured it out. I think it might be a Monkey bug. Declaring it works (anywhere) but it fails to declare when you try to use it anywhere in the code. Try this:- Import mojo Const temp_float1:Float[] = [1.0,2.0,3.0] Class Moo Const temp_float2:Float[] = [1.0,2.0,3.0] Field temp_x:Float[3] End Class TestGame Extends App Field TheMoo:Moo Method OnCreate () TheMoo = New Moo End Method OnUpdate () For Local i:Int = 0 To 2 'TheMoo.temp_x[i] += TheMoo.temp_float2[i] 'TheMoo.temp_x[i] += temp_float1[i] Next End Method OnRender () End End Function Main () New TestGame End It will compile unless you comment out either of the lines inside the OnUpate loop. So clearly they aren't being created properly (or at all) Trivial of course but not using a const array works fine Import mojo Class Moo Const temp_float1:Float = 1.0 Const temp_float2:Float = 2.0 Const temp_float3:Float = 3.0 Field temp_x:Float[3] End Class TestGame Extends App Field TheMoo:Moo Method OnCreate () TheMoo = New Moo End Method OnUpdate () TheMoo.temp_x[0] += TheMoo.temp_float1 TheMoo.temp_x[1] += TheMoo.temp_float2 TheMoo.temp_x[2] += TheMoo.temp_float3 End Method OnRender () End End Function Main () New TestGame End |
| ||
It looks like it is impossible to create a constant array of floats.Const UNIT_SPEED:Float[] = [0.1,0.2,0.3] This results in "Expression cannot be statically evaluated", outside of a class as well. |
| ||
In which case I guess something like this would be a practical alternative:Const UNIT_BASE_SPEED:Float = 0.1 Const UNIT_SPEED:Int[] = [1,2,3] Local mySpeed:Float = UNIT_BASE_SPEED*UNIT_SPEED[2] Computing the float from a base number and an int multiplier... Edit: Whoops, that gives the same error message. So a constant array of ints also isn't allowed. I am using MonkeyPro 38. |
| ||
This has never worked for me, just use Global instead. |