Topic: SmoothEventReciever - error when key pressed

Im using the latest addons.zip 2008 06 09 with irrlicht 1.5.1. and Blitzmax 1.34. I get an error when I press any key ."Unhandled Exception:Attempt to index array element beyond array length.

on 5th line of OnEvent Method

' push the key
            m_char_buf = m_char_buf[..m_char_buf.Dimensions()[0] + 1]  <- here!!
            m_char_buf[m_char_buf.Dimensions()[0] - 1] = key
        
            m_key_buf_old[key] = m_key_buf[key]
            m_key_buf[key] = event.getKeyPressedDown()

I created the receiver with:-

Receiver:SmoothEventReceiver = SmoothEventReceiver.createReceiver()
device.setEventReceiver(Receiver)

Re: SmoothEventReciever - error when key pressed

greetings smile  my apologies on the response delay.  unfortunately i am unable to replicate this issue.  here is a modified hello world that prints keys pressed using the SmoothEventReceiver.  please give it a whirl and see if you get the issue here as well.  also, what version of BMAX are your running?  what OS?  we may need to rebuild mods.

SuperStrict
Framework Irrlicht.Core
Import Irrlicht.AddOns

?MacOS
Local device:IrrlichtDevice = ..
    IrrlichtDevice.Create(EDT_OPENGL, _DIMENSION2DI(640, 480), 16, ..
        False, False, False, Null)
?Not MacOS
Local device:IrrlichtDevice = ..
    IrrlichtDevice.Create(EDT_BURNINGVIDEO, _DIMENSION2DI(640, 480), 16, ..
        False, False, False, Null)
?

If Not device Then Return
        
device.setWindowCaption("Hello World! - Irrlicht Engine Demo")

Local Receiver:SmoothEventReceiver = SmoothEventReceiver.createReceiver()
device.setEventReceiver(Receiver)

Local driver:IVideoDriver = device.getVideoDriver()
Local smgr:ISceneManager = device.getSceneManager()
Local guienv:IGUIEnvironment = device.getGUIEnvironment()

guienv.addStaticText("Hello World! This is the Irrlicht Software renderer!", ..
    _RECTI(10,10,260,22), True)
    
Local mesh:IAnimatedMesh = smgr.getMesh("../../media/sydney.md2")
If Not mesh Then Return
Local node:IAnimatedMeshSceneNode = smgr.addAnimatedMeshSceneNode( mesh )

If node
    node.setMaterialFlag(EMF_LIGHTING, False)
    node.setMD2AnimationFromType ( EMAT_STAND )
    node.setMaterialTexture( 0, driver.getTexture("../../media/sydney.bmp") )
EndIf

smgr.addCameraSceneNode(Null, _VECTOR3DF(0,30,-40), _VECTOR3DF(0,5,0))

While device.run() 

    driver.beginScene(True, True, _SCOLOR(255,100,101,140))

    smgr.drawAll()
    guienv.drawAll()

    Local key:Int = Receiver.popkey()
    Print key

    driver.endScene()
EndWhile

device.drop()

Re: SmoothEventReciever - error when key pressed

Yep same thing even without pressing a key.

Im running blitzmax 1.34 on Windows XP64


Method PopKey:Int(bPeek:Int = False)
        Local key:Int = m_char_buf[0] <- stops here!!
        If Not bPeek Then m_char_buf = m_char_buf[1..]           
        Return key
    EndMethod

Re: SmoothEventReciever - error when key pressed

It seems like this bug is not fixed yet...

The problem is that newer BlitzMax versions want you to pay attention on these two things for array use:
1. You should use the field length to get the number of dimensions of an array instead of Dimensions()[0] (if you are not sure that it contains anything)!
2. You should check whether your array have any fields first before you try to access one of them!



Somewhere in the OnEvent Method in smootheventreceiver.bmx:

...
m_char_buf = m_char_buf[..m_char_buf.Dimensions()[0] + 1] 
m_char_buf[m_char_buf.Dimensions()[0] - 1] = key

m_key_buf_old[key] = m_key_buf[key]
m_key_buf[key] = event.getKeyPressedDown()
...

To work it could look like this:

...
m_char_buf = m_char_buf[..m_char_buf.length + 1]  '<- here!!
m_char_buf[m_char_buf.Dimensions()[0] - 1] = key 'The array will always contain something now so that it do not have to be corrected (yet)

m_key_buf_old[key] = m_key_buf[key]
m_key_buf[key] = event.getKeyPressedDown()
...

But there is also this method which will always let throw out an exception when you call it and no button was pressed:

Method PopKey:Int(bPeek:Int = False)
        Local key:Int = m_char_buf[0] '<- stops here!!
        If Not bPeek Then m_char_buf = m_char_buf[1..]            
        Return key
EndMethod

To avoid this you could edit those code to this one:

Method PopKey:Int(bPeek:Int = False)
        If m_char_buf[0].length > 0 Then '<- check for content
                  Local key:Int = m_char_buf[0]
                  If Not bPeek Then m_char_buf = m_char_buf[1..]            
                  Return key
        EndIf
        Return -1 'To give a "false" callback if there is nothing
EndMethod

Unfortunately there are also some more problems with that useful addon pack, I hope it will be improved next time.

Re: SmoothEventReciever - error when key pressed

greetings and thanks for the detailed report!  will see if i can get these implemented this evening.

Unfortunately there are also some more problems with that useful addon pack, I hope it will be improved next time.

definitely would never guarantee no bugs smile  please post as you find and i will do my best to knock them off!

Re: SmoothEventReciever - error when key pressed

there is a new version of the addons for download.