Author Topic: Help with count down timer needed please  (Read 436 times)

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Help with count down timer needed please
« on: October 06, 2014, 11:40:38 pm »


               

Do you ever have a problem that you think about a lot and just when you think you have found a solution it sort of slips away again. Well I've got one of those now. I have been thinking about this one for 6 hours or more on and off. I am almost certain that it is doable but  am not quit sure. Here is the problem -


 


I have a series of identical placeables that can be used. They all use an instance of the same onused script.

Once each one is used it cannot be re-used unless they are all reset.

Each one has a unique tag and also a variable that contains a unique value.


 


My problem is that I want to introduce a count down timer that counts down in seconds and would behave as follows.




Once the first item is used the counter is initialised and the count down begins

If another of the placeables is used before the countdown reaches zero, the counter is reset to its initial value and the count down continues with the new value.

Repeat until the last placeable is used.

If the count down reaches zero before another placeable is used, stuff happens immediately and the count down is terminated immediately.

Once the last placeable is used the count down is terminated immediately.

 


If at all possible it needs to be called from the onused script as that is where all the stuff (that happens when the counter reaches zero) is situated.


 


Sorry to be pain and keep asking all these questions when I really should be able to figure it out myself.


 


Thanks in advance for any help or comments.


 


TR


 


PS I checked through all the scripts that I posted as "Unearthed Gold" on the new vault but without success.


 



               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Help with count down timer needed please
« Reply #1 on: October 06, 2014, 11:52:27 pm »


               

how precise does the count down have to be? And who sees the countdown? and.. is this for single player?



               
               

               
            

Legacy_kamal_

  • Sr. Member
  • ****
  • Posts: 347
  • Karma: +0/-0
Help with count down timer needed please
« Reply #2 on: October 07, 2014, 12:32:21 am »


               Could you adapt the time written for nwn2?
http://neverwinterva...countdown-timer
               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Help with count down timer needed please
« Reply #3 on: October 07, 2014, 12:58:30 am »


               

@ kamal_ Thanks but I don't think it will work with nwn as there isn't a gui timer that I am aware of in nwn. BTW either NwN2 uses a unique version of xml or there is bug at the end of line 1 of the included xml file. The xml parsers that I have access to are expecting "?>" but only finding ">".


 


@ henesua Thanks for the quick response. Sorry, I should have mentioned that. The timer needs to count down in 1 second decrements. It is primarily intended for single player. it is actually for that more complex trap that I mentioned at the end of the ccc:September 2014: The Mists of Ravenloft thread. This is the last problem I need to get solved to finish it. Silly me, I thought this part would be easy instead it gives me a headache. The intention is for the player to see a number counting down over the PC's head (speakstring) but that should be the easiest part (famous last words?). Thanks for any help you can provide.


 


TR



               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Help with count down timer needed please
« Reply #4 on: October 07, 2014, 01:09:30 am »


               

I'd try a pseuduoheartbeat then. Use an execute script that calls itself. The event on the object would have to interact with it by setting data elsewhere to track it. It can be a fiddly thing to set this up, but it will work.


 


If you have no idea where to start let me know and if I get time I'll post something. I've only been working 8 to 10 hour days so the schedule has been lighter the past week.



               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Help with count down timer needed please
« Reply #5 on: October 07, 2014, 02:08:04 am »


               

Thank you. I've really no idea how to do pseudo heartbeat scripting. I've heard of them a long time ago (on the old, old nwn.bioware site I think) but do not recall having seen an example of one. Please, if you get the time, don't hurry too much as its well past midnight here in the UK and I'm about to go to bed.


 


Thank you for whatever you come up with.


 


TR



               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Help with count down timer needed please
« Reply #6 on: October 07, 2014, 06:43:26 am »


               

I put together a module with a set of scripts you can modify, and uploaded to dropbox. The module also serves as a demo. To configure the behavior edit puzzle_inc. Then recompile puzzle_plc_use and puzzle_count_ex


 


I probably could have piled it all into one script, but I tend to organize things this way.


               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
Help with count down timer needed please
« Reply #7 on: October 07, 2014, 07:02:38 am »


               

A pseudo-heartbeat script ends by rescheduling itself. For a one-second pseudo-heartbeat,



/* do stuff */
DelayCommand(1.0, ExecuteScript("this_script", OBJECT_SELF));

An outline of the OnUsed script logic (using local ints on the area or other central object) could be


 


  • Set the countdown local int to its maximum value (that way, it gets reset every time a placeable is used)

  • If the pseudo-heartbeat script hasn't started, run it. Set a local int to ensure it only starts once.

  • If all the placeables have been used, do stuff, set a local int to flag completion

The pseudo-heartbeat logic would be


 


  • If the completion flag is set, stop, clear all locals, don't run again

  • Reduce the countdown by one. If zero, do failure stuff, clear all locals, don't run again

  • Run again in 1 second

The point of this is to ensure that there can never be more than one instance of the heartbeat script, and that it doesn't keep running when the fun is over.



               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Help with count down timer needed please
« Reply #8 on: October 07, 2014, 03:09:08 pm »


               

