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
Trying to script
Melaara Offline
Junior Member

Posts: 19
Threads: 2
Joined: Apr 2014
Reputation: 1
#1
Trying to script

Hi there, everybody.

I'm new to the forum and also to scripting in the game. I hope that someone might explain to me how to script a little better. xD First of all I would like that someone corrects my hps file. There's no error but some of the things just don't work. Right now my locked door won't open (when I try to use the key it says 'Item cannot be used this way') and my monster won't spawn. It might be that I placed some things wrong, because I just don't know any better. ^^" I hope anybody can help me.

hps file:




////////////////////////////
// Run first time starting map
void OnStart()

{
AddUseItemCallback("", "key_1", "mansion_1", "KeyOnDoor", true);
AddEntityCollideCallback("Player", "scr_enemy1", "MonsterFunction", true, 1);
AddEnemyPatrolNode("servant_grunt_1", "PathNodeArea_1", 0, "");
AddEnemyPatrolNode("servant_grunt_1", "PathNodeArea_4", 0.001, "");
AddEnemyPatrolNode("servant_grunt_1", "PathNodeArea_8", 0, "");
SetEntityActive("servant_grunt_1", true);
}
{
SetSwingDoorLocked("mansion_1", false, true);
PlaySoundAtEntity("", "unlock_door", "mansion_1", 0, false);
RemoveItem("key_1");
}
void KeyOnDoor(string &in asItem, string &in asEntity)
{
}
void MonsterFunction(string &in asParent, string &in asChild, int alState)

////////////////////////////
// Run when entering map
void OnEnter()
{

}

////////////////////////////
// Run when leaving map
void OnLeave()
{

}
(This post was last modified: 04-24-2014, 03:35 PM by Melaara.)
04-24-2014, 03:30 PM
Find
PutraenusAlivius Offline
Posting Freak

Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation: 119
#2
RE: Trying to script

First of all, welcome to the forums!

