How to design a growth script (Pseudo Code)

Hello everyone!

So I am pretty new to Unity. I do have a pretty good understanding of programming concepts. However I am having a bit of trouble trying to develop and design some code.

The idea I need to code out is developing a growth system similar to tamagotchi but make it dynamic enough to work with different characters with a different number of stages.

An example of this would be:
Character 1 - 3 Stages :: Stage one (30 minutes) → Stage two (1 day) → Stage three (2 days)
Character 2 - 4 Stages :: Stage one (30 minutes) → Stage two (1 day) → Stage three (2 days) → Stage four (1 day)

So currently my idea goes as such

Use an ArrayList to house the stages.
A Stage has a name and a time duration
In the Stage Manager list we have the update that counts time
We have a check method to see if we can proceed to the next stage of life.

Now the problem I am having is I don’t think this is the best way to do it. Can anyone point me to a better method of doing this? Perhaps using a LinkedList would be better? Or perhaps there is a way to code all this into one script instead of having a Stage Class?

Any thoughts or ideas on how to plan this out would be very helpful.

Thanks in advance!

Hi :slight_smile:

If you’re not sure about your idea, great ! Set it up, and try it ! Best lesson you’ll get.

Now, I’m going to give you a tip anyways regarding data types, since you’re using unity try and use something that will show up in the inspector.

This is how you do things in unity and will prove to be practical in the long term.

So, you can define as many data classes as you fancy, but make sure you add the [System.Serializable] attribute or unity wont be able to save it. Fields must either be public or have the [SerializeField] attribute in order to show up in inspector and be saved within your unity objects/prefabs.

Finally, use either regular arrays or simple List<> as those will show up in the inspector and will save properly.

For the rest, there’s an infinite amount of ways to solve your problem, I’d feel silly to suggest one… imo your best option is to try and see what works for you and when you have figured it out and feel more comfortable with programming in unity, then you may ask for ways to improve !

1 Like

_Met44 - Thanks for the advice on that. I had no idea that was needed to save those things. I will start researching that part of Unity right now and I will try to put my idea into play. You probably saved me a lot of time trying to figure out why nothing was working just right.

1 Like

Don’t go near the ArrayList. Use a List instead. The rest of your implementation looks pretty standard.

Another option to consider is using an animation curve. Its a nifty way to store and manipulate relationships like this one.

1 Like

Hey BoredMormon,

Would a curve animation work even if I plan of this taking the span of a few days for some stages? After looking into curve animation, and I mean very little, it works best for smaller time frames. I am wrong on this or perhaps I need to go more in depth with this?

Thanks for the ArrayList advice. I have decided to go with a List because it gives me what I need and goes along with showing up int the inspector.

P.S Nice username

You would run the animation curve backwards. The time value on the bottom would represent your stage. The float value would represent the time in days to the next stage.

1 Like

the way you’re phrasing that makes me think you might be looking at something different to what is being suggested.

http://docs.unity3d.com/ScriptReference/AnimationCurve.html

AnimationCurve is just a way of storing a user defined “curve” where you can say “what value is at time x”. It’s really handy because if you expose it in the inspector you have complete control on what that curve looks like (wavy, cliffs, drops, whatever). Think of it more as a way of getting to evaluate complex math functions without having to do the math :slight_smile:

2 Likes

This is rather interesting, would I be right in thinking that an animation curve could be used to calculate required exp numbers when levelling a character in something like an RPG?

I’m thinking that by using something like curve.Evaluate(1/maxLevel * currentLevel) * maxLevelExpPoints I should be able to get a nice set of exp TNL values.

Does that sound sensible?

Edit: I’m ashamed to say that I never even considered that AnimationCurve was used for anything other than animations!

2 Likes

Yup. You can mess around with getting a polynomial fitted to match your curve. Then you have to recalculate coefficients every time you make a change.

Or you just drag in a few key frames until it feels right.

You can also output to an animation curve. Got a complex system with many floats to debug? Drop them all out to animation curves and you can observe them in realtime. In fact I have a tool that does this here. https://bitbucket.org/BoredMormon/trend.me

Finally you can combine both together. Want to know the results of a complex physics simulation? Don’t try and calculate it. Simply run the simulation in advance and bake the data into an animation curve for later use. Saw this done at Unite with a golf game. The devs ran a simulation to extract the distance-applied force curve for each club. Then they used that at run time.

1 Like

I’m glad I stumbled across this thread, it’s certainly going to help with a project I’m currently working on. I’d spent ages playing around with formulae for character and workskill exp tables and not been completely happy with the results. But using an animation curve I had something that was pretty much perfect within about 5 minutes.

Sounds like it could come in useful more than occasionally in the future too.

2 Likes

Thanks for all the good conversation!

So I think I am going to try to use the animation curve. It seems to give me more control and it will also save easily. Time to finish the code and get this baby going!!

So if I understand a little bit here I can set the curve with as many keys as I want ( 1 key = 1 stage). I have been looking around but how can I set a length for the Animation Curve? Any got any good tutorials in mind?

I will keep looking around the API and some youtube videos

Each key has a time and a float value. Treat time as your stage, and the float value as the time to reach the next stage.

Call AnimantionCurve.Evaluate with your stage as a parameter and you will get back the time to reach that stage.

1 Like

Awesome! I have been at work all day so I will get home and start coding this out. Thanks for all the knowledge you given me. I can’t wait to try this out!!

Alright, so I started coding this out but I soon realized that I am not entirely sure how to keep track of where I am currently in the stage. Let me setup some stuff real fast.

We have each keyframe as a combination of a stage with the time when that stage starts.

Keyframe(stage, timeStageStarts);

Current time is the current stage and the value is the time when that stage starts (this is in days).

However I am not entirely sure how to keep track of how far along in the current stage we are.

So from my understand if my stage value is .5 then I should be halfway through stage 0 and getting close to stage 1. But how should I go about increment the value of stage.

I could use DeltaTime and convert it to minutes and do some more converting to get it to a day value. However I feel like perhaps there is a better way or perhaps a more efficient way. Any thoughts on this?

You’ll want to store time passed separately, in whatever system you are using to manage time. The animation curve is just to figure out the time to next stage. In your case its better to treat it as read only data.

Yeah I just go done coding it up a bit and it worked like a charm!! Thank you so much for the help!