Methods muse

Unlike Ruby, Python and Boo don’t have “blocks”, instead what we have are functions, which is basically named blocks, actually. Function save coding time because instead of copy and pasting your code everywhere, you just do multiple calls to the same block. Pure functions don’t give side effects (e.g. printing, changing global variables inside blocks) but since Unity scripting is a OO-based system, we are going to deal more with methods that change internal states of each class.

Remember my last post where I had to deal with jumping? The code was an ugly hack just to get the thing working, and I could have done better by wrapping the overlapping code in a function, and so I did exactly just that.

Instead of :

def OnCollisionEnter(collision as Collision) as void:

    for contact as ContactPoint in collision.contacts:

        if contact.thisCollider.CompareTag("Leg") or contact.otherCollider.CompareTag("Leg"):

            on_the_ground=true

def OnCollisionExit(collision as Collision) as void:

    for contact as ContactPoint in collision.contacts:

        if contact.thisCollider.CompareTag("Leg") or contact.otherCollider.CompareTag("Leg"):

            on_the_ground=false

I got:

def FindCollidedTag(collision as Collision,tag as string, state as bool):

    for contact as ContactPoint in collision.contacts:

        if contact.thisCollider.CompareTag(tag) or contact.otherCollider.CompareTag(tag):

            on_the_ground=state

def OnCollisionEnter(collision as Collision) as void:

    FindCollidedTag(collision,"Leg", true)

def OnCollisionExit(collision as Collision) as void:

    FindCollidedTag(collision,"Leg", false)

It still feels tacky though, since the name is strange.

I also mentioned something about animations of the zoid being tied to internal states. However, some six months ago I found out the animations didn’t really blend well into each other, and the best way to play those animations were to stop the current animation and play the one I specified.
So, I wrapped it up with a for loop and used the CrossFade function to play what I want. Bonus points for checking if the “idle” animation was on and continue playing it.

There was a really huge WTF moment when I reviewed my animations code.

Eventually I changed everything to a animation.Play() call.

 
1
Kudos
 
1
Kudos

Now read this

(first (look Hoplon.io))

Writing web applications can be a chore. With all the languages (minimum of HTML/CSS/JS) you need to learn just to write a single web page, with a little PHP thrown into the mix when you need to do server side computation, the simple... Continue →