Frictional Games Forum (read-only)
[SCRIPT] Question about parameters - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: [SCRIPT] Question about parameters (/thread-24225.html)



Question about parameters - Zokrar - 12-26-2013

I'm relatively uneducated when it comes to programming in general, so if possible, please try to explain simply.

My question is, why do callback functions require parameters? Such as...

Code:
void RandomFunc(string &in asParent, string &in asChild, int alState)

For example, if I have the following script:

Code:
AddEntityCollideCallback("Player", "AreaDoor", "EventDoor", true, 1);

void EventDoor(string &in asParent, string &in asChild, int alState)
{
    
    AddDebugMessage("Player collided with AreaDoor. Starting door scare.", false);
    SetSwingDoorClosed("Door", false, false);
    SetSwingDoorDisableAutoClose("Door", true);

    PlaySoundAtEntity("creak", "joint_door_move_special.snt", "Door", 1, false);

    AddTimer("", 2, "TimerStopSound");
    AddTimer("Door", 0, "TimerMoveDoor");
    
}

Why were the parameters required in the EventDoor function? I didn't reference them in the brackets.


RE: Question about parameters - Romulator - 12-26-2013

Assuming the parameters are the Parent, Child and State, they are primarily used in the declaration to determine what collides with what.

Basically, they are there so that within this routine if you do need to reference them within the braces, you can call them from the void, and the void pulls them from the declaration. This can save you from typing in the object names from the Level Editor on multiple occasions if things only happen between these two objects. A good example is using alState in an if statement:

PHP Code:
void func_shelf(string &in asEntityint alState)
{
     if (
alState == 1)           //Pull lever down all the way.
     
{
     
SetMoveObjectState("shelf",1.0f);           //Move shelf to reveal path
     
PlaySoundAtEntity("""quest_completed.snt""shelf_move_1"0false);       //Play some catchy tunes
          
return;
     }


Another one could be something like using a key to open a door:

PHP Code:
void OnStart()
{
    
AddUseItemCallback("""rusty_key""locked_door_1""OpenDoor"true);
}
 
void OpenDoor(string &in asItemstring &in asEntity)
{
    
SetSwingDoorLocked(asEntityfalsetrue);          //locked_door_1 unlocks
    
PlaySoundAtEntity("""unlock_door"asEntity0false);     //Play unlock sound at door
    
RemoveItem(asItem);                     //Key comes out of inventory


Bottom line is that if something happens to the declared Parent, Child, State, Effects etc, they can be referenced within the braces with ease Smile


RE: Question about parameters - PutraenusAlivius - 12-26-2013

In my opinion, these callback syntaxes gives the parameter's types.

Like in a timer:
PHP Code:
string &in asTimer 
It gives the fact that the string is a timer string.


RE: Question about parameters - Zokrar - 12-26-2013

Okay, I see. Thanks for the replies. One more thing, what does "&in" mean? Is that a C memory thing?

Also, seeing as the parameters are just... parameters, why do I have to call them asParent, asChild and such? Why could I not name them anything and have it work?


RE: Question about parameters - WALP - 12-26-2013

Well thats because you can name it anything and have it work. I often prefer using AsArea instead and I have had no issues with that.


RE: Question about parameters - Zokrar - 12-26-2013

Well, I'll be damned lol. That'll make things cleaner. One last question... I still don't understand why I must have parameters in the callback function, even if they're not used within the brackets. (If I don't have parameters, it doesn't crash the game, but the method simply does not do anything.)


RE: Question about parameters - PutraenusAlivius - 12-27-2013

(12-26-2013, 05:22 PM)Zokrar Wrote: Well, I'll be damned lol. That'll make things cleaner. One last question... I still don't understand why I must have parameters in the callback function, even if they're not used within the brackets. (If I don't have parameters, it doesn't crash the game, but the method simply does not do anything.)

It matches the signature of the parameter and it's respective callback syntax.

I think so.


RE: Question about parameters - Zokrar - 12-27-2013

(12-27-2013, 12:25 AM)JustAnotherPlayer Wrote: It matches the signature of the parameter and it's respective callback syntax.

I think so.

I don't understand what you mean. Could you try explaining it another way, please?


RE: Question about parameters - Romulator - 12-27-2013

In adding the callback, you assign what happens to what, such as in a Collide Callback, when the parent and the child collide, a certain function happens depending on the state they are in.

The function then needs the parent and child to assure that it is a proper collision function between these two. This might be a way to determine what happens if you had two functions with the same name, however Amnesia restricts that by returning an error either way. Like for an example, you cant have two functions called "CollidewithScript"


RE: Question about parameters - Adrianis - 12-28-2013

(12-26-2013, 05:22 PM)Zokrar Wrote: ... I still don't understand why I must have parameters in the callback function, even if they're not used within the brackets ...

The short answer is:
Because that's how Frictional Games have set it up. You don't have to use the parameters in the function, but they have to be there


The long answer is:
When the callback that you set up is activated, the engine looks for a function with the name you set the callback with (in your case, "EventDoor"), the word "void" at the beginning (which is whats called the 'return type' of the function), and the whole list of parameters.

The reason you can change the name of the parameter ('asEntity', 'alState', etc), but not the type ('string &in', 'int' etc) is because the engine is actually just looking for the list of parameter types, it doesn't care about the name.

The details it is looking for are...
PHP Code:
void YourFunctionName(string &in namestring &in nameint name
Those details that the callback is looking for, have already been set up in the engine, so we can't change them. That means that although you don't need use the parameters, they need to be there or else the engine won't be able to find what code to run (the function you write)


Quote:One more thing, what does "&in" mean? Is that a C memory thing?
It's related to it, yeh
The '&' means it takes the address to the bit of memory that the variable is in (meaning the actual value of the variable doesn't have to get copied into new memory, just an address), and 'in' means it can only take that data 'in', it can't pass it 'out' (which means that the value cannot be changed).
So, 'string &in' basically means its a string that function receives that cannot be changed, but its the original string, not a copy

Obviously there's loads more detail to all this, if you interested there's a manual on 'AngelScript', this language, heres a good place to start, but it assumes quite a bit of knowledge.