tulamide's Recent Forum Activity

  • Thank you <img src="smileys/smiley1.gif" border="0" align="middle" />

    I'm glad I could be of help. And imagine my big grin. "Progamming consultant"? Sounds good!

    I hope you'll finish it. Can't wait to see that credit entry... <img src="smileys/smiley4.gif" border="0" align="middle" />

  • Tips'n'Tricks #2: nth Root Calculation

    Here's another one you might find useful. Say, you want to take the squareroot of a number. You'll use the expression sqrt():

    pv = sqrt(16) = 4

    That's good, but what if you need to take the 3rd root or the 5th root? There are quite some situations, where you need them (e.g. when working with audio)

    Here's the trick. Instead of looking for an nth-root expression (which doesn't exist in CC), head towards raising to a power. While 2^3 means 2*2*2, you can calculate the root of a number by using (1 / nthroot) as the exponent.

    For this example we will take a number that we know the 5th root of: 32. The 5th root of 32 is 2 (2*2*2*2*2=32)

    1/5 equals 0.2

    32^0.2 = 2

    Test it in CC. Create a text box and set the text in runtime to 32^0.2 or 32^(1/5). I prefer to use the calculated value (0.2 in this case), because divisions can get inaccurate with single precision floats that are used in CC.

    So, just remember:

    nth root of a number is the same as number^(1/nthroot)

  • Tips'n'Tricks #1: Automatic Loop Counter

    You might already know about the trick to automatically switch between 0 and 1, by simply writing

    pv = 1 - pv

    if you start at 0, consecutive calls of the event will set pv to:

    pv = 1 - 0 = 1

    pv = 1 - 1 = 0

    pv = 1 - 0 = 1

    etc.

    So far, so good. But what if you wanted to switch between 1 and 2? Or count from 1 to 5, repeatedly?

    There's a treasure, we can use here. It's called mod (short for modulo, or modulus), and in CC you use the %-sign to indicate modulo calculation. Modulo calculates the integer remainder of a integer division. For example, 5 % 4 equals 1. To better understand it, let's look at the calculation from the other end. 4 fits into 5 exactly one time (remember: integer division)) and it remains 1. Got it? Ok, what about 11 % 4 ? 4 fits into 11 exactly two times and it remains 3 (2*4 = 8, 11 - 8 = 3). So 11 % 4 equals 3. But why the heck do I tell you this? Because the remainder will never exceed the second number of the calculation. See this list:

    1 % 3 = 1

    2 % 3 = 2

    3 % 3 = 0

    4 % 3 = 1

    5 % 3 = 2

    6 % 3 = 0

    See? It loops. And with a simple but clever trick you can use this to your advantage and implement an automatic loop-counter that doesn't need any bounds check.

    pv = (pv % [upperbound]) + 1

    Fire and forget. Let's say you want to count from 1 to 4 in a loop. 4 will be your upperbound. If you start at 1, consecutive calls of the event will set pv to:

    pv = (1 % 4) + 1 = 2

    pv = (2 % 4) + 1 = 3

    pv = (3 % 4) + 1 = 4

    pv = (4 % 4) + 1 = 1

    pv = (1 % 4) + 1 = 2

    pv = (2 % 4) + 1 = 3

    etc.

    You can do a lot of other things, too. Say you want to switch between 3 and 5. Take an upper bound of 4 but add 2 instead of 1. Here you go:

    pv = (pv % 4) + 2

    pv = (3 % 4) + 2 = 5

    pv = (5 % 4) + 2 = 3

    pv = (3 % 4) + 2 = 5

    pv = (5 % 4) + 2 = 3

    etc.

    You prefer switching between 3 and 4? Alright:

    pv = (pv % 2) + 3

    pv = (4 % 2) + 3 = 3

    pv = (3 % 2) + 3 = 4

    pv = (4 % 2) + 3 = 3

    pv = (3 % 2) + 3 = 4

    etc.

    Or maybe you want to count from 4 down to 2 in a loop? Tricky, but possible:

    pv = (pv % 3) + 2

    pv = (2 % 3) + 2 = 4

    pv = (4 % 3) + 2 = 3

    pv = (3 % 3) + 2 = 2

    pv = (2 % 3) + 2 = 4

    pv = (4 % 3) + 2 = 3

    etc.

    "Right", you say, "now do something completely weird. Your dumb trick will never succeed. I want you to automatically calculate the numbers 5, 2, 4, 6, 3 exactly in this order and looped. HA!" ...Hmm, you mean like so?

    pv = (pv % 5) + 2

    pv = (3 % 5) + 2 = 5

    pv = (5 % 5) + 2 = 2

    pv = (2 % 5) + 2 = 4

    pv = (4 % 5) + 2 = 6

    pv = (6 % 5) + 2 = 3

    pv = (3 % 5) + 2 = 5

    etc.

    You can even do an addition that doesn't add anything, but always stays at the number you add. Ok, it doesn't make sense, but it is possible <img src="smileys/smiley36.gif" border="0" align="middle" /> :

    pv = (pv % 1) + anynumber

    pv can be any number too, e.g. 143

    pv = (143 % 1) + 5 = 5

    pv = (5 % 1) + 5 = 5

    etc.

    Now that you learned the magic of modulo, go out and spread the word. Find wonderful rows of looped numbers, or find new ways of using modulo. The fantastic world of modulo counting awaits you...

  • Hi fellow game designers,

    I decided to open this thread to post tips and tricks here from time to time. I will concentrate on programming rather than on design, and on more simple but effective tricks rather than the next 20000 line code segment.

    If you are an experienced programmer, you probably won't find anything new or exciting here. But if you have made your first steps into programming and want to go deeper, than you're at the right place.

    I don't know how frequently I will post here, so don't ask or try to force me^^

    If you have suggestions, I'd be happy to read them.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • If you need a specific number of decimals, you better use FormatDecimal(number, N), because str() will convert 2.40 to "2.4", omitting the last 0

    With FormatDecimal(2.4, 2) you will get a string with 2 digits for the decimals "2.40"

    Likewise, FormatDecimal(2.41, 3) will return "2.410", etc.

  • That's not a text object bug, but a family issue. You need to manage all private variables of a family from within the family manager, and even then it makes problems sometimes. I don't know any workaround besides deleting all pv, re-creating them in the family manager and hoping for the best...

  • Just make the objects of the inventory layer, that you create at runtime, 'global' (under properties->common)

    Objects (and instances) that are local and are created during runtime will get destroyed as soon as you switch to another layout. To keep them alive, they need to be global in scope.

    Here's the extended example cap: inheritance2.cap

  • The layer pops up in its original form but not with any of the sprites that have been added to the layer.Then you've done something wrong. Here is a basic example: inheritance.cap

  • I recommend reading the wiki. This is the page explaining the system expressions: scroll down to "Math"

    clamp is just one of many functions that are common to most programming languages. The best practice is to read about them, look for descriptions/examples on the net and try for yourself. Then you are aware of them in case you need them.

    Math functions you will almost always need somewhere somehow:

    sin(), cos(), min(), max(), clamp(), ceil(), floor(), round(), abs(), lerp()

  • Just build one with graphics and events. Here's an example to help you get started: slider.cap

  • Nice solution you found here.

    sin() and cos() work on a specific angle, not an angle range. That's what I meant in my first post. The line of sight behavior also needs an angle but calculates it from the positions of Baddie and TempX/Y, so you don't need to calculate it yourself.

  • I see. So you want to spawn it on some random spot and just make sure it is a minimum distance away from all other ones? Than your proposal is the easiest one. You should go for it.

tulamide's avatar

tulamide

Member since 11 Sep, 2009

Twitter
tulamide has 3 followers

Trophy Case

  • 15-Year Club
  • Coach One of your tutorials has over 1,000 readers
  • Email Verified

Progress

17/44
How to earn trophies