int/object updating issue on XY axis

Started by Jordanowen42, Thu 06/03/2025 05:50:32

Previous topic - Next topic

Jordanowen42

Some really good stuff here guys- I really appreciate it. Will implement these ideas and get back to you.

pell

Quote from: Jordanowen42 on Thu 06/03/2025 23:55:18
Quote from: Khris on Thu 06/03/2025 11:35:17@pell
My point is that in the "all this stuff" part, if any of the lines concerning segment #1 are a match, the code for segment #2 never runs. This is also most likely what caused OP to open this thread.
Because why would they deliberately sabotage their segment2 code, then ask here why it doesn't work?
None of this is intentional, because it's all a hot, flaming mess. Again, sorry. But this needs to be unlearned ASAP.

As an aside: using the object[] array in a room script is possible but less readable than using oButton1 or oSegment2 or whatever.

I changed the segment 2 script so it's myCounter 3 and 4. Still nothing. Going to implement some more ideas.


Did you mean for segment 2 to run if segment 1 does not? Because the way you have it coded, the function will return if any of the if statements in segment 1 fail.

Jordanowen42



[/quote]

Did you mean for segment 2 to run if segment 1 does not? Because the way you have it coded, the function will return if any of the if statements in segment 1 fail.
[/quote]

No I mean for segment 1 to run then segment 2 and so on (there will be nine total.).

Jordanowen42

Hey guys-

Improvements implemented and first problem solved but it brings me to my second (and ongoing) problem which I will outline in this video:


So that's where it stands. I know I keep saying this but thanks so much for your help!

Here's the code as it currently stands:

Code: ags


// room script file

// Segment 1
int myCounter1 =0 ;
int myCounter2 =0 ;

// Segment 2
int myCounter3 =0 ;
int myCounter4 =0 ;

// Segment 3
int myCounter5 ;
int myCounter6 ;

// Segment 4
int myCounter7 ;
int myCounter8 ;

// Segment 5
int myCounter9 ;
int myCounter10 ;

// Segment 6
int myCounter11 ;
int myCounter12 ;

// Segment 7
int myCounter13 ;
int myCounter14 ;

// Segment 8
int myCounter15 ;
int myCounter16 ;

// Segment 9
int myCounter17 ;
int myCounter18 ;



function oObject20_AnyClick()
{
if (object[20].Graphic == 410)
{object[20].Graphic = 411;
object[1].Visible = false;
object[2].Visible = true;
object[3].Visible = false;
return;}

if (object[20].Graphic == 411)
{object[20].Graphic = 412;
object[2].Visible = false;
object[3].Visible = true;
return;}

if (object[20].Graphic == 412)
{object[20].Graphic = 410;
object[1].Visible = true;
object[3].Visible = false;
return;}



}

function oObject21_AnyClick()
{
if (object[21].Graphic == 410)
{object[21].Graphic = 411;
object[4].Visible = false;
object[5].Visible = true;
object[6].Visible = false;
return;}

if (object[21].Graphic == 411)
{object[21].Graphic = 412;
object[4].Visible = false;
object[5].Visible = false;
object[6].Visible = true;
return;}

if (object[21].Graphic == 412)
{object[21].Graphic = 410;
object[4].Visible = true;
object[5].Visible = false;
object[6].Visible = false;
return;}

}

function oObject22_AnyClick()
{
if (object[22].Graphic == 410)
{object[22].Graphic = 411;
object[7].Visible = false;
object[8].Visible = true;
object[9].Visible = false;
return;}

if (object[22].Graphic == 411)
{object[22].Graphic = 412;
object[7].Visible = false;
object[8].Visible = false;
object[9].Visible = true;
return;}

if (object[22].Graphic == 412)
{object[22].Graphic = 410;
object[7].Visible = true;
object[8].Visible = false;
object[9].Visible = false;
return;}

}

function room_Load()
{

object[1].Visible = true;
object[4].Visible = true;
object[7].Visible = true;
}
function UpdateTwoObjects()
{
    
    if (object[1].Visible == true) { myCounter1 = 4; myCounter2 = 4;  return;}
    if (object[2].Visible == true) { myCounter3 += 3; myCounter4 += 2; myCounter1 -= 1; myCounter2 -= 1; return;}
}

