🌑

Jason Storey

PocketHunter - Devlog #2: Movement Jitter, Random Encounter Refactor

I started today with a simple mission; update the code that handled the random animal encounters to not require storing a 10,000 item array in memory. In it’s simplist form my random encounter logic calculated how many Super Slam animals and how many Non Super Slam animals were in a region, multiplied that by the encounter rates for Super Slam / Non Super Slam animals and built an array of animal names. This array contained 10,000 items (this was sort of arbitrary I decided there would be 30 Super Slam animal encounter chances per 10,000 encounters), then each time an encounter happens I generate a random number between 0 and 9999 and use that as the index to grab from the array, returning me the name of the animal the player will encounter.

This always seemed overkill, having to store 10,000 items in an array in memory, so I set out this morning to change this to a simpler and less memory intensive method.

Instead of building a 10,000 item array and selecting an item at random from it I wrote a little helper method percent_chance? which takes in a percentage (float) and returns true or false. Next I again built my animal arrays but rather then a giant 10,000 item one I instead built one array of all the Non Super Slam animals in the region and one with all the Super Slam animals.

My base encounter rate for a Super Slam animal is 0.3% so now all I do is utilize my percent_chance?method to determine if I will pluck an item out of the Super Slam array or the Non Super Slam array:

def select_animal(args)
  if percent_chance?(0.3)
    args.state.super_slam[random(0, args.state.non_super_slam.count - 1)]
  else
    args.state.non_super_slam[random(0, args.state.non_super_slam.count - 1)]
  end
end

Much simpler and I’m no longer storing a large array in memory. As I move forward with my project I will obviously replace the hardcoded 0.3 value with one stored in the game state and calculated based on other properties to determine the final Super Slam encounter rate at any specific time.

As a side note I also added a animal rarity level for each encounter (common, uncommon, rare, and majestic) as well as drops (pelts, meat, etc) each with its own level/rating randomly generated from a base range depending on the animal’s rarity level.

Why does my player move like he’s having a seizure?

Wanted to touch on this real quick. While I was playing with my game and testing the new encounter trigger logic I noticed the rectangle that is my player sprite was behaving strangly. When moving along with the camera the rectange would jitter severely rather then move smoothly across the scene. I spent about 2.5 hours debugging this; looking at the DRGTK samples, changing values, doing really anything to find out why. Until I finally was fed up and posted on the DragonRuby Discord. Within maybe 5 minutes someone replied and noted that it may have to do with my movement code and NOT the camera since the level itself moved smoothly within my camera view.

Then it hit me like a ton of bricks… I had set my player speed variable to 0.8 but the example I was using to try and compare and debug my jittery movement to was using 5, so I thought, maybe it has to be a whole number? And again, voila I change it to 1 and my player is moving smooth as butter. Thanks DRGTK community!

, , — Jan 11, 2024