Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Help Question about parameters
Zokrar Offline
Member

Posts: 85
Threads: 16
Joined: May 2013
Reputation: 0
#1
Question about parameters

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...

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

For example, if I have the following script:

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.

(This post was last modified: 12-26-2013, 05:04 AM by Zokrar.)
12-26-2013, 04:58 AM
Find Reply
Romulator Offline
Not Tech Support ;-)

Posts: 3,627
Threads: 63
Joined: Jan 2013
Reputation: 195
#2
RE: Question about parameters

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: (Select All)
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: (Select All)
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

Discord: Romulator#0001
[Image: 3f6f01a904.png]
(This post was last modified: 12-26-2013, 05:59 AM by Romulator.)
12-26-2013, 05:56 AM
Find Reply
PutraenusAlivius Offline
Posting Freak

Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation: 119
#3
RE: Question about parameters

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

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

"Veni, vidi, vici."
"I came, I saw, I conquered."
12-26-2013, 08:19 AM
Find Reply
Zokrar Offline
Member

Posts: 85
Threads: 16
Joined: May 2013
Reputation: 0
#4
RE: Question about parameters

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?

(This post was last modified: 12-26-2013, 04:53 PM by Zokrar.)
12-26-2013, 04:52 PM
Find Reply
WALP Offline
Posting Freak

Posts: 1,221
Threads: 34
Joined: Aug 2012
Reputation: 45
#5
RE: Question about parameters

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.
12-26-2013, 05:17 PM
Find Reply
Zokrar Offline
Member

Posts: 85
Threads: 16
Joined: May 2013
Reputation: 0
#6
RE: Question about parameters

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.)

(This post was last modified: 12-26-2013, 05:23 PM by Zokrar.)
12-26-2013, 05:22 PM
Find Reply
PutraenusAlivius Offline
Posting Freak

Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation: 119
#7
RE: Question about parameters

(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.

"Veni, vidi, vici."
"I came, I saw, I conquered."
12-27-2013, 12:25 AM
Find Reply
Zokrar Offline
Member

Posts: 85
Threads: 16
Joined: May 2013
Reputation: 0
#8
RE: Question about parameters

(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?

12-27-2013, 01:59 AM
Find Reply
Romulator Offline
Not Tech Support ;-)

Posts: 3,627
Threads: 63
Joined: Jan 2013
Reputation: 195
#9
RE: Question about parameters

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"

Discord: Romulator#0001
[Image: 3f6f01a904.png]
12-27-2013, 02:07 AM
Find Reply
Adrianis Offline
Senior Member

Posts: 620
Threads: 6
Joined: Feb 2012
Reputation: 27
#10
RE: Question about parameters

(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: (Select All)
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.

12-28-2013, 01:05 AM
Find Reply




Users browsing this thread: 1 Guest(s)