[Solved]Moving characters according to a day/night cycle with minutes

Started by Glenjamin, Sun 23/12/2018 20:07:46

Previous topic - Next topic

Glenjamin

Hey everyone.

My game has NPCs that walk around according to the time of day.

There are two time variables.  24 hours, and 20 minutes per hour. (I added minutes to have a greater density of actions as one an hour didn't give any room for complex walking)

After a timer is expired, the minutes increase. When the minutes reach 20 the hour increases and the minutes are reset.

Adding events for heach min/hour is done with if and else if statements in a script. While it seems a little messy it's actually not too hard to work with.

Here's a clip, the hour keeps increasing until 24:

Code: ags
 if (WorldTime == (1)){
   
   } else if (WorldTime == (2)){
               

   } else if (WorldTime == (3)){
               

   }


Within each hour, I then have checks for the minutes. This is where I add the events for the NPCs:

Code: ags

   } else if (WorldTime == (12)){
                  if (WorldMins == (0)){
                      if (player.Room == coscar.Room){
                      coscar.AddWaypoint(889, 107); 
                
                      }else{
                        coscar.x = 889;
                        coscar.y = 107;
                      }
                        
            
            }else if (WorldMins == (1)){
              if (player.Room == coscar.Room){
                       //coscar.AddWaypoint(801, 114); 
                       
                      }else{
                        coscar.x = 801;
                        coscar.y = 107;
                      }
            }//else if etc 


An issue I'm coming across is character's walk speeds seem to randomly fluctuate between AddWaypoint functions.  Sometimes they move at default speed (3), and other times they slow down to a crawl.

I measured the amount of space an npc could walk between time intervals before and it came out to about 53 (I guess pixels? Units?) a minute and 1056 units an hour. They're defiantly not moving at that speed.


This code IS in the repeatedly_execute function so I figured it might've had something to do with the addwaypoint function being called repeatedly. To counter this I tested a few variations of the following:
Also I wanted these events to be effected by blocking which is why I have both the minute timer and the actions in repeatedly_execute rather than late_repeatedly_execute.

Code: ags
} else if (WorldTime == (12)){
                  if (WorldMins == (0)){
                      if ((player.Room == coscar.Room) && (coscar.Moving == false)){
                      coscar.AddWaypoint(889, 107); 
                
                      }else{
                        coscar.x = 889;
                        coscar.y = 107;
                      }

            }


Instead of calling the function once this made coscar move even slower and flash constantly between being invisible and visible. I have genuinely no idea what this could be.

Any help is greatly appreciated, thanks.




Khris

There's no need to put those numbers in parentheses. Just do if (WorldTime == 1).

As for the slowdowns and flashing, you've already identified the problem: you're calling these commands 40 times per second. What you need to do is implement this in a way that makes sure the characters don't get sent to the same spot again and again.

I'd also consider using proper indentation, especially with all those nested blocks it'll make the code much readable and manageable.

Glenjamin

I figured it out.

Whenever I added another condition to the function, it factored in the else statement which moved the character if the player was outside the room.

The solution was to break it up.

So instead of this:

Code: ags
 else if (WorldTime == (12)){
                  if (WorldMins == (0)){
                      if( (player.Room == coscar.Room) && (coscar.X == 705)){
                      coscar.AddWaypoint(889, 107); 
                
                      }else{
                        coscar.x = 889;
                        coscar.y = 107;
                      }


Do this:

Code: ags
 else if (WorldTime == (12)){
                  if (WorldMins == (0)){
                       if (coscar.X == 705){             
                          if (player.Room == coscar.Room){
                                 coscar.AddWaypoint(889, 107); 
                
                                     }else{
                                      coscar.x = 889;
                                      coscar.y = 107;
                                    }
                                    }


I'll defiantly work on my indentation. This project is pretty big and should be as relentlessly clean and neat as possible.

Thanks

SMF spam blocked by CleanTalk