Dynamic Blinking / math.random issue

  • #1, by ianfoxTuesday, 21. August 2018, 17:33 6 years ago
    Hi everyone.
    I recently created some dynamic blinking for my character using the following on the first frame of my character's standing/blinking animations:

    if math.random(70) < 69 then
     ActiveAnimations["front"].AnimationLastFrame = 1
    else
     ActiveAnimations["front"].AnimationLastFrame = 5
    end

    However, the results are really tempermental. Sometimes the results are fine - for instance when the character faces forwards, but then when facing left the blinking animation just cycles endlessly, which obviously looks ridiculous.  If I close and relaunch, the results differ again depending on which way he is facing.

    Has anyone encountered something similar?  Might I have implemeneted the script incorrectly?

    Thanks,
    Ian

    Newbie

    24 Posts


  • #2, by sebastianTuesday, 21. August 2018, 17:54 6 years ago
    do you reset the animation to just show the standing/no blinking frames afterwards? 

    Thread Captain

    2346 Posts

  • #3, by afrlmeTuesday, 21. August 2018, 22:11 6 years ago
    Are you using mirrored animations?

    Also, you really should give your character animations unique names. If you had multiple characters active on the same scene & all of their general animation names (walk, talk, idle, etc) are all named the same "front", "back", etc, then the engine will have no idea which animation you are trying to access in the ActiveAnimation table. You should name them something along the lines of this... "tom_idle_front" or "tom_idle_s" - s being short for south. This way, you won't generate any conflict issues because the engine doesn't target self when you access an animation via the animation, but instead it access a global table that contains all of the active animations that are currently playing on the scene.

    Quick tip #1: you might want to include math.randomseed(os.time()) above the first line of your script to guarantee a random number each time math.random() is used - or alternatively, add the math.randomseed line into an execute a script action part in the game launch actions via the main game settings section of the editor.

    Quick tip #2: there's an alternative method you could use to add dynamic blinking to your characters seeing as you are using a single frame idle animation - I personally had to use AnimationLastFrame because our characters have animated breathing. The method is to create an execute a script action part for the first frame & then you will use math.random() to determine how long the first frame should be displayed for, then in the next frame you will set the global delay time back to the default value. That way you can set a min/max delay for how often the character should blink.


    Imperator

    7278 Posts

  • #4, by ianfoxWednesday, 22. August 2018, 00:30 6 years ago
    Hi AFRLme, many thanks for your reply.  As you mentioned - I am using mirrored animations, and I think perhaps that might be causing some issues.

    I've renamed all the animations following your format, and used the script you described in Quick Tip 2, and as shown in the screen shots.  It has worked in part, but despite checking the script numerous times for typos, some just don't seem to want to work.  Strangely, for the sw and se idle animations, it is the mirrored version that works fine, and the original seems to ignore the script.

    Would this be linked to how the game reads the global table of animations?

    Cheers,
    Ian

    Newbie

    24 Posts

  • #5, by ianfoxWednesday, 22. August 2018, 00:36 6 years ago
    On a further check - it's all the mirrored animations and only originals that don't have a mirrored version that follow the script.  

    Any animation that is mirrored somewhere else completely ignores the script!

    Newbie

    24 Posts

  • #6, by afrlmeWednesday, 22. August 2018, 01:33 6 years ago
    Aye, never use mirrored animations - especially if you plan on accessing/manipulating them later on with Lua script.

    Tip number 2 is still valid. Naming convention is important in VS because some things get clumped together in temporary active tables & it's also good practice for animations linked to scene objects & actions linked to scene objects/scenes, etc as it means you can use shorthand/global access instead of direct linking.

    example: a condition linked to a specific scene object & let's say you have another condition somewhere else in the project with the same name...
    Scenes["scene_1"].SceneObjects["door"].SceneConditions["door_open"].ConditionValue

    As you can see I used very generic names for the scene object & the condition, thus I had to directly link to it, whereas if I provide unique names, like this...
    Conditions["101_door_open"].ConditionValue -- 101_ being the id prefix I assigned the scene (chapter 1, scene 01 = 101)

    lets me access it globally & reduce the amount of code I need to type.

    Here's a screenshot giving you an example of my naming convention system... that's not to say you have to use it, but naming convention can be really useful when it comes to Lua script & keeping things organized in general throughout your game development project(s).

    Imperator

    7278 Posts

  • #7, by ianfoxWednesday, 22. August 2018, 10:29 6 years ago
    I've unchecked the mirrored animations and assigned them their own sets of frames - the problem is solved!

    Really appreciate your help, and the advice on naming conventions - I might have to go through renaming a lot of stuff but I think it'll be worth it to avoid any future issues.

    Cheers,

    Ian

    Newbie

    24 Posts