Help with your scripting

Mouse Clicking Twice? (SOLVED)

Mouse Clicking Twice? (SOLVED)

Postby Shando » 11 Dec 2013, 01:41

Hi All,

Got a small problem with Mouse clicking :(

When I use the following code:

Code: Select all
obj = 0; -- | Define a variable for our object ID

function onClick ( obj )
    sky.lprint ( "You clicked Object: " .. obj );
end


and click on an object in the scene, I get the following in the console:

Code: Select all
You clicked Object: 153
You clicked Object: 153


I'm using a Ttesports gaming mouse (Polling Rate 1000Hz and DPI at 1600). Reducing the Polling Rate to 125Hz still causes the above.

I suppose I could include some sort of check that the mouse hasn't been pressed recently??

Any help would be greatly appreciated.

Regards

Shando
Last edited by Shando on 12 Dec 2013, 02:16, edited 1 time in total.
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Mouse Clicking Twice?

Postby SolarPortal » 11 Dec 2013, 13:28

i cannot replicate this fault with our mouse. lol

a couple of things:
  • the onClick() event doesn't pass any arguments and is called on the object you are clicking.
  • put a print inside the onMouseDown() event and see how many times it is called when clicked.

The code below only clicks once for me:
Code: Select all
obj = 0;
function onInit(objID)
   obj = objID;
   --game.enableMouseRay(1);
end--

function onMouseDown(button,x,y)
--   sky.lprint("click");
   game.clickObject(0);
end

function onClick( )
   sky.lprint("You clicked me!     "..obj);
end


Maybe if you could send us your code for the setup clicking and clicking functions, this way we can see if it is hardware fault or if there is a code bug.
thanks :D

Edit: If you have an ordinary mouse like us lol :P, have you tried to see if that has the same issue to help rule out the software as faulty.
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Mouse Clicking Twice?

Postby Shando » 11 Dec 2013, 22:26

Hi SolarPortal,

Now I'm totally confused :?

In the API docs it says to use:

Code: Select all
void Basic_Events::onClick  ( int  object_ID )


but you have said to use the following:

Code: Select all
the onClick() event doesn't pass any arguments and is called on the object you are clicking


I have tried both ways, and there is no difference, except that I can't use

Code: Select all
entity.getPosition ( obj )


anymore as "obj" is always set to zero.

Anyway, my code is as follows:

Code: Select all
function onMouseDown ( nBtn, nX, nY )
    -- nBtn = 1 - Left Click
    -- nBtn = 2 - Right Click
    sky.setVar ( "nMouseBtn", nBtn );
    game.clickObject ( 0 );   
end


then in boardModel:

Code: Select all
obj = 0; -- | Define a variable for our object ID
nBtn = 0;

function onClick ( obj )
sky.lprint("obj = " .. obj);
    nBtn = sky.getVar ( "nMouseBtn" );
sky.lprint("nBtn = " .. nBtn);
    if ( nBtn == "1" ) then
sky.lprint("SPAWN");
        nLocX, nLocY, nLocZ = entity.getPosition ( obj );
        id = entity.spawn ( "altar", nLocX, 1, nLocZ, 0.01, 0.01, 0.01 );
        entity.addScript ( id, "/playerModel.lua" )
    end
end


Then, in playerModel:

Code: Select all
obj = 0; -- | Define a variable for our object ID
nBtn = 0;

function onClick ( obj )
    nBtn = sky.getVar ( "nMouseBtn" );
sky.lprint("nBtn = " .. nBtn );
sky.lprint("obj = " .. obj );
    if ( nBtn == "2" ) then
sky.lprint("DELETE");
        entity.delete( obj );
    end
end


Basically, I create the Playing Board using boardModels, and then place the playing pieces on top, using playerModels.

my log looks like this from placing, and deleting, 2 models:

obj = 125
nBtn = 1
SPAWN
obj = 125
nBtn = 1
SPAWN
obj = 126
nBtn = 1
SPAWN
obj = 126
nBtn = 1
SPAWN
nBtn = 2
obj = 197
DELETE
nBtn = 2
obj = 198
DELETE
nBtn = 2
obj = 199
DELETE
nBtn = 2
obj = 200
DELETE


My guess is that the raycast is seeing both models, but that wouldn't explain the fact that it's spawning 2 playerModels each time??

Edit: If you have an ordinary mouse like us lol :P, have you tried to see if that has the same issue to help rule out the software as faulty.


I do have another mouse somewhere, I'll have to see if I can track it down :)

Thanks for the help so far.

Regards

Shando
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Mouse Clicking Twice?

Postby SolarPortal » 11 Dec 2013, 23:34

sorry for the confusion shando, :P
I made a mistake lol.
The onClick() event does pass the objID hit.
Code: Select all
function onClick( objIDHit )


The part that confused me was seeing obj passed through, which is what we use for the objID sent through the onInit() event lol.
i do apologize for any inconvenience caused. :)
The best use variable would be "objIDHit" or "objIDClicked".
Because if you had added an onInit event, then the onclick might have had issues because of the "obj" variable.

We have updated the Lua API reference.

here is a file of the code you sent:
http://www.chi-ad.com/Skyline/downloads123/Temp/User/Shando_MouseClick.zip
Note: Copy the asset library folder over the top of yours as it contains a script file that is required for the scene

This file works perfectly for us and only ever clicks once on the objects.
It spawns at the position and deletes when the spawned object is right clicked.

BTW: We also tested two overlapping objects, and the onClick event only returned back from the object that was clicked.
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Mouse Clicking Twice?

Postby Shando » 11 Dec 2013, 23:52

Hi SolarPortal

i do apologize for any inconvenience caused. :)


No worries :)

I have tried your Scene, and it still does the same thing (i.e. doubles everything) :cry:

Code: Select all
obj = 4
nBtn = 1
SPAWN
obj = 4
nBtn = 1
SPAWN
nBtn = 2
obj = 7
DELETE
nBtn = 2
obj = 8
DELETE


I have also tried with another mouse, with the same results :?:

Any other ideas???

Regards

Shando
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Mouse Clicking Twice?

Postby Shando » 12 Dec 2013, 00:28

OK, bit of an update....

If you add a math.random(10) to the Y value in the Spawn MicroScript in the mouseclick scene:

Code: Select all
      id = entity.spawn ( "Sphere_small", nLocX, nLocY+math.random(10), nLocZ, 1, 1, 1 );


Then 2 spheres are spawned.

However, deleting works correctly! So, it looks like it is something to do with entity.spawn??

Regards

Shando

PS: Logging mouseDown shows that it is only called once.
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Mouse Clicking Twice? (SOLVED)

Postby Shando » 12 Dec 2013, 02:18

Hi SolarPortal,

Sorry, but it was my fault all along :oops:

I'd left the following line in my main onInit:

Code: Select all
   game.enableMouseRay ( 1 );


Once I removed it, everything works fine :D

Apologies for any inconvenience.

Shando
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Mouse Clicking Twice? (SOLVED)

Postby SolarPortal » 12 Dec 2013, 11:16

lol np, i am glad it is sorted out now :D
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 5 guests

cron