Second, here's your issue:
PHP Code: (Select All)
{
SetSwingDoorLocked("mansion_1"falsetrue);
PlaySoundAtEntity("""unlock_door""mansion_1"0false);
RemoveItem("key_1");

void KeyOnDoor(string &in asItemstring &in asEntity)
{
}
void MonsterFunction(string &in asParentstring &in asChildint alState
The KeyOnDoor callback function and the 3 functions above it are misplaced, and the MonsterFunction callback doesn't have braces, this is what it should look like.

PHP Code: (Select All)
void KeyOnDoor(string &in asItemstring &in asEntity)
{
SetSwingDoorLocked("mansion_1"falsetrue);
PlaySoundAtEntity("""unlock_door""mansion_1"0false);
RemoveItem("key_1");
}

void MonsterFunction(string &in asParentstring &in asChildint alState)
{


"Veni, vidi, vici."
"I came, I saw, I conquered."
04-24-2014, 03:36 PM
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#3
RE: Trying to script

Welcome to the forums!

I suppose I could give you some basics to scripting. This will be my understanding of it, so correct me if I'm wrong anywhere. I'll just be doing it at the top of my head, so hopefully I won't forget anything important xP


Don't forget to always have a copy of this page available.

Amnesia's scripts are done in the language of AngelCode which is a branch of C++. If you need any foundation help, you can always Google C++ tutorials. They should cover what things mean, and they mostly also apply to AngelCode.

Most of the script consists of code blocks which each execute some code within. Mainly what you do is call the code block. Once you do, whatever is inside it will run. Either you use a callback (check) to execute a code block at a specific time or event, or you use a sequence of code blocks to execute one after the other.

PHP Code: (Select All)
void Constructor(string &in asParameter)
{
    Function(
"String"1.0ftrue);


This example code block has a few distingt objects:

void is the keyword. It determines what this code block will return. What that means is that it will send something back to the function calling the block. There are several keywords, but void is most common.

void - It returns nothing to the caller.
bool - It returns either true or false to the caller.
int - It returns a non-decimal numeral.
float - It returns a decimal numeral.
string - It returns a piece of text wrapped in double-quotes.
char - It returns a single character wrapped in single-quotes.

Most of the time you will be using void, so you won't have to worry about this. There are more keywords than these, but these are the most common ones (well, char isn't much used either).

Constructor is the name of the code block. The name can often be whatever you want it to be, just don't use spaces or special characters. I like to use capitals to separate words. I named it Constructor here because this whole line is called the constructor of a block.

(string &in asParameter) is the parameter field. The parameters are contained within parenthesis. The constructor's parameters are used to define what kind of code block this is. Some have no parameters (by leaving the parenthesis empty), others require a set of them. The whole constructor line needs to match what is calling it in order to be recognized. So basically if your code block is named StartEffect but the parameters don't match what is required by the function calling this block, it will not find it.
Don't worry too much about them though. Not yet at least. All you really need to worry about from the start is to have them there. If you don't know what parameters to use, you can copy them from the syntax right below the function you used to call it, written on the Engine Scripts page.

After opening the block itself, you'll find Function which is the name of the function you want to run within this block. There are many on the Engine Scripts page you can use. You can even make your own, but you don't need to, so I suggest you wait with that. An example of a function is PlayMusic. You can ctrl/cmd-f that on the scripts page and take a look. It has the parameters, syntax and documentation.

The function often has parameters just like the constructor. They are separated by commas if there are several. The first one is "String". This is, obviously, a string param. Strings, as mentioned above, return pieces of text. Remember to always either USE quotes around the text, or point to a VARIABLE that contains text.
Any parameter can be directly input with a value or replaced with a variable. Let's say you have a situation like this:

PHP Code: (Select All)
void Constructor(string &in asParameter)
{
    
string text "String";
    Function(
text1.0ftrue);


This time, the result is the same, but instead of inputting "String" in the param, you use a variable named text which eventually leads to an actual string piece containing "String". You can also use the current constructor parameters as variables to point to the value of the function calling the block. Get it? If not, don't worry too much about it. You don't need it, it's just very helpful for more advanced scripts.

Perhaps you can already guess what the other two parameters are. 1.0f is a float param, and true is a bool param. Floats use decimals, hence the period. The 'f' is often there to help you know that it is a float, but it's not required.

Lastly, you always end a function line with a semi-colon. DON'T use a semi-colon at the end of the contructor. You either use a block ( using {braces}) or a semi-colon, but never both. Don't forget that you DO need one. If you have none, the game will crash. Don't have constructors without blocks, and don't have function calls without semi-colons.


That's some overview of how the syntax is. When it comes to practise, you'll find it to be quite simple.

You already have void OnStart(). This is the constructor for the code block that is pre-programmed to run when the level is loaded for the first time. It's often used for starting effects and callbacks. I can see you've already done some work, so I'll assume you already know this. The functions themselves are pretty straight forward if you read the documentation on the Engine Scripts page.

I only really see the problem here as being that you've put the constructor line below the code block. Always have the constructor above the braces.

(This post was last modified: 04-24-2014, 04:38 PM by Mudbill.)
04-24-2014, 04:34 PM
Find
Melaara Offline
Junior Member

Posts: 19
Threads: 2
Joined: Apr 2014
Reputation: 1
#4
RE: Trying to script

Thank you all very much! With your help I solved the problem with the script! Big Grin (Altough I still have problems with the monster spawning because it spawns as soon as I just look at the area. ^^)
And I finally understand what I'm actually writing! Smile Thank you, everybody!
(This post was last modified: 04-24-2014, 05:38 PM by Melaara.)
04-24-2014, 05:36 PM
Find
FlawlessHappiness Offline
Posting Freak

Posts: 3,980
Threads: 145
Joined: Mar 2012
Reputation: 171
#5
RE: Trying to script

Post your script again, then we can solve the monster-thingy Wink

Trying is the first step to success.
04-24-2014, 06:19 PM
Find
Melaara Offline
Junior Member

Posts: 19
Threads: 2
Joined: Apr 2014
Reputation: 1
#6
RE: Trying to script

(04-24-2014, 06:19 PM)FlawlessHappiness Wrote: Post your script again, then we can solve the monster-thingy Wink

My Script looks now like this:



////////////////////////////
// Run first time starting map
void OnStart()

{
AddUseItemCallback("", "key_1", "mansion_1", "KeyOnDoor", true);
AddEntityCollideCallback("Player", "scr_enemy1", "MonsterFunction", true, 1);
AddEnemyPatrolNode("servant_grunt_2", "PathNodeArea_1", 0, "");
AddEnemyPatrolNode("servant_grunt_2", "PathNodeArea_4", 0.001, "");
AddEnemyPatrolNode("servant_grunt_2", "PathNodeArea_8", 0, "");
SetEntityActive("servant_grunt_2", true);
}
void KeyOnDoor(string &in asItem, string &in asEntity)
{
SetSwingDoorLocked("mansion_1", false, true);
PlaySoundAtEntity("", "unlock_door", "mansion_1", 0, false);
RemoveItem("key_1");
}

void MonsterFunction(string &in asParent, string &in asChild, int alState)
{
}
////////////////////////////
// Run when entering map
void OnEnter()
{

}

////////////////////////////
// Run when leaving map
void OnLeave()
{

}



But I think the problem has nothing to do with the script but more with the editor. I read on the wiki that somewhere I have to insert "Playercollide" so that when I walk into the area the monster will appear, but even after several tutorials I still haven't found where to insert that. Smile
04-24-2014, 07:18 PM
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#7
RE: Trying to script

The reason nothing happens is because you have nothing in your MonsterFunction block.

PHP Code: (Select All)
void MonsterFunction(string &in asParentstring &in asChildint alState)
{


It's empty. You need to put something in there. Probably wanna move the SetEntityActive function you have in OnStart into there instead.

Do you have the area in your level editor named "scr_enemy1"?

04-24-2014, 08:07 PM
Find
Melaara Offline
Junior Member

Posts: 19
Threads: 2
Joined: Apr 2014
Reputation: 1
#8
RE: Trying to script

(04-24-2014, 08:07 PM)Mudbill Wrote: The reason nothing happens is because you have nothing in your MonsterFunction block.

PHP Code: (Select All)
void MonsterFunction(string &in asParentstring &in asChildint alState)
{


It's empty. You need to put something in there. Probably wanna move the SetEntityActive function you have in OnStart into there instead.

Do you have the area in your level editor named "scr_enemy1"?


That finally worked! Thank you! I did not know that I had to put it in there since all I wanted to do was to spawn the monster that moment I walk into the script area. I watched some tutorials, nothing was mentioned there either. :/ Well, at least it's solved! Thank you! Smile
04-24-2014, 11:26 PM
Find




Users browsing this thread: 1 Guest(s)