HeroEngine Forums
Welcome, Guest. Please login or Register for HeroCloud Account.

Author Topic: basic Flying for character - toggle  (Read 3431 times)

Thazager

  • General Accounts
  • *
  • Posts: 1155
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
basic Flying for character - toggle
« on: Oct 27, 13, 10:23:42 PM »

This will let your character fly around the area. It is a toggle that uses the "F" key.  The character can fly at any angle, this looks best in 1st person view.  It adds 2 keys for going straight UP and straight DOWN. It adds 3 lines to the keybindings.ini  file in the repository.

(There were many parts to this, I hope I got them all. This is probably not the best approach to some sections, if you have better code feel free to add it in :) )

Download the GameKeyBindings.ini  file from the repository.
below    [Command] = Input_Command
add     
  Flying = F
  Up = ANY+HOME
  Down = ANY+END


make sure no other command is using the F key.  If it is, change 1 of the keys to another value. (only the last command set to F will use that key)

Upload the GameKeyBindings.ini file to the repository, in the same place you found it. (the main HE directory)
_________________________________________________________________________
In the DOM, create a new (server side) field
name = TMP_toggleFly
type = boolean

copy to client

set Delta Priority = 1
set Destination field (client) = TMP_toggleFly
set Change Callback = yes

In (server side) class E_CommonCharacter

add field -> TMP_toggleFly
_________________________________________________________________________
In the DOM, create a new (server side) field
name = TMP_Flying
type = boolean

copy to client

set Delta Priority = 1
set Destination field (client) = TMP_Flying
set Change Callback = yes

In (server side) class E_ACCController

add field -> TMP_Flying
_________________________________________________________________________
In (client side) class E_CommonCharacter
add field -> TMP_toggleFly

In (client side) class E_ACCController
add field -> TMP_Flying
_________________________________________________________________________
adding code to scripts to make it start and function.

In (server side) script E_CommonCharacterClassMethods,

in method  InitCommonCharacter()

under
    me.E_hitPoints = 100

add
Code: [Select]
      me.TMP_toggleFly = false    // init off


in method  ApplyDeath ()

under
    me.E_isDead = true

add
Code: [Select]
  if me.TMP_toggleFly
    me.TMP_toggleFly = false    // clear fly
  .

_________________________________________________________________________
In (server side) script E_PlayerAccountClassMethods,

add  function

Code: [Select]
// send the ToggleFly to the data area
untrusted function SetToggleFlyServer( accountID as ID, toggle as Boolean)
  acct as NodeRef of Class E_playerAccount = accountID
  char as NodeRef of Class E_playerCharacter = acct.GetMyChar()
  char.TMP_toggleFly = toggle
.
_________________________________________________________________________
In (client side) script E_ACCControllerClassMethods,

in method _onBehave()

under
  myChar as NodeRef = me._accControllerOwnerRef

add

Code: [Select]
  if me.TMP_Flying
    me.HeroEngineACCC_MoveType = NORMAL     //clear possible swim
    SetAniminput(mychar,"MoveType","Normal")
    SetCameraWaterCollision("GAME",None)
  .
---------------------------------------------------------------

under
          i = i + 1
above
        else if t == "forward"

add

Code: [Select]
        else if t == "up"
          if me.TMP_Flying
            me.HeroEngineACCC_NavMode = LOCAL
            me.HeroEngineACCC_State = MOVE
          .
          i = i + 1
        else if t == "down"
          if me.TMP_Flying
            me.HeroEngineACCC_NavMode = LOCAL
            me.HeroEngineACCC_State = MOVE
          .
          i = i + 1

---------------------------------------------------------------
near jumping
          //
          // JUMP BEHAVIOR
          //


under
  if (me.HeroEngineACCC_MoveType = SWIM)
    return
  .

add
Code: [Select]
  if me.TMP_Flying      // no jump when flying
    return
  .

---------------------------------------------------------------
in method _onPostAnimUpdate()

under
  myChar as NodeRef = me._accControllerOwnerRef

add
  me.checkToggleTravel()     // fly check

---------------------------------------------------------------
change
          if (me.HeroEngineACCC_State != DEAD)

to
          if (me.HeroEngineACCC_State != DEAD and not(me.TMP_Flying))


---------------------------------------------------------------
change

    if not didCollision
      // grounding -- stick the character to the ground when not jumping/falling etc.
      me._HE_ACCC_Ground(mychar, elapsed)
    .

to
Code: [Select]
    if not didCollision
      if not me.TMP_Flying            // not when flying
        // grounding -- stick the character to the ground when not jumping/falling/flying etc.
        me._HE_ACCC_Ground(mychar, elapsed)
      .
    .