function UpdateCounters()
{
   

// Segment 1 integers
if (myCounter1 == 0 && myCounter2 == 0) {object[11].Graphic = 376; } 
else if (myCounter1 == 1 && myCounter2 == 1) {object[11].Graphic = 404; } //check
else if (myCounter1 == 1 && myCounter2 == 2) {object[11].Graphic = 398; } //check
else if (myCounter1 == 1 && myCounter2 == 3) {object[11].Graphic = 392; } //check
else if (myCounter1 == 1 && myCounter2 == 4) {object[11].Graphic = 386; } //check
else if (myCounter1 == 2 && myCounter2 == 1) {object[11].Graphic = 407; } //check
else if (myCounter1 == 2 && myCounter2 == 2) {object[11].Graphic = 401; } //check
else if (myCounter1 == 2 && myCounter2 == 3) {object[11].Graphic = 395; } //check
else if (myCounter1 == 2 && myCounter2 == 4) {object[11].Graphic = 389; } //check
else if (myCounter1 == 3 && myCounter2 == 1) {object[11].Graphic = 405; } //check
else if (myCounter1 == 3 && myCounter2 == 2) {object[11].Graphic = 399; } //check
else if (myCounter1 == 3 && myCounter2 == 3) {object[11].Graphic = 393; } //check
else if (myCounter1 == 3 && myCounter2 == 4) {object[11].Graphic = 387; } //check
else if (myCounter1 == 4 && myCounter2 == 1) {object[11].Graphic = 402; } //check
else if (myCounter1 == 4 && myCounter2 == 2) {object[11].Graphic = 396; } //check
else if (myCounter1 == 4 && myCounter2 == 3) {object[11].Graphic = 390; } //check
else if (myCounter1 == 4 && myCounter2 == 4) {object[11].Graphic = 384; } //check
else if (myCounter1 == 5 && myCounter2 == 1) {object[11].Graphic = 406; } //check
else if (myCounter1 == 5 && myCounter2 == 2) {object[11].Graphic = 400; } //check
else if (myCounter1 == 5 && myCounter2 == 3) {object[11].Graphic = 394; } //check
else if (myCounter1 == 5 && myCounter2 == 4) {object[11].Graphic = 388; } //check
else if (myCounter1 == 6 && myCounter2 == 1) {object[11].Graphic = 403; } //check
else if (myCounter1 == 6 && myCounter2 == 2) {object[11].Graphic = 397; } //check
else if (myCounter1 == 6 && myCounter2 == 3) {object[11].Graphic = 391; } //check
else if (myCounter1 == 6 && myCounter2 == 4) {object[11].Graphic = 385; } //check

// Segment 2 integers
if (myCounter3 == 0 && myCounter4 == 0) {object[12].Graphic = 376; } 
else if (myCounter3 == 1 && myCounter4 == 1) {object[12].Graphic = 404; } //check
else if (myCounter3 == 1 && myCounter4 == 2) {object[12].Graphic = 398; } //check
else if (myCounter3 == 1 && myCounter4 == 3) {object[12].Graphic = 392; } //check
else if (myCounter3 == 1 && myCounter4 == 4) {object[12].Graphic = 386; } //check
else if (myCounter3 == 2 && myCounter4 == 1) {object[12].Graphic = 407; } //check
else if (myCounter3 == 2 && myCounter4 == 2) {object[12].Graphic = 401; } //check
else if (myCounter3 == 2 && myCounter4 == 3) {object[12].Graphic = 395; } //check
else if (myCounter3 == 2 && myCounter4 == 4) {object[12].Graphic = 389; } //check
else if (myCounter3 == 3 && myCounter4 == 1) {object[12].Graphic = 405; } //check
else if (myCounter3 == 3 && myCounter4 == 2) {object[12].Graphic = 399; } //check
else if (myCounter3 == 3 && myCounter4 == 3) {object[12].Graphic = 393; } //check
else if (myCounter3 == 3 && myCounter4 == 4) {object[12].Graphic = 387; } //check
else if (myCounter3 == 4 && myCounter4 == 1) {object[12].Graphic = 402; } //check
else if (myCounter3 == 4 && myCounter4 == 2) {object[12].Graphic = 396; } //check
else if (myCounter3 == 4 && myCounter4 == 3) {object[12].Graphic = 390; } //check
else if (myCounter3 == 4 && myCounter4 == 4) {object[12].Graphic = 384; } //check
else if (myCounter3 == 5 && myCounter4 == 1) {object[12].Graphic = 406; } //check
else if (myCounter3 == 5 && myCounter4 == 2) {object[12].Graphic = 400; } //check
else if (myCounter3 == 5 && myCounter4 == 3) {object[12].Graphic = 394; } //check
else if (myCounter3 == 5 && myCounter4 == 4) {object[12].Graphic = 388; } //check
else if (myCounter3 == 6 && myCounter4 == 1) {object[12].Graphic = 403; } //check
else if (myCounter3 == 6 && myCounter4 == 2) {object[12].Graphic = 397; } //check
else if (myCounter3 == 6 && myCounter4 == 3) {object[12].Graphic = 391; } //check
else if (myCounter3 == 6 && myCounter4 == 4) {object[12].Graphic = 385; } //check


}

