Help with your scripting

функция onKeyDown() [DECIDED]

функция onKeyDown() [DECIDED]

Postby ant0N » 07 Nov 2012, 06:32

Hello! please help out.
Code: Select all
   function onKeyDown( key )
      if(key=="y") then
         entity.setRotation( entity.getEntityIDFromTag("pla"), 7, 3, 2 )
      end
   end

when you press the "Y", the object is to rotate. But he only turns once.
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [РЕШЕНО]

Postby ant0N » 07 Nov 2012, 06:58

I already solved the problem :). just not the command was used. That's all right:
Code: Select all
   function onKeyDown( key )
      if(key=="y") then
         entity.roll( entity.getEntityIDFromTag("pla"), 1,  1 )
      end
   end
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [РЕШЕНО]

Postby ant0N » 07 Nov 2012, 07:22

it would be even better, if SetRotation rotate the object, but did not turn. This is done in other engines.
SetRotation - rotate
SetTurn - turn (in degrees)



Though, can I still do not understand correctly...
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 07 Nov 2012, 11:28

Good to see you are exploring the lua scripting :)

SetRotate(x,y,z) is an exposed system function that is used in the main engine for object direct rotation such as when you use the translation properties to rotate your object in the editor. It sets a fixed position based on the passed values from 0 not a relative position based on the previous rotation, this enables specific rotations to be applied. Whereas the roll is a relative rotation adding the amount onto the previous orientation.

We can not change the behavior of the setRotation() but I can look at making a turn(r,p,y) function to give accumulative roll, pitch, yaw in one function if you need this.
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: функция onKeyDown() [DECIDED]

Postby ant0N » 07 Nov 2012, 14:01

but I can look at making a turn(r,p,y) function to give accumulative roll, pitch, yaw in one function if you need this.

I don't understand this offer, please try again :)
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 07 Nov 2012, 14:15

np,
This is to work along side the entity.roll / Pitch / yaw I could make a new function called entity.turn(r,p,y)
This will handle all the roll/ pitch /yaw in one function. This would save calling entity.roll(..); entity.pitch(..); entity.yaw(..)
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: функция onKeyDown() [РЕШЕНО]

Postby ant0N » 07 Nov 2012, 14:18

it would be great!!!!! do it please
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [РЕШЕНО]

Postby ant0N » 07 Nov 2012, 14:21

how about entity.move and entity.translate?
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 07 Nov 2012, 14:31

we have entity.setPosition(id,x,y,z) which sets absolute positional values so yes we could make a move(x,y,z) but I am not sure if this would be efficient.
I have been using setPosition(...) inside the update to loop to create controlled movement as I found it has more control.

If you can explain what you would like it for as there may be another way around it. If not I will be very happy to add the function for you :D
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: функция onKeyDown() [РЕШЕНО]

Postby ant0N » 07 Nov 2012, 14:41

Code: Select all
entity.move(objectID,0,0,1)
entity.translate(objectID,0,0,1)

analogues of the teams from the Blitz3D
Code: Select all
MoveEntity(entity,x,y,z)
TranslateEntity(entity,x,y,z)

if you haven't worked with Blitz3D, I can paint more. ;)
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 07 Nov 2012, 14:49

It was a long time since I was on Blitz3D. I think I understand:

Code: Select all
function onUpdate()

      entity. move(1,0,0)

end


this would continuously move the object 1 unit to the right. I can add this for you :)

Isn't the translate similar to our setPosition(..)?
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: функция onKeyDown() [РЕШЕНО]

Postby ant0N » 07 Nov 2012, 17:25

please add this command!
function onUpdate()

entity. move(1,0,0)

end
it is much easier. 8-)
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 07 Nov 2012, 17:40

ok I will add it to our todo list for you ;)
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 07 Nov 2012, 20:59

We have added the 2 new commands:

entity.move(id,x,y,z);

entity.turn(id,pitch,yaw,roll);

These commands are relative, and increment from its previous value not absolute in command such as setPosition(...)

You can get your hands on these in the new patch release to be release within the hour ;)
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: функция onKeyDown() [РЕШЕНО]

Postby ant0N » 08 Nov 2012, 05:16

