Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: createasaurus on June 13, 2014, 09:10:20 AM

Title: [Solved] INT Switch with 100 options slowing down game... is there a better way?
Post by: createasaurus on June 13, 2014, 09:10:20 AM
I'm making a preschool education game, where the user picks letters, numbers, shapes, and colors.  There are about 100 possibilities.

Right now, I pick a random number 1-100.  Then an I have an Int Switch determine:
If random=1, play audio "one" and put graphic 1 on polygon.
If random=2, play audio "two" and put graphic 2 on polygon.
If random=3, play audio "three" and put graphic 3 on polygon.
etc, ect, ect, ect ... all the way to 100.

I'm having some frame drop issues overall in my game.  I haven't isolated this as the problem, but this is a prime suspect.  So, my question is, is there a simpler, more elegant, less sloppy way to do this?

Thanks!
Title: Re: INT Switch with 100 options slowing down game... is there a better way?
Post by: Lane on June 13, 2014, 09:34:07 AM
Seems like unless they are running simultaneously then this probably isn't the culprit. How does it work, exactly?

The Profiler would let you track this down pretty quickly.
Title: Re: INT Switch with 100 options slowing down game... is there a better way?
Post by: Alex Chouls on June 13, 2014, 10:34:37 AM
Is the frame rate drop in the editor or in a standalone build?
Title: Re: INT Switch with 100 options slowing down game... is there a better way?
Post by: JennaReanne on June 13, 2014, 01:56:43 PM
I would see if you can find some ways to get around using an int switch.  For example, you could use the variable as your sound / image names.  If the player picks "3", you would convert that to a string variable named something like "ImageIDName" and update the graphic to your "ImageIDName" variable (which in this case, would be 3).  I use this method frequently with the "TK 2d Sprite Set Id" action, but I'm sure there are similar options even if you're not using 2D Toolkit.
Title: Re: INT Switch with 100 options slowing down game... is there a better way?
Post by: Martin-Vaupell on June 14, 2014, 07:03:09 PM
To get around the switch..

To be honest, I would turn to C# and do a while loop,
Exspecially if i have to loop through more than 5 of the same function.

or better yet, write a
On Case select function, that can autoreplace for each number.

There is a "school" saying for programmers.
If you write the same line twice, with different variables, then you are doing something wrong, or ineffective.

Meaning

If random=1, play audio "one" and put graphic 1 on polygon.
If random=2, play audio "two" and put graphic 2 on polygon.
If random=3, play audio "three" and put graphic 3 on polygon.
etc, ect, ect, ect ... all the way to 100.

It is bad practice.

Instead i would proberly come up with some thing similar to this..
(written in english)

Code: [Select]
E is equal to 0
  For as long as E is less than 100 do the following
    E is equal to E now add 1;
    if random=E, play audio E$, and show graphic E;

    // failsafe exit
    if E is greater than 100 then exit
      break;
  Next


I doubt playMaker has a action for this, but I'm sure one of the very nice
programmers around could write such a specifik action for you to use.
Title: Re: INT Switch with 100 options slowing down game... is there a better way?
Post by: jeanfabre on June 16, 2014, 03:24:01 AM
Hi,

 I bet here your problem is audio, you need to make sure your audio is properly instantiated, cause it can be a killer for performances for Unity to always process your audio file.

 I doubt the playmaker logic is the issue here.

 Can you investigate your audio setup? I would likely suggest all your audio is instantiated and you simply enable disable. Else I totally recommand using an audio framework that will likely better handle complex audio needs ( and performances).

Bye,

 Jean
Title: Re: INT Switch with 100 options slowing down game... is there a better way?
Post by: Lane on June 16, 2014, 01:30:36 PM
Martin does make a good point though, even if it's not the primary cause there is still a lot of waste doing it that way.

Not ideal but better would be getting the random number, converting it to a string, then using Send Event By Name to fire a global event. Kinda sucks because you still need 100 states. More intuitive would be using Build String to compile the names of the audio and graphic plus the random number on the end, then just make sure all your asset names line up to that... Not sure how to make the conversion from a string to an ambiguous asset name though.
Title: Re: INT Switch with 100 options slowing down game... is there a better way?
Post by: jeanfabre on June 16, 2014, 02:29:56 PM
Hi,

 I would suggest "ResourceLoad" action from this forum, but then we're back at square one for audio perfs...

Typically, I guess some audio framework will allow "libraries" and calling a sound by its name, and then building string would start being indeed a very powerful alternatives.

Bye,

 Jean
Title: Re: INT Switch with 100 options slowing down game... is there a better way?
Post by: createasaurus on June 17, 2014, 01:53:21 PM
Thank you everyone!

I have a work around:  The problem was happening specifically when I "created new object."  and the very first state was the Random 100 INT thing.  Now, instead I'm "recycling" my objects by  toggling their visibility and moving them around.   This is working for me.

@Lane re: Profiler and convert to string. Thank you.
@ Alex:The frame rate was in both the editor and standalone build on WiiU hardware.
@JennaReanne re: convert to string.  Thank you.
@ Martin-Vaupell: Thank you.
@ JeanFabre:  re: audio instantiated and ResourceLoad.   Thank you, I will learn more about this next.