Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Game programming

keyboard handler

All about the StoneScript

keyboard handler

Postby jdebroy » 17 Aug 2012, 03:55

Hi,
Is there a way of using a keyboard handler (like onKeyboardKeyUp) without placing it on the Users main AIs?

Let me explain a little more. To capture keystrokes or other input I have to create the handler on the User's main AIs and send events to the tagged or runtime created objects on my scene.
If I place an onKeyboardKeyUp handler on the Object's AImodel (a one not included on the User's Main AIs), there is no response. It would be great and simpler flow if I can just capture keystrokes directly on any AImodel.

In any case, the editor allows to include those handlers on object's AI, but there is no alert that the object's AI does not respond to user input, so it is confusing. Should it respond to keystrokes/user input or not?
jdebroy
Expert Boarder
Expert Boarder
 
Posts: 130

Re: keyboard handler

Postby Selzier » 17 Aug 2012, 04:14

The FPS Demo does this by using this code in the "InputCore" AI which IS included in the User Main AI. But sends the keyboard event off to the AICamera which IS NOT a part of the user main AI >

Code: Select all
    --Forward to camera
    local hScene = application.getCurrentUserScene ( )
    local hCam = scene.getTaggedObject ( hScene, "Camera" )
    if ( hCam )
    then
        object.sendEvent ( hCam, "AICamera", "onKeyboardKeyDown", kKeyCode )
    end
Selzier
Platinum Boarder
Platinum Boarder
 
Posts: 494
Location: Boise

Re: keyboard handler

Postby jdebroy » 17 Aug 2012, 13:22

Fair enough, thanks!
jdebroy
Expert Boarder
Expert Boarder
 
Posts: 130

Re: keyboard handler

Postby error454 » 19 Aug 2012, 00:22

This can be confusing at first but it makes much more sense once you understand the difference between User AI and Object AI.

User AI is AI associated with a user. Only users can type on a keyboard, move a mouse and use a joystick. This is why User AIs are the only AIs that receive those handler calls.

You can technically make any AI a user AI but it doesn't make sense to. For instance, an explosion AI that draws explosion animations. This AI doesn't require keyboard input and isn't tied to a single user.

You can simply forward events as posted above. But don't feel like you have to forward at the lowest level. For instance, instead of sending onKeyboardKeyUp to your object, you could send onMoveUp or onAccelerate if it makes your code/events easier.
error454
Platinum Boarder
Platinum Boarder
 
Posts: 655
Location: Portland, OR USA

Re: keyboard handler

Postby remiaboudib1 » 07 Nov 2012, 21:23

hello,
i'm trying to solve a similar problem i created a AI for an object and i place some code for on keyboard key down, and i created another AI for a second object and also he has some code in his onkeyboardkeydown....the problem is when i send event for both of them from the main AI it all mess up the send event goes for only one AI where is the problem???please help me
remiaboudib1
Fresh Boarder
Fresh Boarder
 
Posts: 17

Re: keyboard handler

Postby MrGamer » 07 Nov 2012, 21:52

Try to use this code in user ai
Code: Select all
local hScene = application.getCurrentUserScene ( )
if ( hScene~=nil )
then
   scene.sendEventToAllObjects (hScene , "*","onKeyboardKeyDown" ,kKeyCode  )
end



it will send key down to every object in scene and you can handle input there.
MrGamer
Expert Boarder
Expert Boarder
 
Posts: 137


Return to Game programming