---------------------------------------------------------------
add function

Code: [Select]
// check for flying
method checkToggleFlying()
  acct as NodeRef of Class E_playerAccount = GetAccountID()
  if acct <> 0
    char as NodeRef of Class E_playerCharacter = acct.GetMyCharacter()
    if char <> 0
      me.TMP_Flying = char.TMP_toggleFly          // fly
    .
  .
.


---------------------------------------------------------------
in method _HE_ACCC_NavigateLocal()

near section     is CAMERA

under
        else
          me.HeroEngineACCC_LastCameraHeading = camrot.y
          me.HeroEngineACCC_DesiredRotation.y = -camrot.y
          me.E_ACCCRotationDone = false

above
        .

add
Code: [Select]
          if me.TMP_Flying
            me.HeroEngineACCC_DesiredRotation.z = -camrot.x     // set pitch angle
          else
            me.HeroEngineACCC_DesiredRotation.z = 0.0
          .


---------------------------------------------------------------
in method _HE_ACCC_Gravity()

under
    var Gravity = .98

add
Code: [Select]
    if me.TMP_Flying
      Gravity = 0.0
      me.setHJOveridePosture( Normal )    // set animation type here
    .


---------------------------------------------------------------

under
          if GetCmdState("Movement","Left")
            impulseDelta.z = -accelerationFactor

add
Code: [Select]
            if me.TMP_Flying
              impulseDelta.z = -accelerationFactor*8
            .

---------------------------------------------------------------

under
          if GetCmdState("Movement","Right")     
            impulseDelta.z = accelerationFactor

add
Code: [Select]
            if me.TMP_Flying
              impulseDelta.z = accelerationFactor*8
            .

---------------------------------------------------------------
change

Code: [Select]
          if dowalklimit
            vec as Float = SquareRoot(Power(me.HeroEngineACCC_ImpulseVector.x, 2) + Power(me.HeroEngineACCC_ImpulseVector.z, 2))
            if vec > walklimit
              var ratio = walklimit/vec
              me.HeroEngineACCC_ImpulseVector.x = me.HeroEngineACCC_ImpulseVector.x * ratio
              me.HeroEngineACCC_ImpulseVector.z = me.HeroEngineACCC_ImpulseVector.z * ratio
            .
          else       
            vec as Float = SquareRoot(Power(me.HeroEngineACCC_ImpulseVector.x, 2) + Power(me.HeroEngineACCC_ImpulseVector.z, 2))
            if vec > speed
              var ratio = speed/vec
              me.HeroEngineACCC_ImpulseVector.x = me.HeroEngineACCC_ImpulseVector.x * ratio
              me.HeroEngineACCC_ImpulseVector.z = me.HeroEngineACCC_ImpulseVector.z * ratio
            .
          .

to
Code: [Select]
          if me.TMP_Flying
            me.E_ACCCUserFallImpulse = true
            me.HeroEngineACCC_ImpulseVector.x = me.HeroEngineACCC_ImpulseVector.x * .9  // slow our drift
            me.HeroEngineACCC_ImpulseVector.y = me.HeroEngineACCC_ImpulseVector.y * .3
            me.HeroEngineACCC_ImpulseVector.z = me.HeroEngineACCC_ImpulseVector.z * .9
          else
            vec as Float = SquareRoot(Power(me.HeroEngineACCC_ImpulseVector.x, 2) + Power(me.HeroEngineACCC_ImpulseVector.z, 2))
            if dowalklimit
              if vec > walklimit
                var ratio = walklimit/vec
                me.HeroEngineACCC_ImpulseVector.x = me.HeroEngineACCC_ImpulseVector.x * ratio
                me.HeroEngineACCC_ImpulseVector.z = me.HeroEngineACCC_ImpulseVector.z * ratio
              .
            else       
              if vec > speed
                var ratio = speed/vec
                me.HeroEngineACCC_ImpulseVector.x = me.HeroEngineACCC_ImpulseVector.x * ratio
                me.HeroEngineACCC_ImpulseVector.z = me.HeroEngineACCC_ImpulseVector.z * ratio
              .
            .
          .


---------------------------------------------------------------
in method _HE_ACCC_Land()

under
  Behave( mychar, "killanim" )
  SetNodePosition(mychar,finalpos)

add
Code: [Select]
  if me.TMP_Flying    // do not land when flying is on
    return
  .

---------------------------------------------------------------
in method _HE_ACCC_DoSwimPhysics()

