1
\$\begingroup\$

Right now I'm programming a game that has an animated typing effect typically seen in older 1980's and 90's role-playing games. An example can be seen here.

The typing effect is generated by taking a target string and rendering a substring of it each frame. The length of the substring is initially zero, but it is incremented every few hundred milliseconds. This gives the impression that the string is being typed out. The pseudocode would more or less look like:

var message = "This is some dialogue.";
var substringLength = 0;

if (someTimeHasPassed)
{
   render(message.substring(0, substringLength);
   substringLength++;
}

This works great, but I'm using a garbage-collected language with immutable strings. Every few milliseconds, I create a new substring on the heap. Garbage collections become very frequent and I receive memory warnings on mobile devices.

Is there a way to generate this typing effect without creating lots of strings in memory?

\$\endgroup\$
4
  • \$\begingroup\$ When engine are you using? \$\endgroup\$
    – Steven
    Commented Jan 14, 2016 at 6:48
  • \$\begingroup\$ Have you profiled to check that the string allocations are causing the GCs? I'm skeptical - strings are not that big, and even in the constrained memory space of a mobile device I wouldn't expect your message.substring call, happening every few hundred ms (as described above), to cause GC issues. What goes on in render()? What does your someTimeHasPassed check actually look like? \$\endgroup\$ Commented Jan 14, 2016 at 7:53
  • \$\begingroup\$ @Steven The game is written in JavaScript with the Phaser library on Apache Cordova. \$\endgroup\$ Commented Jan 14, 2016 at 9:45
  • \$\begingroup\$ @ChrisMills-Price I'll try checking that now with profiling. There actually isn't any render() function, as Phaser uses a scene-graph setup. The string is being updated with a loop with JavaScript's String.splice() function. During this time, the device becomes really warm and receives memory warnings from the OS. \$\endgroup\$ Commented Jan 14, 2016 at 9:50

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.