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
Need script help asap! extremely difficult script!
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#10
RE: Need script help asap! extremely difficult script!

(12-19-2012, 07:34 PM)dnalange Wrote: I am an OK scripter but thisn is i bit too much for me atm...
I know it kinda looks scary but, trust me, it's not that hard. Just try to understand it one step at the time. Hopefully, you'll then be able to transfer the code to the script for your level.

I'll try to help here as much as I can. But, can you write in more detail next time which bits you don't understand, so that people here can focus on that.

OK, here goes.

Fist, are you unfamiliar with these?
PHP Code: (Select All)
int secretPassCode 231;
int numPresses 0;     // the number of button presses (so far)
int userEntry 0

Those are just local variables. For example, writing
int secretPassCode = 231;

is really similar to writing
SetLocalVarInt("secretPassCode", 231);

except that it's easier to work with, and potentially somewhat faster. Also, Amnesia's functions allow you to set only 3 types of data (SetLocalVarInt(), SetLocalVarFloat(), SetLocalVarString()), but the script engine itself supports variables, and it supports more types than just those 3.

The format is:
type variable_name = initial_value;

For example:
int numPresses = 0;
int userEntry = 0;
string myName = "TheGreatCthulhu";
bool isAmnesiaAwesome = true;
float pi = 3.14f;


So, using variables now I can write things like:
if (userEntry == secretPassCode)

instead of:
if (GetLocalVarInt("userEntry") == GetLocalVarInt("secretPassCode"))

It's easier to read and understand.

You should be familiar with the OnEnter() part - it simply sets what function is to be called when the player clicks on each script area. I've set them all to the same function, the OnButtonPress() function.

Now, I know that on the wiki it says that the callback syntax for SetEntityPlayerInteractCallback() is:
void MyFunc(string &in asEntity)

but note that the name of the single parameter, asEntity, is actually irrelevant - the types have to match (it has to be a string), and I can name it whatever I like. So I named it 'sender' - to denote that it represents the id of the script area which "sent" the button press.

Next, inside the function, on the line
int digit = GetDigitFromButton(sender);

I declare a local int variable called digit, that will hold the current digit the user entered, and assign it the return value of the GetDigitFromButton() function. I hope you understand what's going on there. Forget for the moment how GetDigitFromButton() does what it does - you just need to know that it takes the name of the script area as input, and returns the corresponding digit as output. The number it returns then get's stored inside the digit variable. OK?

GetDigitFromButton() accomplishes this simply through use of if statements - I've seen you've used them before, so I suppose you understand what's going on inside the function.


I'm guessing that you're having trouble to understand this line, though:
userEntry += digit * Pow(10, 2 - numPresses);

For that, we have to go back to my "a b c thing", as you called it. What I was trying to explain is this: suppose the player presses the buttons "2", then "3", then "1", to enter the code 231.
How do you get from separate digits ("2", "3", and "1") to the code 231?

Well, take a look at this:
231 = 200 + 30 + 1

This can be written as:
231 = (2 * 100) + (3 * 10) + (1 * 1)

Now I don't know how good you're at math, but numbers 100, 10, and 1 (and such) are known as powers of 10. That simply means this:

1000 = 10^3 = 10 * 10 *10
100 = 10^2 = 10 * 10
10 = 10^1 = 10
1 = 10^0 ------> (that's what mathematicians decided)

You read, for example, 10^2, as "10 to the power of 2" - this simply means 10 * 10, ot 100 (note that the number of zeros is 2).

OK. So I made a function which can raise integer numbers to some power.
So, what we wrote here as 10^2, we can now write in script as
Pow(10, 2)

The function will return 100 in that case.
So, what then, Pow(10, 2 - numPresses) does?

Well, since it's a 3 digit code, for the 1st button press, you want it to return 100, for the 2nd press you want it to return 10, and for the third press you want it to return 1.
(Remember how we got to 231 from "2", "3" and "1"?)

Which means that you need this:
Pow(10, 2) // returns 100
Pow(10, 1) // returns 10
Pow(10, 0) // returns 1


This is why we count how many button presses there were since the start of the sequence. What are the corresponding values of numPresses variable? These:
0
1
2


For this reason, I can use this variable to get the powers of 10 I need; the values of
Pow(10, 2 - numPresses) will be:

Pow(10, 2 - 0) // 10^2
Pow(10, 2 - 1) // 10^1
Pow(10, 2 - 2) // 10^0


Then I simply multiply this value with the current digit:
digit * Pow(10, 2 - numPresses);

and add the result to the overall sum:
userEntry = userEntry + digit * Pow(10, 2 - numPresses);

Because, as you remember:
231 = 200 + 30 + 1


If you're confused by the AddDebugMessage() function calls - these lines are not important; you can safely delete them, and the script will still work. These just display some messages in the lover left corner of the screen, so that you can follow what's going on while you're ingame (if you've enabled debug messages).


(12-19-2012, 07:34 PM)dnalange Wrote: now IF it is possible i would like to have an ENTER\accept to the finishing toutch...

It is possible, but some tweaks need to be made. Basically if the pass code is always the same length (same number of digits), you can simply add another interact callback function for the enter "button", and move all of the pass code checking code from OnButtonPress() to that other function.

That would be all the code inside the if (numPresses >= 3) part (just copy everything between { and }), except that now there's really no need to check if all of the buttons were pressed, since pressing "enter" is basically the Player saying "I'm done".

In the end, if you still have some trouble understanding how this all works - that's ok; you can copy and make use of the helper functions Pow() and GetDigitFromButton() to your code (also make sure to copy the INVALID_VALUE const at the top), and they should work - but, maybe you'd have to make some modifications to match the names and number of your script areas.

Any other parts you don't understand or need help with?
(This post was last modified: 12-19-2012, 09:14 PM by TheGreatCthulhu.)
12-19-2012, 09:09 PM
Find


Messages In This Thread
RE: Need script help asap! extremely difficult script! - by TheGreatCthulhu - 12-19-2012, 09:09 PM



Users browsing this thread: 1 Guest(s)