Thank you!!! it's great that you fulfill our desires. :D
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [РЕШЕНО]

Postby ant0N » 08 Nov 2012, 12:55

tell me how does this function:
Code: Select all
function onKeyDown( key )
end

it is caused by the constantly, when you press the corresponding key? or when you press a key, it will be called only once?
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 08 Nov 2012, 16:42

The key event is triggered when Skyline detects that the user has pressed a key. This event is only triggered once when the key is pressed.
The argument "Key" contains a string representing the key that was pressed.

Example:
Code: Select all
function onKeyDown( key )
   sky.lprint("Key =  "..key); -- any key will cause this line to be called
   
   if(key=="p") then
      sky.lprint("You pressed the P Key!"); -- only the "p" key will call this line.
   end
end


I am currently writing a "First Steps to Scripting" tutorial, this will cover the main system callbacks and how to work with them from Lua ;)
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: функция onKeyDown() [РЕШЕНО]

Postby ant0N » 09 Nov 2012, 03:11

I need a function, которая срабатывает constantly, when the key is pressed. There is such a function?
P.S. can't wait for the textbooks. :)
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 09 Nov 2012, 11:24

sorry there is no function to continuously send a key down event, this is usually done in script by setting a flag isPressed = 1 when the key is pressed then setting isPressed = 0 when the key is released. Then in your game loop check the state of isPressed and if isPressed=1 then do something, this is how you could move an object in game.

When coding there is generally many ways to achieve the same result.

What purpose do you require the function for?
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: функция onKeyDown() [DECIDED]

Postby ant0N » 09 Nov 2012, 11:33

for example in Leadwerks you can do so:
Code: Select all
function object:Update()

    if KeyDown(KEY_UP) ==1 then
        cube:Movef(0,0,1) -- cube moves when you press the "UP"
    end

end
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 09 Nov 2012, 12:14

Ok I see what you are saying!

We could add a flag to the system key event and let a lua library check the state of this flag? I will think about this for you ; )

But for now you can do it this way in Skyline:

Code: Select all
function onUpdate( timeDelta )
   if( moveFlagX == 1) then entity.move(obj,-0.001,0,0) end
   if( moveFlagY == 1) then entity.move(obj,0.001,0,0) end
end

function onKeyDown( key )
   if(key=="z") then moveFlagX = 1   end
   if(key=="x") then moveFlagY = 1   end
end

function onKeyUp( key )
   if(key=="z") then moveFlagX = 0   end
   if(key=="x") then moveFlagY = 0   end   
end
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: функция onKeyDown() [DECIDED]

Postby ant0N » 09 Nov 2012, 12:24

Thank You, NightHawk! This method is quite inconvenient, I thought what can be done easier.
These commands are necessary:
KeyDown(Key)
KeyHit(Key)
MouseDown()
MouseHit()
They will make the code shorter.
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 09 Nov 2012, 13:23

Yes I agree, any thing that will make the code shorter has to be good, as long as there is not performance difference. I will have a look at these commands later for you ;)
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: функция onKeyDown() [РЕШЕНО]

Postby ant0N » 09 Nov 2012, 14:41

Thank You!!! :D
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: функция onKeyDown() [DECIDED]

Postby StarFire » 09 Nov 2012, 19:44

We have added 2 new commands:

input.keyDown( String key ) This function returns 1 if the specified key is pressed and 0 if it is not pressed. This command is key specific.

input.releaseKey( String key ) This function is needed to prevent key lock when using a player is trying to move x and y. Eg z moves left and x moves right, there will be times when the keys cancel movement and could be frustrating to the player. By calling the releaseKey(key) function you can force the oppsoing key to free. This makes the player smoother to control.

Code: Select all
function onUpdate( timeDelta )
   if( input.keyDown( "z" ) == 1) then entity.move(obj,-0.001,0,0); input.releaseKey("x");   end
   if( input.keyDown( "x" ) == 1) then entity.move(obj,0.001,0,0);  input.releaseKey("z");   end
end


Note: This should be fine in most cases but may use a tiny amount more cpu than the longer Flag method I mentioned in an earlier post. The event driven approach generally uses less cpu

These new commands will be in the next release ;)

I will look at the other commands you mentioned.
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Next

Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 8 guests

cron