above
  pos as Vector3
  GetNodePosition(mychar,pos)

add
Code: [Select]
  if me.TMP_Flying    // fly overrides swim
    return
  .

---------------------------------------------------------------
in method _HE_ACCC_HandleWater()

above
  testWater as Boolean = true

add
Code: [Select]
  if me.TMP_Flying    // fly overrides swim
    return
  .

_________________________________________________________________________
In (client side) script Input_movement,

in function onCmdStart()


under
  when cmd

add
Code: [Select]
    is "Flying"
      FlyToggle()
      return true
    .
    is "Up"
      SetCmdState("Movement","Up",true)
      FigureOutMovement()
      return true
    .
    is "Down"
      SetCmdState("Movement","Down",true)
      FigureOutMovement()
      return true
    .

---------------------------------------------------------------
in function onCmdStop()

under
  when cmd

add
Code: [Select]
    is "Flying"
      FlyToggle()
      return true
    .
    is "Up"
      FigureOutMovement()
      return true
    .
    is "Down"
      FigureOutMovement()
      return true
    .

---------------------------------------------------------------
in function FigureOutMovement()

under
  var behavior = "move"
  var needtostop = true

add
Code: [Select]
  if char.TMP_toggleFly = true
    if GetCmdState("Movement","Up")
      behavior = behavior + " up"
      needtostop = false
    else if GetCmdState("Movement","Down")
      behavior = behavior + " down"     
      needtostop = false     
    .
  .

---------------------------------------------------------------
add function

Code: [Select]
function FlyToggle()
  acct as NodeRef of Class E_playerAccount = GetAccountID()
  char as NodeRef of Class E_playerCharacter = acct.GetMyCharacter()
  if (char.TMP_toggleFly = true)
    call server E_playerAccountClassMethods:SetToggleFlyServer(acct, false)
    char.TMP_toggleFly = false
    println("Fly off")  // print on console, that we are not flying
  else
    call server E_playerAccountClassMethods:SetToggleFlyServer(acct, true)
    char.TMP_toggleFly = true
    pos as Vector3
    var pc = GetPlayerCharacterNode()
    GetNodePosition(pc, pos)
    SetNodePosition(pc, (pos.x, (pos.y + 0.1), pos.z))                // add some height
    println("Fly on")  // print on console, that we are flying
  .
.

« Last Edit: Nov 10, 13, 05:09:21 PM by Thazager »
Logged

HistoriaOnline

  • World Owners
  • ****
  • Posts: 26
  • Aspiring MMO Creator
    • View Profile
Re: basic Flying for character - toggle
« Reply #1 on: Nov 09, 13, 07:17:44 AM »

15:16:05: SCRIPT:[Input_Command]:Got key(Up) but did not handle it
15:16:05: SCRIPT:[Input_Command]:Got key(Up) but did not handle it
15:16:06: SCRIPT:[Input_Command]:Got key(Up) but did not handle it
15:16:09: SCRIPT:[Input_Command]:Got key(Up) but did not handle it

any thoughts?


Fixed.
« Last Edit: Nov 09, 13, 08:44:18 AM by HistoriaOnline »
Logged

HistoriaOnline

  • World Owners
  • ****
  • Posts: 26
  • Aspiring MMO Creator
    • View Profile
Re: basic Flying for character - toggle
« Reply #2 on: Nov 09, 13, 07:53:36 AM »

Where is the function checkToggleFlying supposed to be ? and why is it never used?

How are you using a me object in the function? Im lost...

All in all every time i activate the F key my char jumps a bit up then gets snapped back to the floor (as if i'm guessing that the flag for flight isn't true)

:[E_ACCControllerClassMethods]:E_ACCControllerClassMethods line 1033 (_onPostAnimUpdate) has determined I should be falling
« Last Edit: Nov 09, 13, 08:43:36 AM by HistoriaOnline »
Logged

Thazager

  • General Accounts
  • *
  • Posts: 1155
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: basic Flying for character - toggle
« Reply #3 on: Nov 09, 13, 06:10:47 PM »

Thanks for finding those errors, I have added the place for checkToggleFlying in above. This checks for the toggle when the player pushes the key.


It should be in method _onPostAnimUpdate()
under
  myChar as NodeRef = me._accControllerOwnerRef

add
  checkToggleFlying(me)     // fly check

Also I needed to change the function to a method.

(This was converted from a number of scripts which I separated from the ACCController. I missed a few things in the conversion.)
« Last Edit: Nov 09, 13, 06:17:14 PM by Thazager »
Logged

