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 (Request) Moving Shelf using Secrets Books
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#2
RE: (Request) Moving Shelf using Secrets Books

So you're thinking a puzzle similar to the one in the Archives, except instead of a time limit, they're ordered? So I presume no time limit for this one?

There are several ways of achieving this. First thing I can think of goes like this:

Let's say we have 3 books, book_1, book_2 and book_3. They need to be pulled in the order they are named, but they all look the same.

They each have an area covering the position they need to be in to trigger the pull. These are named area_book_1, area_book_2 and area_book_3.

[Image: 7mgK5VK.png]

Start by adding the collision callbacks:

PHP Code: (Select All)
int bookCounter 1;

void OnStart() 
{
    for(
int i 14i++) 
    {
        
AddEntityCollideCallback("book_"+i"area_book_"+i"PullBook"false1);
    }


PS: If you're unfamiliar with for-loops, here it basically just means that it runs the AddEntityCollideCallback 3 times, with a temporary variable counting 1, 2 and 3 (the variable is named i). I then use that variable to append the current loop number to the names of the books and areas.

The integer variable that I set here will be used later (remember to place it outside the OnStart function).

So now that this callback is created, we need the callback event:

PHP Code: (Select All)
void PullBook(string &in asParentstring &in asChildint alState
{
    
//this should run every time you pull a book out from the shelf.
    //to verify this, enable debug messages for your game and see if this shows up:
    
AddDebugMessage("Pulled a book!"false);


PS: If you don't know how to enable debug messages, see this guide.

If this message doesn't show up when you want it, you may have to tweak the position of the area and the book so that they trigger when they should.

Now, let's put the checks in that PullBook function.

PHP Code: (Select All)
void PullBook(string &in asParentstring &in asChildint alState
{
    if(
asParent == StringSub(asParent0asParent.length() - 1) + bookCounter
    {
        
AddDebugMessage("Pulled the correct book. Number: " bookCounterfalse);
        
bookCounter++;
    } 
    else 
    {
        
AddDebugMessage("Pulled the wrong book. Number: " bookCounterfalse);
        
bookCounter 1;
    }

    if(
bookCounter 3
    {
        
AddDebugMessage("Completed the puzzle!"false);
    }


Ok, so the explanation for this is as follows:

First we check if the number associated with the current book you're pulling matches the value of the bookCounter variable. If the player pulls book_2 first, then that will not match the bookCounter variable, but if they pull book_1, then it will. If it matches, it runs the following section. If not, it runs the else-section.

If they pull the correct book, the bookCounter increases by one, so that it now matches the digit on the next book.

If they pull the wrong book, the bookCounter variable resets back to 1 and the player has to start over.

After they've pulled the first book, bookCounter becomes 2 (still not enough for the completed if-statement). After the second book, it becomes 3 (still not). After the third and final book, the value is increased to 4, which is more than 3, which is what the final if-statement checks for. Once you reach this section, the puzzle is complete.

That's the magic of it all. But remember, there's more you need to do in terms of resetting the books when they fail it, and disabling the event when they complete it so that it doesn't happen multiple times. Scripts you may find useful for this are ResetProp, SetPropStaticPhysics, perhaps AddPropForce, and RemoveEntityCollideCallback.

PS: If you're curious about the first if-statement and what it really means, remember that asParent is a string reference to the parent entity that triggered this current event (for example book_1). StringSub is a function that creates a new sub string from another string. I put asParent into it, and told it to create a new string from index 0 through the last index minus 1. Basically it took book_1 and removed the last character, and then it added the bookCounter instead.

You can read about all the functions on the wiki.

I wrote this out of visualization and memory so I may have overlooked something. Hopefully not.

(This post was last modified: 05-09-2017, 12:25 PM by Mudbill.)
05-06-2017, 02:15 PM
Find


Messages In This Thread
RE: (Request) Moving Shelf using Secrets Books - by Mudbill - 05-06-2017, 02:15 PM



Users browsing this thread: 1 Guest(s)