Requesting a Child's Guide to Booleans

Started by Jordanowen42, Wed 04/12/2024 07:19:12

Previous topic - Next topic

Jordanowen42

Hi Folks-

Thanks again in advance for helping me with something that is probably second nature to even most beginner programmers. I need help doing booleans and I need it in the simplest possible terms as I am beneath novice level at programming.

Here's the set up: I have a puzzle in one room (room 106) that when solved allows the player to take a blue sphere in another room (room 105) into their inventory. I wanted to accomplish this with a boolean. If the player tries to take the blue sphere without solving the puzzle the sphere swells up and hisses angrily. If the player solves the puzzle the blue sphere allows itself to be taken.

To accomplish this I set up first put this in the global header:
Code: ags
import bool BlueSphereIsTakeable;

Then I put this in the Global Script:
Code: ags
bool BlueSphereIsTakeable = false;

Then I put this in the room 105 code:

Code: ags
function room_Load()
{
 if (BlueSphereIsTakeable)
    {
        object[0].Visible = false;
    }
}

The idea there is that the blue sphere object disappears. Once I can get this to happen I will then have it replace the blue sphere with an identical object that can be taken.

And I put this in the Room 106 code:

Code: ags
function room_Leave()
{
if (object[1].Graphic == 227 && object[2].Graphic == 228 && object[3].Graphic == 229 && object[4].Graphic == 235 && object[5].Graphic == 230 && object[6].Graphic == 231 &&object[7].Graphic == 232 && object[8].Graphic == 233 && object[9].Graphic == 234)
{BlueSphereIsTakeable}
}

I thought I was supposed to specify "BlueSphereisTakeable == true" but ChatGPT disagrees.

Anyway, the game either won't start or starts and then crashes whenever I try to enter either of these two rooms. Pulling my hair out.

Snarky

#1
Do not use ChatGPT with AGS!

It's not some ideological thing, either. It just doesn't work. The AGS code ChatGPT proposes is most often nonsense.

In this case, your intuition is correct and ChatGPT is (as usual) wrong:

Code: ags
function room_Leave()
{
  if (object[1].Graphic == 227 && object[2].Graphic == 228 && object[3].Graphic == 229 && object[4].Graphic == 235 && object[5].Graphic == 230 && object[6].Graphic == 231 && object[7].Graphic == 232 && object[8].Graphic == 233 && object[9].Graphic == 234)
  {
    BlueSphereIsTakeable = true;
  }
}

(That if-condition is horrendous, but shouldn't cause any crashes, assuming the objects exist.)

If it still doesn't work, post the actual error messages you get.

Khris

Note that you can add these bools in the "Global Variables" pane in the editor. This way you do not need to import/declare the variable manually in the global header/script.

Next, you can reference objects by script name in the room script:
Code: ags
function oBlueSphere_Interact(Object* the Object, CursorMode mode) {
  if (BlueSphereIsTakeable) {
    oBlueSphere.Visible = false;
    player.AddInventory(iBlueSphere);
    Display("You successfully pick up the sphere.");
  }
  else Display("The sphere hissen at you and tries to bite you. You decide to keep your hand off of it for now.");
}

eri0o

I would use the global variables pane too.

Also name your objects in the editor and use them by name. The usual thing in ags is to name room objects starting with the low case letter o. Inventories in AGS are also normally named with lower case letter i.

Jordanowen42

By golly it worked!

Thanks so much once again. :)

ThreeOhFour

Quote from: Jordanowen42 on Wed 04/12/2024 07:19:12I thought I was supposed to specify "BlueSphereisTakeable == true" but ChatGPT disagrees.

Just to clarify this, because it doesn't look like ChatGPT thought to give you any explanations here (another good reason to ask AGS folks instead of ChatGPT). The "==" operator is used to check things, not set them. The reason Snarky's version works is because he used the "=" operator. That's the one that sets things.

I am not much of a programmer and so sometimes I accidentally use the wrong one of these myself. It's a very good idea to search your AGS manual for a list of the operators and what they do, because knowing this will be very helpful for figuring out problems like this!

Crimson Wizard

#6
On a topic of ChatGPT, one big reason why I am biased against using auto-generated code is that if a person does not have enough knowledge, they would not be able to tell if the code is correct, but more importantly - if the code is suitable. Because even if the code does compile and seemingly work for the first time, that does not mean that it will work always and do precisely what you need.

I strongly advise learning what the code means if it's taken from someones examples in the internet, or generated by machine, not just put blindly into a program.

ThreeOhFour

I had this exact conversation with someone who was urging me to use AI for coding help recently. A piece of code that works might seem nice, but if I do not understand the mechanics of it then it is useless to me the moment I want to adjust it or need to fix something with it. It's worth struggling to learn something as long as I understand how the logic works at the conclusion, I think.

Snarky

Where I can see AI being useful is if you've written some chunk of code that you think ought to work, but doesn't. If you feed that to ChatGPT and ask it to find the problem, it might be able to spot your mistake—even sometimes with AGS code depending on what the problem is.

Danvzare

Quote from: ThreeOhFour on Thu 05/12/2024 10:52:56I had this exact conversation with someone who was urging me to use AI for coding help recently. A piece of code that works might seem nice, but if I do not understand the mechanics of it then it is useless to me the moment I want to adjust it or need to fix something with it. It's worth struggling to learn something as long as I understand how the logic works at the conclusion, I think.
While I'm in agreement. Doesn't this same logic apply to modules and templates?
I mean, I honestly have no idea how Tween functions. I just plop it in and it works. It's just a little too complicated for me to wrap my head around.

Although with that being said, I usually spend the time looking over any modules I use, figuring them out, and improving them where I see fit. (Just not Tween, because that thing is huge.)
But I think it's safe to say that most people (especially beginners) don't.
I suppose pointing a beginner to a module to help them out with their problem, isn't actually helping them in the long term.

Crimson Wizard

#10
Quote from: Danvzare on Thu 05/12/2024 17:55:50While I'm in agreement. Doesn't this same logic apply to modules and templates?

I don't think so. I believe there's a difference between inserting a code snippet in your program without understanding what it does, and using a separate module, or a library through its program interface. Modules are being a responsibility of someone who wrote them and maintains them. If they don't work you report that and their maintainers fix the problem while understanding what they are doing.
With a ai-generated code you are relying on a machine which has zero responsibility over what it did.

Quote from: Danvzare on Thu 05/12/2024 17:55:50I suppose pointing a beginner to a module to help them out with their problem, isn't actually helping them in the long term.

As a programmer I've been using libraries written by someone else without ever looking inside of them. That saves a lot of time, and lets me focus at the problems at hand. If I ever need to know how the library works, I can do that later when I can dedicate time and have a good reason to.

ThreeOhFour

A module also comes ready to take input parameters and provide a result, the intended result being documented by the creator. A piece of code taken from some source and plopped into your script does not. A well made module is likely a very good source to point a beginner at because if they do want to understand how it works they can study it in the way you describe.

If I believed I had to understand *every* line of code that makes things run I wouldn't be using AGS. Or Windows. :=

SMF spam blocked by CleanTalk