HistoriaOnline

  • World Owners
  • ****
  • Posts: 26
  • Aspiring MMO Creator
    • View Profile
Re: basic Flying for character - toggle
« Reply #4 on: Nov 10, 13, 04:44:58 AM »

Thanks a lot for helping out and sharing, hopefully soon I will also be able to do the same =]

I'm guessing me.checktoggleflying() instead of checkToggleFlying(me).

I did everything, now flying turns on and off right away, maybe because he gets multiple key press when i press F?
not sure, ill try to redo everything again.

also, you had a return somewhere, I replaced that one with a break, since I cant return anything in that method.
maybe i'm wrong, still new to this hero script, used to java.
« Last Edit: Nov 10, 13, 05:27:36 AM by HistoriaOnline »
Logged

Thazager

  • General Accounts
  • *
  • Posts: 1155
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: basic Flying for character - toggle
« Reply #5 on: Nov 10, 13, 05:16:17 PM »

Quote
I'm guessing me.checktoggleflying() instead of checkToggleFlying(me).
Yes, thanks. Fixed above.

Return in some cases is used to exit the function/method without going further in the function.
function DoFlying()
  If  fly= false
     return         // skip fly function
  .

  ...  // do stuff
.

Break is used alot for exiting loops.
  loop i from 1 to 10
    if item = "done"
      doFunction()
      break
    .
  .
Logged

HistoriaOnline

  • World Owners
  • ****
  • Posts: 26
  • Aspiring MMO Creator
    • View Profile
Re: basic Flying for character - toggle
« Reply #6 on: Nov 11, 13, 02:45:50 AM »



Break is used alot for exiting loops.
  loop i from 1 to 10
    if item = "done"
      doFunction()
      break
    .
  .


Yes I am aware of that, I have Programmed in Java and C++ for a long time, I also read the HSL specifications, But you had a return inside a none returning function, ill try to find it later and let you know where it is. (I just replaced it with a  break to try and quick fix)
Im kinda bummed my char doesn't fly > .< but honestly I didn't read enough of the functions of Hero engine yet to be able to do it on my own without a tutorial, only a week in or so (programming wise), but i definitely have programmed a lot before, Im on my last year of Computer science, and I tried programming my own engine with C#,XNA and Lidgren.
Logged

Wallace

  • World Owners
  • ****
  • Posts: 2
    • View Profile
Re: basic Flying for character - toggle
« Reply #7 on: May 07, 15, 09:23:29 AM »

Hi there!

Thanks to Thazager for this tutorial, its fun and i am learning a lot from these! :)

If anyone is doing this tutorial in the future and gets this error or an error similar to this:

SCRIPT:[Input_Command]:Got key(Up) but did not handle it

Remember that the code is place in the client side script "Input_Movement" and not in the client side script "Input_Command", so make sure to place the keybindings code under the Input_Movement block in your GameKeyBindings.ini.

So the first step at the top of the tutorial must actually be:

Download the GameKeyBindings.ini  file from the repository.
below    [Command] = Input_Movement (Not Input_Command)
add     
  Flying = F
  Up = ANY+HOME
  Down = ANY+END


Thanks again for this tutorial Thazager, I am learning tons!

Now for the next issue. It now works but it toggles flying on and off very quickly in one key press. (same issue HistorianOnline was left with)

So if i find a fix for that i will post it here for future users. :)
Logged

Thazager

  • General Accounts
  • *
  • Posts: 1155
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: basic Flying for character - toggle
« Reply #8 on: May 07, 15, 12:52:22 PM »

I believe I know why it happens so fast.

In our game we have a delay after the first click of "F". Its based on a reuse timer (similar to the attack, fireball, and cast heal in base script) that when used, it does not allow a quick second "push" (repeat key) of the key. So, if you add Flying to the hotbar, similar to other click abilities, it should respond better.

Yes, logically the commands
  Flying = F
  Up = ANY+HOME
  Down = ANY+END
are movement and can be added there for clarity.

Also note that each section is just a key push, and can be placed in either location.
Logged

GlorianLanTarini

  • General Accounts
  • *
  • Posts: 148
    • View Profile
Re: basic Flying for character - toggle
« Reply #9 on: Aug 08, 15, 06:56:16 PM »

Hi,

Just delete FlyToggle() in function onCmdStart() in script Input_movement
Must be like this:
Code: [Select]
  is "Flying"
    return true
  .
 

And all be fine. Why? Because onStart proc when you push button, and then onStop proc when you release it. So, in you version fly will work only while you holding button pressed.
« Last Edit: Aug 08, 15, 07:01:43 PM by GlorianLanTarini »
Logged