(Solved) Object is coming back and i want it to be gone forever

Started by actaria, Fri 19/08/2022 15:02:39

Previous topic - Next topic

actaria

Hello,

I am still working on a kitchen with a fridge.
With the help of Morganw i solved my fridge pick up items problem.

The player open the fridge and pick a chicken.
Then the player pick the chicken from inventory and click on the character to eat it

i went on my player character clicked on events that leads me to global script.

Global Script code:

Code: ags

function cChar1_UseInv()
{
if(cChar1.ActiveInventory == ichicken)
cChar1.Say("This chicken is delicious");
cChar1.LoseInventory(ichicken);
}


It works well but the problem is that since the chicken isnt in my inventory anymore it reappear in the fridge because of the condtion of my
fridge code and of course i want it to be gone forever in my stomach  :)



I tried to put:
chicken.Visible = false; in the global script but it looks that i can not call an object from there.

Here is the kitchen room code:

Code: ags

function fridge_close_Interact()
{
  fridge_close.Visible = false;
  fridge_open.Visible = true;
  chicken.Visible = !player.HasInventory(ichicken);
   }



I tried to find solutions and what is the best way to do this but i didnt find anything working.
Since it's kind of specific i didn't find anything that matches the same question.

Any help would be appreciated thanks a lot in advance.

Pax Animo

#1
If you want to change a rooms object via the global script you have to use:

Code: ags
object[ID].Visible = false;


but be careful using that as it changes all objects with that ID. Edit: Changes the object in the current room only.

You could use a global variable of type bool and set it to false after taking the chicken out of the fridge, then check if the bool is true or false when opening the fridge.

Just a few ideas.

Misunderstood

actaria

Hello Pax  Animo,

Thanks for the reply i am still leaning and i didn't know about that command for the global script, this is what i was looking for.

Good thing that you warned me the fact  that all objects with the same ID will be affected.

I will see what is the best solution between this and using a bool

Very nice ideas thanks again i should be fine with this  :)

Snarky

It won't affect all objects with that ID. It will affect the object with that ID in the room you are currently in.

The issue here is not the Global Script, and this will not fix it. The problem is the logic in the fridge_close_interact() function. Simply put, testing whether the chicken is in your inventory is wrong, for the reason you have discovered.

What you need is a variable that stores whether the chicken is in the fridge, and then display the chicken if it is.

Pax Animo

Quote from: Snarky on Fri 19/08/2022 19:05:25
It won't affect all objects with that ID. It will affect the object with that ID in the room you are currently in.

That's good to know, i can't remember where i read that all objects share the same ID globally throughout rooms. (or maybe i just misunderstood it at that time)

This will open up new possibilities for my self.

Cheers Snarky.
Misunderstood

Crimson Wizard

Quote from: Pax Animo on Fri 19/08/2022 23:33:04
That's good to know, i can't remember where i read that all objects share the same ID globally throughout rooms. (or maybe i just misunderstood it at that time)

To clarify, they do share same numeric IDs (these are just array indexes), but only one in the current room is affected at a time.

actaria

I didn"t managed to make it work yet, i tried lots of differents things with variable using at the beginning of my room script

Code: ags
int myCounter = 0;


I tried lots of differents codes but none of them is working as expected.
I also don't understand how i declare in the global script the fact that he chicken has been eaten.


I also tried with a bool but didnt work etiher  :)

at the beginning of the room script

Code: ags

bool eat_chicken = false;


room scipt:

Code: ags

function fridge_close_Interact()
{
fridge_close.Visible = false;
  fridge_open.Visible = true;
   if (eat_chicken)
   {
  chicken.Visible = false;
   }
    else{
       chicken.Visible = !player.HasInventory(ichicken);
   }
 }



Thanks for your help i am kind of lost.





Rik_Vargard

#7
Isn't there an error in line 7 ?

cihicken.Visible = false;

And maybe also in line 5

if (eat_chicken)  ... if you want to check if the chicken has been eaten you'll need:

if (eat_chicken = true);

and for line 10 I would write:

if (!player.HasInventory(ichicken)) chicken.Visible = true;

And also here, an error?

bool eat_chikken = false;

actaria

