(SOLVED) What's the point of "lt" in getlocationtype? (sierra)

Started by skooperstooper, Sun 07/08/2022 15:34:12

Previous topic - Next topic

skooperstooper

I was reading through global functions and looking at getlocationtype, which returns what type of element is at that screen location (hotspot, character, object, nothing). The example the manual gave is...

Code: ags
if (GetLocationType(mouse.x,mouse.y) == eLocationCharacter)
    mouse.Mode = eModeTalk;


I read it as if it's a character, use the talk cursor mode. It's easy enough. But every time I see getlocationtype used in the forums, there's this...

Code: ags
int lt = GetLocationType(mouse.x, mouse.y);


I read that as defining a custom variable named lt that stores whatever value getlocationtype returns. Is that just to simplify the code by being able to type lt for checks instead of writing out that whole line or does lt actually do something on its own?

This is my main hurdle learning scripting. Telling the difference between what someone is writing because that's how it has to be written in AGS (built-in functions/variables/whatever with fixed names and uses), or if it's just their own custom stuff that can be done another way.

Like if I hadn't gone through the tutorial, I would've assumed a hotspot and character need a lowercase h and c to work. Not that it's just convention. Especially when there are other things like cursor modes where the lowercase letter in front does need to be there because that's how its script name is hard coded.

Basically, I don't want to just parrot how someone else does something. I want to understand the core way of doing it so I can then apply my own custom approach. That's why I focus so much on the manual.

The forums are great because I almost always find threads relevant to what I'm learning about. I just end up wasting a lot of time trying to figure out if something in the recommended code is built-in or custom lol

Bookmarked
https://adventuregamestudio.github.io/ags-manual/Globalfunctions_General.html
Yes, I did the tutorial, went through the manual, and searched the forums and web. If I'm asking, it's because I missed it, forgot, or still don't get it.

Khris

If you need to check against a single type of location, comparing the function call directly is fine.
However if you want to compare against several types, storing the function's return value in an int and then comparing the int against multiple types is good practice.

Also note that there's an enum called LocationType so I'd do:

Code: ags
  LocationType lt = GetLocationType(mouse.x,mouse.y);


An enum is basically a subset of int, a limited range of natural numbers. Using it in custom functions has the benefit of the auto-complete window suggesting valid values.

Snarky

I think you'll get the hang of it pretty quickly. The amount of stuff that "has" to be a certain way in AGS is strictly finite, so you'll soon recognize the difference.

Quote from: skooperstooper on Sun 07/08/2022 15:34:12
I read that as defining a custom variable named lt that stores whatever value getlocationtype returns.

Correct.

Quote from: skooperstooper on Sun 07/08/2022 15:34:12
Is that just to simplify the code by being able to type lt for checks instead of writing out that whole line or does lt actually do something on its own?

The former. If you are going to be using the location type in several places (for example, testing to see if it's a Hotspot, Character, Object…), it's usual to store it in a temporary variable so that you don't have to write the whole GetLocationType(mouse.x, mouse.y) over and over. This a very common "programming pattern," and falls under the principle called DRY, "Don't Repeat Yourself": avoid duplication, try to never do the same thing twice. Although it's probably not very important in this particular case, it's a good habit to get into.

In general, there are a few good reasons to do this:

First, it makes the code shorter, and (if you give the variable a good, clear name), easier to read.

Second, it ensures that you really do use the same value everywhere, for example that you don't accidentally switch mouse.x and mouse.y in one place. (Not very likely in this case, but if the expression becomes more complicated, it can be easy to slip up.) And in some cases, the value returned by the function could even change between each time you call it! For example, if you there was a Mouse.Update() call somewhere between two calls to GetLocationType(mouse.x, mouse.y), the coordinates might now be different and you could get an inconsistent result.

Third, it means you only have to do the function call and any calculation that the expression involves once. Some function calls and calculations can be slow, and if you keep running them over and over it can slow down the program. Again, this probably doesn't apply to GetLocationType(), but repeated calls to functions like GetTextWidth() or DrawingSurface.GetPixel() should be avoided.

Finally, it means that if you have to change it â€" like, say that for some reason you want the mouse hotspot to be at (mouse.x+1, mouse.y-1) instead â€" you only have to make the change in one place. Again, this helps to avoid bugs, because if there are a lot of copies of the same line, it's easy to overlook one of the places you should be updating. In the general case, this is probably the most important reason, and it also helps to determine when, for example, to break out a calculation into its own function.

skooperstooper

Okay, thanks to both of you! You've been really helpful! I'm at least grateful because I think the hardest part of coding is understanding what code does when you see it, and I already get that. It's just going to take me a while to learn all the different things I can do myself and what's best for those scenarios.
Yes, I did the tutorial, went through the manual, and searched the forums and web. If I'm asking, it's because I missed it, forgot, or still don't get it.

SMF spam blocked by CleanTalk