Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6999889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:36:01+00:00 2026-05-27T20:36:01+00:00

I am trying to write a tutorial game in Scala & Processing, intending to

  • 0

I am trying to write a tutorial game in Scala & Processing, intending to use as much FP as possible. However, I come to a conclusion that immutable-state game objects are not profitable in such application. If an object is big, it may result in quite intensive memory consumption in case of numerous such objects being constantly updated (therefore, making copies of itself each cycle), for example, using the copy() func. What is the default approach to resolute this? The only thing that I come up with – is to slice the object in tiny pieces-objects so that only those who need to be updated, are updated while leaving the “big” objects same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:36:01+00:00Added an answer on May 27, 2026 at 8:36 pm

    A game engine is essentialy a (discrete) event simulation. Typically, these are implemented via mutable data structures such as heap for the events, quad/oct trees for spatial queries about objects and lots of hash tables.

    For each of these data structures, mutable variants are faster. Furthermore, immutable data structures produce garbage which has to be collected, so the pressure on the GC is higher, and your application ends up being slower. Where real-time is a concern, GC pauses can be harmful (e.g. it may affect the framerate in your game), and this is particularly the case on platforms with less processing power, such as Android.

    As for the copy() method – it doesn’t have to copy the entire object to create the updated version. If your object is organized hierarchically in a tree-like manner (e.g. as a hierarchy of case classes), then changing one property of the object requires rewriting only one path in this tree – you won’t need to rewrite everything. Still, it will be more costly than just having a mutable version and updating in-place.

    Example:

    case class World(map: Array[Item], players: Vector[Player])
    
    case class Player(health: Int, speed: Int, weapon: Weapon, shield: Shield)
    
    case class Weapon(strength: Int, ammo: Int)
    

    To add more ammo to the weapon, you don’t have to copy the entire World:

    def setAmmo(playerNum: Int, newAmmo: Int, world: World): World = {
      val p = players(playerNum)
      world.copy(players = players.updated(playerNum, p.copy(weapon = p.weapon.copy(ammo = newAmmo))))
    }
    

    In this example, map, other players, and the shield of the modified Player stay the same in memory and are not eagerly copied.

    I would advise going for a mutable data structure to represent the state – unless you have a concurrent game engine with (for example) a single writer that simulates the game state, and a range of readers which render output, handle sound, network and so on, the benefits of immutable data structures are close to none in this use case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to follow Java's JDBC tutorials to write a Java program that can
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
Trying to write a couple of functions that will encrypt or decrypt a file
im trying to write an app that will display a list off lines from
I'm trying to write a regex function that will identify and replace a single
I'm trying to write a simple maze game, without using any deprecated OpenGL API
I was trying to make Tic Tac Toe game for the monthly tutorial and
I'm trying to write a very simple terminal app that will scan for Bluetooth
I'm trying to write my first query that uses a database join. I've gone
I'm trying to write a program that outputs data that can be served over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.