[SOLVED] Changing visibility of objects in functions.

Started by PanzerHans, Fri 24/11/2023 21:25:56

Previous topic - Next topic

PanzerHans

Hello AGS comunnity!

I really honestly tried to find a solution to my problem myself, the solution that works for me seems too weird.

So - I have two objects in a room located on same place (a cabinets with an open/closed door) and I want the door to open and close by clicking, by changing the visibility of the objects. I was try:
------------------------------------------------------------------------------------------------------------------------------------------
1: In this version, only the first part ("opening the cabinet") works, and the game does not respond to further clicks.

Code: ags
function oCabinetClose_Interact()
{
if ((oCabinetClose.Visible == true) && (oCabinetOpen.Visible == false))
  {
    oCabinetClose.Visible = false;
    oCabinetOpen.Visible = true;
  }
  else if ((oCabinetClose.Visible == false) && (oCabinetOpen.Visible == true))
  {
  oCabinetClose.Visible = true;
  oCabinetOpen.Visible = false;
  }
 
} 
------------------------------------------------------------------------------------------------------------------------------------------

2: In this attempt, only the first part works again (again only "opened door"), but I suspect that this code is completely wrong.

Code: ags
function oCabinetClose_Interact()
{
 int i;
 i = oCabinetClose.Visible == true;
 if (i == true)
 {
  oCabinetClose.Visible = false;
 }
 else if (i == false)
 {
  oCabinetClose.Visible = true;
 }
}
-----------------------------------------------------------------------------------------------------------------------------------------

3: The only solution I came up with is to use a function on both objects.

Code: ags
function oCabinetClose_Interact()
{
if (oCabinetClose.Visible == true)
{
  oCabinetOpen.Visible = true;
  oCabinetClose.Visible = false;
}
}


function oCabinetOpen_Interact()
{
if (oCabinetOpen.Visible == true)
{
  oCabinetOpen.Visible = false;
  oCabinetClose.Visible = true
}
}

This work, but I suspect the situation could be handled much more elegantly. I hope that you, experienced guys, can advise how to solve such situations.

I will also add that BaselineOverriden is false, oCabinedClose is visible after startup(on top of oCabinetOpen), I make sure that the visibility states in the properties correspond to the start of code.

Thank you :=

RootBound

#1
I would just go with your first version and add a "return;" inside the end of the first if statement, which makes sure that only the first if statement runs. Otherwise the changes made by the first statement then cause the else if statement to run as well (if I'm reading it right).
They/them. Here are some of my games:

Khris

The problem with your first attempt isn't the code itself but the fact that you're turning the object invisible, which means it no longer reacts to clicks.

You can fix this by opening the events pane of oCabinetOpen and pasting "oCabinetClose_Interact" in the interacts line, this way the function runs when you interact with either object.

You also do not need "== true" in your conditions.
Code: ags
  if (oCabinetClose.Visible && !oCabinetOpen.Visible) ... 

In fact, all you need is:
Code: ags
function oCabinetDoor_Interact() {
  oCabinetClose.Visible = !oCabinetClose.Visible;
  oCabinetOpen.Visible = !oCabinetOpen.Visible;
}
Now paste oCabinetDoor_Interact as the function to call in both objects' event tables.


Note that you don't need a second object either, use one object called "cabinet door" and simply switch its sprite:
Code: ags
  oCabinetDoor.Graphic = 123 - oCabinetDoor.Graphic;
Replace 123 with the sum of the sprite slots of the two images.


PanzerHans

#3
Your solution works great - not surprising  ;) . The fact that I can directly call functions other than the automatically assigned ones from the event pan is great thing, I didn't know they could be used like that, I must have missed it. Thank you very much for your time, to all of you.

SMF spam blocked by CleanTalk