function room_RepExec()
{
    UpdateTwoObjects();
    UpdateCounters();



}



pell

#24
I've watched the video, and I'm going through the code to try to match these variables names to what they're meant to represent.

In case you're still up, can you tell me if myCounter1 and myCounter3 are supposed to be the six states and myCounter2 and myCounter4 the four temperatures? I ask because myCounter1 and myCounter3 show seven states in the code, 0 through 6.

Jordanowen42

Quote from: pell on Fri 07/03/2025 07:37:05I've watched the video, and I'm going through the code to try to match these variables names to what they're meant to represent.

In case you're still up, can you tell me if myCounter1 and myCounter3 are supposed to be the six states and myCounter2 and myCounter4 the four temperatures? I ask because myCounter1 and myCounter3 show seven states in the code, 0 through 6.

So the counters represent the temp and height of lava in each segment of the pipe. Each set of two counters is assigned to one segment. Odd counters refer to the temperature on the spreadsheet I showed in the video and even numbers refer to the height. (So vertical and horizontal, respectively.

Objects 1-9 refer to the rectangular lights that indicate which hatch is closed. If a hatch light is on that means that hatch is closed.

Objects 11-19 refer to the square windows that indicate the temp and fluid levels of the lava in that segment of the pipe.

Objects 20-22 refer to the three switches (top to bottom) that are set to the left of each row of windows.

pell

#26
I think I know what's happening here. You're calling UpdateTwoObjects every game cycle, because you call it from room_RepExec. That means that while object[2] is visible, the calculations to change the values of the counters happen every game cycle. If I'm right about this being the problem, I recommend one of two options:

1. The easiest way is just to call UpdateTwoObjects only when it needs to happen. Am I right in thinking that would be whenever the player clicks on one of the buttons?

2. If you want to call it every game cycle, you could set some kind of TwoObjectsNeedUpdating type of variable, and then set that to true after pressing a button (or whenever the update needs to happen) and then false again after performing the update.

Edit: To be clear, option 2 would normally not be a good idea. But it does illustrate how to selectively control something that would otherwise get changed by a continuous process (like keyboard and  mouse input).

I really loved the Myst games. I played the second, third and fourth games in the series, but not the first or last ones. I'm looking forward to seeing how this comes out.

Jordanowen42

Quote from: pell on Fri 07/03/2025 08:20:53I think I know what's happening here. You're calling UpdateTwoObjects every game cycle, because you call it from room_RepExec. That means that while object[2] is visible, the calculations to change the values of the counters happen every game cycle. If I'm right about this being the problem, I recommend one of two options:

1. The easiest way is just to call UpdateTwoObjects only when it needs to happen. Am I right in thinking that would be whenever the player clicks on one of the buttons?

2. If you want to call it every game cycle, you could set some kind of TwoObjectsNeedUpdating type of variable, and then set that to true after pressing a button (or whenever the update needs to happen) and then false again after performing the update.

Edit: To be clear, option 2 would normally not be a good idea. But it does illustrate how to selectively control something that would otherwise get changed by a continuous process (like keyboard and  mouse input).

I really loved the Myst games. I played the second, third and fourth games in the series, but not the first or last ones. I'm looking forward to seeing how this comes out.

I think we're on the right track here but can you tell me what a "game cycle" is? I am very much learning as I go.

To answer your questions, the answer to #1 is yes. But how do I make it update the two objects only when I need it to happen? As regards point two, I think you're on the right track but I'm too much of a n00b to know how to implement that.

If you haven't played Myst 1 I recommend trying RealMyst which is on the Apple app store and, I believe, Steam as well. It's an upgraded version that plays in a full motion 3D environment so it's more like the latter period games you're familiar with. Avoid 5.


pell

A game cycle, more commonly called a game loop, is just the continuous running process of the game. A game is a program that runs in a loop until you tell it to quit. While it's looping, it's checking for user input, updating the screen (animation) and performing the game logic.

The room_RepExec function is called repeatably, so anything you put in there is called repeatedly.

Just call UpdateTwoObjects where in the code you want that to happen. For example, you change visibility of the objects in oObject20_AnyClick() based on mouse clicks, so that might be a good place to call the update.

pell

#29
I want to point out that there's nothing magic about room_RepExec. You can designate a function to run repeatedly while you're in a particular room by clicking its "Repeatedly execute" field in the Events tab under the room's Properties. That function name is just the default name that the editor gives you, and it automatically generates an empty function with that name in your room script.

You can use any allowable function name or even create the function first in the room's script and then enter that name into the "Repeatedly execute" field. If you do change the name, you need to make sure the name in that field matches the name of the function you want to call. Just changing the name in the script won't automatically change the name in the Events tab. This is true of the functions called from the other events as well.

Snarky

#30
Hold on for one moment...

Quote from: Jordanowen42 on Fri 07/03/2025 06:48:25
Code: ags
function UpdateCounters()
{
   

// Segment 1 integers
if (myCounter1 == 0 && myCounter2 == 0) {object[11].Graphic = 376; } 
else if (myCounter1 == 1 && myCounter2 == 1) {object[11].Graphic = 404; } //check
else if (myCounter1 == 1 && myCounter2 == 2) {object[11].Graphic = 398; } //check
else if (myCounter1 == 1 && myCounter2 == 3) {object[11].Graphic = 392; } //check
else if (myCounter1 == 1 && myCounter2 == 4) {object[11].Graphic = 386; } //check
else if (myCounter1 == 2 && myCounter2 == 1) {object[11].Graphic = 407; } //check
else if (myCounter1 == 2 && myCounter2 == 2) {object[11].Graphic = 401; } //check
else if (myCounter1 == 2 && myCounter2 == 3) {object[11].Graphic = 395; } //check
else if (myCounter1 == 2 && myCounter2 == 4) {object[11].Graphic = 389; } //check
else if (myCounter1 == 3 && myCounter2 == 1) {object[11].Graphic = 405; } //check
else if (myCounter1 == 3 && myCounter2 == 2) {object[11].Graphic = 399; } //check
else if (myCounter1 == 3 && myCounter2 == 3) {object[11].Graphic = 393; } //check
else if (myCounter1 == 3 && myCounter2 == 4) {object[11].Graphic = 387; } //check
else if (myCounter1 == 4 && myCounter2 == 1) {object[11].Graphic = 402; } //check
else if (myCounter1 == 4 && myCounter2 == 2) {object[11].Graphic = 396; } //check
else if (myCounter1 == 4 && myCounter2 == 3) {object[11].Graphic = 390; } //check
else if (myCounter1 == 4 && myCounter2 == 4) {object[11].Graphic = 384; } //check
else if (myCounter1 == 5 && myCounter2 == 1) {object[11].Graphic = 406; } //check
else if (myCounter1 == 5 && myCounter2 == 2) {object[11].Graphic = 400; } //check
else if (myCounter1 == 5 && myCounter2 == 3) {object[11].Graphic = 394; } //check
else if (myCounter1 == 5 && myCounter2 == 4) {object[11].Graphic = 388; } //check
else if (myCounter1 == 6 && myCounter2 == 1) {object[11].Graphic = 403; } //check
else if (myCounter1 == 6 && myCounter2 == 2) {object[11].Graphic = 397; } //check
else if (myCounter1 == 6 && myCounter2 == 3) {object[11].Graphic = 391; } //check
else if (myCounter1 == 6 && myCounter2 == 4) {object[11].Graphic = 385; } //check

// Segment 2 integers
if (myCounter3 == 0 && myCounter4 == 0) {object[12].Graphic = 376; } 
else if (myCounter3 == 1 && myCounter4 == 1) {object[12].Graphic = 404; } //check
else if (myCounter3 == 1 && myCounter4 == 2) {object[12].Graphic = 398; } //check
else if (myCounter3 == 1 && myCounter4 == 3) {object[12].Graphic = 392; } //check
else if (myCounter3 == 1 && myCounter4 == 4) {object[12].Graphic = 386; } //check
else if (myCounter3 == 2 && myCounter4 == 1) {object[12].Graphic = 407; } //check
else if (myCounter3 == 2 && myCounter4 == 2) {object[12].Graphic = 401; } //check
else if (myCounter3 == 2 && myCounter4 == 3) {object[12].Graphic = 395; } //check
else if (myCounter3 == 2 && myCounter4 == 4) {object[12].Graphic = 389; } //check
else if (myCounter3 == 3 && myCounter4 == 1) {object[12].Graphic = 405; } //check
else if (myCounter3 == 3 && myCounter4 == 2) {object[12].Graphic = 399; } //check
else if (myCounter3 == 3 && myCounter4 == 3) {object[12].Graphic = 393; } //check
else if (myCounter3 == 3 && myCounter4 == 4) {object[12].Graphic = 387; } //check
else if (myCounter3 == 4 && myCounter4 == 1) {object[12].Graphic = 402; } //check
else if (myCounter3 == 4 && myCounter4 == 2) {object[12].Graphic = 396; } //check
else if (myCounter3 == 4 && myCounter4 == 3) {object[12].Graphic = 390; } //check
else if (myCounter3 == 4 && myCounter4 == 4) {object[12].Graphic = 384; } //check
else if (myCounter3 == 5 && myCounter4 == 1) {object[12].Graphic = 406; } //check
else if (myCounter3 == 5 && myCounter4 == 2) {object[12].Graphic = 400; } //check
else if (myCounter3 == 5 && myCounter4 == 3) {object[12].Graphic = 394; } //check
else if (myCounter3 == 5 && myCounter4 == 4) {object[12].Graphic = 388; } //check
else if (myCounter3 == 6 && myCounter4 == 1) {object[12].Graphic = 403; } //check
else if (myCounter3 == 6 && myCounter4 == 2) {object[12].Graphic = 397; } //check
else if (myCounter3 == 6 && myCounter4 == 3) {object[12].Graphic = 391; } //check
else if (myCounter3 == 6 && myCounter4 == 4) {object[12].Graphic = 385; } //check
}

This looks like you've copy-pasted the same code twice, just changing the object index and variables. And you're planning to do the same thing for each of the nine segments?

STOP!

If you ever find yourself copying large chunks of code in multiple places, you need to stop and think, because that's hardly ever right. What you should do here is to have a function that updates one of the displays, which takes the object that needs to be updated and the values that control how it will be updated as arguments:

Code: ags
// Corrected function name
function UpdateLavaState()
{
  if (object[1].Visible == true) { myCounter1 = 4; myCounter2 = 4;  return;}
  if (object[2].Visible == true) { myCounter3 += 3; myCounter4 += 2; myCounter1 -= 1; myCounter2 -= 1; return;}
}

function UpdateLavaDisplay(Object* lavaDisplay, int lavaTemp, int lavaHeight)
{
  if (lavaTemp == 0 && lavaHeight == 0)      { lavaDisplay.Graphic = 376; } 
  else if (lavaTemp == 1 && lavaHeight == 1) { lavaDisplay.Graphic = 404; } //check
  else if (lavaTemp == 1 && lavaHeight == 2) { lavaDisplay.Graphic = 398; } //check
  else if (lavaTemp == 1 && lavaHeight == 3) { lavaDisplay.Graphic = 392; } //check
  else if (lavaTemp == 1 && lavaHeight == 4) { lavaDisplay.Graphic = 386; } //check
  else if (lavaTemp == 2 && lavaHeight == 1) { lavaDisplay.Graphic = 407; } //check
  else if (lavaTemp == 2 && lavaHeight == 2) { lavaDisplay.Graphic = 401; } //check
  else if (lavaTemp == 2 && lavaHeight == 3) { lavaDisplay.Graphic = 395; } //check
  else if (lavaTemp == 2 && lavaHeight == 4) { lavaDisplay.Graphic = 389; } //check
  else if (lavaTemp == 3 && lavaHeight == 1) { lavaDisplay.Graphic = 405; } //check
  else if (lavaTemp == 3 && lavaHeight == 2) { lavaDisplay.Graphic = 399; } //check
  else if (lavaTemp == 3 && lavaHeight == 3) { lavaDisplay.Graphic = 393; } //check
  else if (lavaTemp == 3 && lavaHeight == 4) { lavaDisplay.Graphic = 387; } //check
  else if (lavaTemp == 4 && lavaHeight == 1) { lavaDisplay.Graphic = 402; } //check
  else if (lavaTemp == 4 && lavaHeight == 2) { lavaDisplay.Graphic = 396; } //check
  else if (lavaTemp == 4 && lavaHeight == 3) { lavaDisplay.Graphic = 390; } //check
  else if (lavaTemp == 4 && lavaHeight == 4) { lavaDisplay.Graphic = 384; } //check
  else if (lavaTemp == 5 && lavaHeight == 1) { lavaDisplay.Graphic = 406; } //check
  else if (lavaTemp == 5 && lavaHeight == 2) { lavaDisplay.Graphic = 400; } //check
  else if (lavaTemp == 5 && lavaHeight == 3) { lavaDisplay.Graphic = 394; } //check
  else if (lavaTemp == 5 && lavaHeight == 4) { lavaDisplay.Graphic = 388; } //check
  else if (lavaTemp == 6 && lavaHeight == 1) { lavaDisplay.Graphic = 403; } //check
  else if (lavaTemp == 6 && lavaHeight == 2) { lavaDisplay.Graphic = 397; } //check
  else if (lavaTemp == 6 && lavaHeight == 3) { lavaDisplay.Graphic = 391; } //check
  else if (lavaTemp == 6 && lavaHeight == 4) { lavaDisplay.Graphic = 385; } //check
}

And then you could just do:

Code: ags
function room_RepExec()
{
  UpdateLavaState();
  UpdateLavaDisplay(object[11], myCounter1, myCounter2); // Segment 1
  UpdateLavaDisplay(object[12], myCounter3, myCounter4); // Segment 2
}

As you add more segments, you'll save more and more code.

(Khris already told you about other things you should be doing that would be better than this, but I guess as a novice coder you are confused by his advice, so let's take it one step at a time.)

Khris

#31
Made a demo game:

https://drive.google.com/file/d/1Sypp0xs5XjlZUQleTxxjGfJbeQxGkBvB/view?usp=sharing

I implemented the three buttons; they switch hatches and randomly set the temperature and level.

This uses three sprites only, a few more are needed to polish the optics. The colored squares are rectangles drawn to the background, then the panel is drawn on top.

Here's the room script:
Spoiler
Code: ags
// room script file

// for each line, store active column, temp and level
int hatch[3];
int temp[3];
int level[3];

// 6 colors for 6 temperatures
int col[6];

void DrawSquare(DrawingSurface* ds, int line) {
  ds.DrawingColor = col[temp[line]];
  int x = hatch[line] * 220 + 370;
  int y = line * 140 + 180;
  ds.DrawRectangle(x, y + 40 - level[line] * 5, x + 60, y + 60);
}

void UpdateScreen() {
  DrawingSurface* ds = Room.GetDrawingSurfaceForBackground();
  ds.Clear(0);
  // draw squares on background
  for (int line = 0; line < 3; line++) {
    DrawSquare(ds, line);
  }
  // draw panel on top
  ds.DrawImage(286, 135, 3);
  // draw green lights on panel
  for (int line = 0; line < 3; line++) {
    for (int column = 0; column < 3; column++) {
      int slot = 2; // off
      if (hatch[line] == column) slot = 1; // on
      ds.DrawImage(column * 215 + 475, line * 142 + 175, slot);
    }
  }
  ds.Release();
}

function room_Load()
{  
  // grey
  col[0] = Game.GetColorFromRGB(128, 128, 128);
  // light orange
  col[1] = Game.GetColorFromRGB(255, 220,  80);
  // orange
  col[2] = Game.GetColorFromRGB(255, 160,   0);
  // dark orange 
  col[3] = Game.GetColorFromRGB(210, 135,   0);
  // light red
  col[4] = Game.GetColorFromRGB(255, 100, 100);
  // dark red
  col[5] = Game.GetColorFromRGB(170,   0,   0);
  
  UpdateScreen();
}

void HandleLine(int line) {
  hatch[line] = (hatch[line] + 1) % 3; // switch column
  // random values for now
  temp[line] = Random(5);
  level[line] = Random(5);
  UpdateScreen();  
}

function hButton_AnyClick(Hotspot *theHotspot, CursorMode mode)
{
  // hotspots 1-3 => lines 0-2
  HandleLine(theHotspot.ID - 1);
}
[close]

Jordanowen42

Quote from: Jordanowen42 on Fri 07/03/2025 06:48:25(Khris already told you about other things you should be doing that would be better than this, but I guess as a novice coder you are confused by his advice, so let's take it one step at a time.)

You are correct that I definitely find a lot of this confusing but I suppose the difficulty I'm having trouble seeing is how these things pertain to my overall problem. It seems like most of what you're telling me are essentially formatting issues that would make the script more succinct but I don't see how they will resolve the issues I described in the video.

Also, while I realize that you probably don't have the time it would help to see your updates inside of the larger whole of my existing script. A lot of what I'm seeing here is very alien to me.
Quote from: Khris on Fri 07/03/2025 10:10:12Made a demo game:

https://drive.google.com/file/d/1Sypp0xs5XjlZUQleTxxjGfJbeQxGkBvB/view?usp=sharing

I implemented the three buttons; they switch hatches and randomly set the temperature and level.

This uses three sprites only, a few more are needed to polish the optics. The colored squares are rectangles drawn to the background, then the panel is drawn on top.

Here's the room script:
Spoiler
Code: ags
// room script file

// for each line, store active column, temp and level
int hatch[3];
int temp[3];
int level[3];

// 6 colors for 6 temperatures
int col[6];

void DrawSquare(DrawingSurface* ds, int line) {
  ds.DrawingColor = col[temp[line]];
  int x = hatch[line] * 220 + 370;
  int y = line * 140 + 180;
  ds.DrawRectangle(x, y + 40 - level[line] * 5, x + 60, y + 60);
}

void UpdateScreen() {
  DrawingSurface* ds = Room.GetDrawingSurfaceForBackground();
  ds.Clear(0);
  // draw squares on background
  for (int line = 0; line < 3; line++) {
    DrawSquare(ds, line);
  }
  // draw panel on top
  ds.DrawImage(286, 135, 3);
  // draw green lights on panel
  for (int line = 0; line < 3; line++) {
    for (int column = 0; column < 3; column++) {
      int slot = 2; // off
      if (hatch[line] == column) slot = 1; // on
      ds.DrawImage(column * 215 + 475, line * 142 + 175, slot);
    }
  }
  ds.Release();
}

function room_Load()
{  
  // grey
  col[0] = Game.GetColorFromRGB(128, 128, 128);
  // light orange
  col[1] = Game.GetColorFromRGB(255, 220,  80);
  // orange
  col[2] = Game.GetColorFromRGB(255, 160,   0);
  // dark orange 
  col[3] = Game.GetColorFromRGB(210, 135,   0);
  // light red
  col[4] = Game.GetColorFromRGB(255, 100, 100);
  // dark red
  col[5] = Game.GetColorFromRGB(170,   0,   0);
  
  UpdateScreen();
}

void HandleLine(int line) {
  hatch[line] = (hatch[line] + 1) % 3; // switch column
  // random values for now
  temp[line] = Random(5);
  level[line] = Random(5);
  UpdateScreen();  
}

function hButton_AnyClick(Hotspot *theHotspot, CursorMode mode)
{
  // hotspots 1-3 => lines 0-2
  HandleLine(theHotspot.ID - 1);
}
[close]

I ran the game you provided and it worked but I'm not sure how to incorporate this into my code. I'm pretty much at the level of copy/pasting and what you see in my original code is the extent of my programming knowledge. I appreciate your help tho.

Snarky

#33
Quote from: Jordanowen42 on Sat 08/03/2025 06:21:51You are correct that I definitely find a lot of this confusing but I suppose the difficulty I'm having trouble seeing is how these things pertain to my overall problem. It seems like most of what you're telling me are essentially formatting issues that would make the script more succinct but I don't see how they will resolve the issues I described in the video.

I understand what you mean, but I would turn that around. You're over-focused on a particular bug that is blocking you from moving forward right now (which is a very natural attitude when you just want it to work, dammit!), but the bigger problem is that you've built it wrong from the start.

This is essentially a pretty simple task, but you've over-complicated it in various ways, which leaves a lot of scope for mistakes. If the structure isn't fixed, you're going to be playing whack-a-mole with bugs every step of the way, and even if you ultimately get it to work it will be a mess that doesn't really make sense.

That's why it's better to step back and try to fix the foundation. There's a good chance the bug then won't appear at all, and if it does it will be much easier to fix.

And consider: this is not the only bit of coding you'll be doing for this game, right? In the big picture, it's more valuable to learn ways to avoid or solve such problems in the future, than to hack this until it somehow works and then immediately run into similar difficulties with the next task.

pell

@Jordanowen42 Are you still having the problem we talked about?

Regarding the coding advice we've given you, I suggest taking whatever makes sense to you as a beginner and moving on to make your game. You can fall into a trap of rewriting things every time you get (or somebody gives you) a better idea and never get your project finished.

A lot of these things will make more sense to you as you gain experience.

Khris

A first step would be to replace myCounter1, myCounter2, etc. with properly named arrays.
Using arrays is very basic but also very useful.

Declare one at the top of the script:
  int temp[3]; // three is the size, i.e. the number of variables you'll get

This creates three variables: temp[0], temp[1] and temp[2]. You can use these exactly like you would temp1, temp2 and temp3 with the added benefit that you can use a variable as the index:
  int a = 1;
  temp[a] = 4;

You can even use an expression:
  temp[(4 + 2) / 3]
for instance is perfectly fine and will be the equivalent of temp[2].
(These examples are pretty nonsensical, they're just supposed to show what is possible)

The next useful thing is custom functions; you're already using them. It essentially means turning a bunch of lines into a single command, with the additional benefit of being able to customize each call using the function's parameters.

Writing code for Myst-style puzzles is way easier to do and debug if you learn a few basic programming concepts first.
Like Snarky said, it's not just about implementing this specific puzzle.

Jordanowen42

Let me pose another question- and I'm quite serious: how much would one of you chaps charge to program the whole puzzle for me?

pell

Quote from: Jordanowen42 on Sun 09/03/2025 03:38:16Let me pose another question- and I'm quite serious: how much would one of you chaps charge to program the whole puzzle for me?

I'm always up for a freelance gig, and you can message me if you're serious. But like I said before, I think you're probably one line of code away from fixing the immediate issue of the counter automatically changing through all its stages.

SMF spam blocked by CleanTalk