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 Riddle, is there a better way to do this?
WALP Offline
Posting Freak

Posts: 1,221
Threads: 34
Joined: Aug 2012
Reputation: 45
#1
Script Riddle, is there a better way to do this?

When helping daily fix a script issue in the other thread, with the books pulling puzzle from 03_archives, I ran into some "interresting" code, this is a copy paste from that thread in case it gets closed:

When reading frictionals code however, I discovered a piece of code I initially thought was retarded, but upon closer inspection seemed to be a workaround for a tricky code issue, and I was wondering what the ideal way to implement something like it would have been. You can take a look at the actual code yourself in the 03_archives.hps, but in short it goes like this:

PHP Code: (Select All)
void CollideSecretBook(string &in asBookstring &in asCollideAreaint BookState)
{
    
AddTimer(asBook,20,"PushBackBook1"); //name of the timer(asTimer) = name of the book being pulled out(asBook)
}

PushBackBook1(string &in asTimer)
{
    
SetPropObjectStuckState(asTimer, -1); //Pushes the book back
    
AddTimer("2"+asTimer0.25f"PushBackBook02"); //then just 0.25 seconds later the same book is put into the normal state. 
}

PushBackBook2(string &in asTimer)
{
    if(
asTimer == "2SecretBook_1"
    {
        
SetPropObjectStuckState("SecretBook_1"0);
    }
    else if(
asTimer == "2SecretBook_2"
    {
        
SetPropObjectStuckState("SecretBook_2"0);
     }
    else 
    {
        
SetPropObjectStuckState("SecretBook_3"0);
    }


Basically PushBackBook1 and PushBackBook2 do almost the same thing, but for some reason PushBackBook2 needs code for every single book while the other simply got the name of the book from the timer name.

The reason for this is that the timer that calls PushBackBook2, cant be named the same as the Book, since there's already a timer for PushBackBook1 with the very same name.
Hence the scripter had to rename the timer with the "2" in front, but the problem with doing this was that now he could not use the timer name anymore to pass on the name of the book, so instead of just writing:

SetPropObjectStuckState(asTimer, 0);

He had to go through and script for every single timer name

if(asTimer == "2SecretBook_1")
{
SetPropObjectStuckState("SecretBook_1", 0);
}
else if(asTimer == "2SecretBook_2")
{
SetPropObjectStuckState("SecretBook_2", 0);
}
else
{
SetPropObjectStuckState("SecretBook_3", 0);
}


This seems terribly messy, and I cant help but feel there has to be some way that the name of Book can be passed on directly from PushBackBook1 to PushBackBook2.

Any ideas?
02-24-2016, 11:56 PM
Find
Spelos Away
Banned

Posts: 231
Threads: 19
Joined: Sep 2014
#2
RE: Script Riddle, is there a better way to do this?

You can just work with its subString
PHP Code: (Select All)
PushBackBook2(string &in asTimer)
{
    
SetPropObjectStuckState(StringSub(asTimer112), 0);


And basically just get 12 characters of asTimer excluding the first one.

Other way would be to declare a LocalVariable for the current book.
Something like this:
PHP Code: (Select All)
PushBackBook1(string &in asTimer)
{
    
SetPropObjectStuckState(asTimer, -1); //Pushes the book back
    
SetLocalVarString("CurrentBook"asTimer);
    
AddTimer("2"+asTimer0.25f"PushBackBook02"); //then just 0.25 seconds later the same book is put into the normal state. 
}

PushBackBook2(string &in asTimer)
{
    
SetPropObjectStuckState(GetLocalVarString("CurrentBook"), 0);

(This post was last modified: 02-25-2016, 02:23 AM by Spelos.)
02-25-2016, 02:14 AM
Find
WALP Offline
Posting Freak

Posts: 1,221
Threads: 34
Joined: Aug 2012
Reputation: 45
#3
RE: Script Riddle, is there a better way to do this?

(02-25-2016, 02:14 AM)Spelos Wrote: You can just work with its subString
PHP Code: (Select All)
PushBackBook2(string &in asTimer)
{
    
SetPropObjectStuckState(StringSub(asTimer112), 0);


And basically just get 12 characters of asTimer excluding the first one.

Other way would be to declare a LocalVariable for the current book.
Something like this:
PHP Code: (Select All)
PushBackBook1(string &in asTimer)
{
    
SetPropObjectStuckState(asTimer, -1); //Pushes the book back
    
SetLocalVarString("CurrentBook"asTimer);
    
AddTimer("2"+asTimer0.25f"PushBackBook02"); //then just 0.25 seconds later the same book is put into the normal state. 
}

PushBackBook2(string &in asTimer)
{
    
SetPropObjectStuckState(GetLocalVarString("CurrentBook"), 0);


Substring seems to be the way to go, though somehow I still got this feeling there has to be an even better way lol.

I thought of the variable solution as well, but the problem with it is that we are dealing with multiple timers for each book here, so if say CurrentBook was Book1, but at the same time we had a timer going for Book2, which expires before Book1 is passed to PushBackBook2, then CurrentBook would be set to Book2, and PushBackBook2 would end up operating on it twice, without ever doing the operation on Book1.

Since in this particular situation the timer expires after only 0.25 sec it's extremely unlikely if not impossible that this would occur, but in similar scripts where the timer does not run out so quickly it would be a problem.

anyway thanks a lot, this real helpful
(This post was last modified: 02-25-2016, 02:52 AM by WALP.)
02-25-2016, 02:36 AM
Find




Users browsing this thread: 1 Guest(s)