Quote from: Rik_Vargard on Sat 20/08/2022 11:53:57
Isn't there an error in line 7 ?

cihicken.Visible = false;
Hello and thanks for the help.

Yes it's because i translate from French to English to make it easier and i made a mistake typing here.

QuoteAnd maybe also in line 5

I have no error but maybe that's why it isn't working.

Quote
if (eat_chicken)  ... if you want to check if the chicken has been eaten you'll need:

if (eat_chicken = true);

and for line 10 I would write:

if (!player.HasInventory(ichicken)) chicken.Visible = true;
Ok i will try that  :)
Quote
And also here, an error?

bool eat_chikken = false;

same translation problem i am sorry i should have pay more attention.

Thanks for your help i will try again  :)

heltenjon

Sort of roundabout, but what if you create a character cFridge and give that character the chicken, then phrase it that the object chicken is visible if cFridge has the chicken inventory item?
I'm sure there are more elegant solutions, but that logic should work, at least.

Quote from: Crimson Wizard on Sat 20/08/2022 03:55:53
Quote from: Pax Animo on Fri 19/08/2022 23:33:04
That's good to know, i can't remember where i read that all objects share the same ID globally throughout rooms. (or maybe i just misunderstood it at that time)

To clarify, they do share same numeric IDs (these are just array indexes), but only one in the current room is affected at a time.
Does that mean that you affect all with the same numeric ID if you change it in a dialogue? (If dialogues take place "outside" all rooms, that is?)

Crimson Wizard

#10
Quote from: heltenjon on Sat 20/08/2022 15:01:50
Quote from: Crimson Wizard on Sat 20/08/2022 03:55:53
Quote from: Pax Animo on Fri 19/08/2022 23:33:04
That's good to know, i can't remember where i read that all objects share the same ID globally throughout rooms. (or maybe i just misunderstood it at that time)

To clarify, they do share same numeric IDs (these are just array indexes), but only one in the current room is affected at a time.
Does that mean that you affect all with the same numeric ID if you change it in a dialogue? (If dialogues take place "outside" all rooms, that is?)

It does not matter which script this is in, the only room which is affected is the current one at the time when the script is run.

actaria

This is how i finally made it works

I created a global variable

eat_chicken  type int  initial value 0

code in the room:

Code: ags

function fridge_close_Interact()
{
  fridge_close.Visible = false;
  fridge_open.Visible = true;
  if (eat_chicken == 0)
   {
  chicken.Visible = true;
   }
    if (player.HasInventory(ichicken))
   {
  chicken.Visible = false;
   }
     }




Thank you all for your help once again.

Rik_Vargard

Quote from: actaria on Sat 20/08/2022 12:47:11
same translation problem i am sorry i should have pay more attention.

My main language is also french, so sometime I use the french word for a variable or objects etc. when it comes more easily to me.   ;-D

actaria

Quote from: Rik_Vargard on Sat 20/08/2022 19:25:32
Quote from: actaria on Sat 20/08/2022 12:47:11
same translation problem i am sorry i should have pay more attention.

My main language is also french, so sometime I use the french word for a variable or objects etc. when it comes more easily to me.   ;-D

O yes i just saw under your nice avatar (is it Dracula ?) that you are from Belgium  :)

Snarky

I'm glad you got it to work, but I feel you're making it more complicated than it should be. As I suggested earlier, if you make a global variable called for example chicken_in_fridge (true at game start), you could simply have:

Code: ags

function fridge_close_Interact()
{
  fridge_close.Visible = false;
  fridge_open.Visible = true;
  chicken.Visible = chicken_in_fridge;
}


(And you would set chicken_in_fridge to false when the player takes it out of the fridge.)

This would be better not only because it is shorter, but because it more closely matches the real-world logic: the question is simply whether the chicken is in the fridge, so keep track of and test that instead of trying to rule out every other possibility. That way, it won't break even if you create lots of other possibilities for what could happen with the chicken; you wouldn't have to add additional tests for "gave chicken to ornitho-pathologist," "combined chicken with demon mask and drone to create flying monster," "used chicken as bait to catch killer dolphin" or whatever. And it would be straightforward to put the chicken back in the fridge simply by changing one variable.

SMF spam blocked by CleanTalk