@ henesua Thank you. I have downloaded and had a quick look, got brain ache, had another look, more brain ache, Ahhh!  '<img'>  I am almost certain that once I understand it I will be able to make it fit my needs. I just didn't realise there would be quite that much code... One thing I've already discovered is a command I didn't know existed. As soon as I saw it I was struck by some possibilities. I am talking about DeleteLocalInt which allows for the dynamic deletion of variables. Correct me if I'm wrong (I haven't tested it) but I think this opens the way to implementing dynamic pseudo-arrays and dynamic pseudo-linked lists (stacks, queues, trees, etc). Of course I bet the seasoned scripters on here have known this for ages.


 


@Proleric Thank you for explaining the underlying principles behind pseudo heartbeat code. So at its heart (no pun intended) it relies on recursion. No wonder I got a headache '<img'>. What surprises me more is how I could at the same time be so close and so far from getting this. 


 


Thanks guys


 


TR



               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Help with count down timer needed please
« Reply #9 on: October 07, 2014, 04:53:26 pm »


               

I'm really sorry for the complexity. Complexity comes from my lack of experience as a coder. But I had hoped that my comments and the structure will make it easy to modify.


 


The only script you should need to edit is puzzle_inc. It has a section called CONFIGURABLE with some constants you can tweak as needed. You should be able to find the functions in that script that you can customize too. There's a function that executes when its all completed. One that handles the display of the count and so forth.


 


As far as handling blueprints goes I set up the switching placeables (the one the player needs to use) as all having the same tag. This allows you to simply place them in the module and the script will be able to generate a count of how many toggles need to be toggled (I had initially stored them as a list but that seemed needless).


 


As you can also see, there is a master placeable that needs a different tag and a unique one. There should only be one of these. At present it is the thing speaking the count, but you can set that to the player if you want. This master placeable is key to the whole operation as all the code executes on it, and it stores the state of the puzzle.


 


There is also a placeable that can be used to reset the whole puzzle. I did this for testing purposes because the way I set this up, if the count reaches 0, then the whole thing gets stuck in a fail state, and the player can't interact any further with the switches. To start over you need to reset.


 


Also when the puzzle is successfully completed, the puzzle resets to start.


 


You can see all this behavior by playing with the demo.


 


If you have any questions, fire away. I'll answer as best I can.



               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Help with count down timer needed please
« Reply #10 on: October 07, 2014, 11:21:22 pm »


               

Don't worry about the complexity, any fault is at my end. Your comments are great. My problem is that I always have problems reading other peoples code. What I am going to do, and I hope you don't mind, is to rename some of the variables to ones I use myself. It's not for the purpose of taking ownership but rather just to enable me to understand it faster. Part of my trouble is that until I started this trap/trick/puzzle I hadn't written anything of any real size in any language for years. Anyway, once I have done that and had a play around for a while I should hopefully be able to use this. If not I will get back to you.


 


One of the things that should help me to understand the workings is to gradually go through and comment out (temporarily) anything I don't understand and see what happens. This should also help me later, if anything goes wrong, when I start to modify your code to fit the exact needs of my grand idea (sarcasm aimed at myself).


 


So thanks for all that you've done for me with this.


 


TR



               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Help with count down timer needed please
« Reply #11 on: October 07, 2014, 11:48:16 pm »


               

Change the code however you like. I gave it to you to use however you wish. BUT keep in mind that most of the code should just work for your needs unmodified. Thats part of why it looks so complicated. I made a framework with a few modifiable parts that should allow you to make it do whatever you want. You only likely have to edit the contents of a few of the functions to customize the behavior. The rest of it is just infrastructure.


 


But however it goes, I don't mind. Have at it.


 


edit--


here are the functions that you need to modify to make work with your module



void PuzzleCountRestore(object oPC, int nFirstTime=FALSE)

void PuzzleCountZero(object oPC)

void PuzzleCountDisplay(object oPC, int nPuzzleCount)

void PuzzleSolved(object oPC)

in each case there is a portion of the code which currently has text feedback. You likely want to change the text feedback or replace it with a VFX or an executescript call for something exciting to happen.


 


As is the code allows you to place any number of toggles you want as long as they all have the same tag as that identified here:


 




// use the same tag for all the toggle objects
const string PUZZLE_TOGGLE_OBJECT_TAG = "puzzle_toggle";

anyway… i'm just trying to help you access what I made. if you want to make more substantial changes thats fine with me.



               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Help with count down timer needed please
« Reply #12 on: October 08, 2014, 12:27:14 am »


               

Here's what I wrote up last night to do this. It's more of a template and may need some tweaking. Also it's not going to compile as is. You need to fill on some parts to make it work. The idea is to hook it into the on used handler you already have. Pseudo-heartbeats do not need to be done with their own script and ExecuteScript. You can do it with a function call as well. Basically it's the same as what Proleric outlined except I'm overloading the counter to tell if the HB has started or not yet.  Fwiw...


 


Edit: shortened link



               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Help with count down timer needed please
« Reply #13 on: October 08, 2014, 11:25:17 am »


               

Again thanks guys. BTW for anyone who gets paranoid about shortened links there is a site that should ease your worries. It's called LongURL and it works similar to google translate in that you paste the shortened link into a text box, push a button and it tells you the full url and how many redirects, it takes, from the original URL that you posted.


 


long_url.png


Just click the anywhere on the above picture to go there.


 


TR