Game Development Community

How to Limit Calculation Output by a Range?

by Chase Webb · in Torque 2D Beginner · 03/01/2013 (5:38 am) · 5 replies

Hello again! I'm trying to make sure I don't cram too many unrelated things into a single thread, so now I have a math related question for you all. In my previous issue I managed to make it so a user could click somewhere on the screen to generate a number used to determine how many layers of rectangles would generate on the screen. What I soon discovered was that I was trying to create more layers than the engine allowed (31).

My question for those of you with more experience with TorqueScript is, how would you go about limiting the output of the equation? Some sort of If Else statements? Or is there an alternative command available to make it easier? Thanks!

#1
03/01/2013 (6:05 am)
Quote:how would you go about limiting the output of the equation?
What equation? It's impossible to answer such a vague question really. Ask a specific question and you should get a specific answer.
#2
03/01/2013 (6:11 am)
Be aware that the scene layers are fixed in the range of 0-31 but you can have SceneObject that are on the same layer but positioned relatively by setting their "SceneLayerDepth" i.e. their depth within the layer.

Each SceneObject has a "SceneLayerDepth" field which is a simple floating-point value that be in any range you like both positive and negative. You can also change this with "%sceneObject.setSceneLayerDepth(%depth)" and access it with "%sceneObject.getSceneLayerDepth()".

This is used when you set the scene layer sort order to "Z" or "-Z" i.e. the layer is sorted by depth. Any objects on that layer will be sorted by depth.

You can change a Scenes sort mode using "%scene.setLayerSortMode(
layer, sortMode )".

Note that you can also move SceneObject backwards/forwards and to-the-back and to-the-front using their SceneDepth. There are commands on each SceneObject to do this like "setSceneLayerDepthBackward" and "setSceneLayerDepthForward" etc.

Scene Script Bindings (approx line 3078)
SceneObject Script Bindings (approx line 124)

#3
03/01/2013 (6:24 am)
Simple - clamp your output.

if (%myNum < 0)
   %myNum = 0;
if (%myNum > 31)
   %myNum = 31;

Output clamped.
#4
03/01/2013 (6:35 am)
Richard is correct. You can also do this:
%myNum = mClamp( %myNum, 0, 31 );

There are more math commands exposed to the scripts here.

#5
03/02/2013 (5:48 am)
Once again, great responses everyone :) I like finding that there are several answers, and the thread always ending in the easiest or latest way to do it :)

I used Melv's code on the end and it worked like a charm. Thanks a bunch! now to clean up my little Toy so I can put it online :)