[SOLVED] on_event static non-property access error

Started by cipberbloom, Tue 03/08/2021 00:36:09

Previous topic - Next topic

cipberbloom

Greetings, all -

I'd like a series of actions to take place when anything is added to the inventory. One of the things I've tried is the following:

Code: ags

// When an inventory object is picked up, do this:
function on_event()
{
   if (EventType == eEventAddInventory)
   {
   invenOverlay = Overlay.CreateGraphical(300, 300, 137, false);
   Wait(40);
   invenOverlay.Remove();
   aMalMrBagPaperSack.Play();
   }
}


Which produces 'static non-property access: internal error' when compiled.


I know that it is this bit...

Code: ags
(EventType == eEventAddInventory)


...that's causing the error, so it's a matter of me not understanding what exactly does what, where things are meant to go and not go.

Any advice is much appreciated!  ;-D


Matti

Instead of

Code: ags
if (EventType == eEventAddInventory)


write

Code: ags
if (event == eEventAddInventory)


Because the function looks like this:

Code: ags
function on_event(EventType event, int data)


;)

cipberbloom

Thank you!  ;-D It seemed confusing in the manual to have 'event' as a parameter since the function is named 'on_event', but I guess the function could just as easily have been named Mordred or anything else!

The data for eEventAddInventory is supposed to be the number of the inventory item, but seeing as I want this to happen when any of the items are added I'm not sure what to put for the data. I've tried 0, null, and then dumb things didn't even think about not being valid integers and won't list here because I'm already feeling the programming-block shame/ineptitude so keenly (laugh).

Now I'm getting a 'Function declaration has wrong number of arguments to prototype' error.  ???





Khris

#3
Writing code like this requires making the mental switch from calling a function that is provided by AGS to writing a function that is called by AGS.

You need
Code: ags
function on_event(EventType event, int data)
{
  if (event == eEventAddInventory)
  {
    invenOverlay = Overlay.CreateGraphical(300, 300, 137, false);
    Wait(40);
    invenOverlay.Remove();
    aMalMrBagPaperSack.Play();
  }
}


What happens is this: you call something like  player.AddInventory(iWhatever);  and this causes AGS to call
Code: ags
  on_event(eEventAddInventory, iWhatever.ID);

Your task is to write this function so you can specify what happens when AGS calls it. This requires adhering to the parameters scheme exactly, for one. As you can see though, the if block ignores the data parameter.

PS: wrapping your mind around this is a lot easier if you are used to writing your own custom functions.

cipberbloom

Quote from: Khris on Tue 03/08/2021 07:49:22
Writing code like this requires making the mental switch from calling a function that is provided by AGS to writing a function that is called by AGS.
(...)
Quote
What happens is this: you call something like  player.AddInventory(iWhatever);  and this causes AGS to call
Code: ags
  on_event(eEventAddInventory, iWhatever.ID);

Your task is to write this function so you can specify what happens when AGS calls it. This requires adhering to the parameters scheme exactly, for one. As you can see though, the if block ignores the data parameter.

PS: wrapping your mind around this is a lot easier if you are used to writing your own custom functions.

Thank you!!! I had to read that more times than I'd like to admit, but I get it now, it clarifies so very much. This is going to help me a TONNE going forward. Man, you are a gem!  ;-D

Khris


SMF spam blocked by CleanTalk