— Intro and what I have now —
I’m new the whole coding thing. So I decided to make a simple “small town simulation” where every NPC has some sort of behavior which determines what said NPC should do on a moment to moment basis. So far it’s been pretty straightforward (I’ve just started) - NPC receives some data, processes it and does something appropriate.
There is also an in-game timer (handled in the FixedUpdate). Basically, every real life second = in-game minute.
When the player “goes to bed”, this timer is advanced by 8 in-game hours.
— Hypothetical situation —
There are two points: pointA and pontB.
There is a ‘cop’ in the pointA and he starts walking towards pointB.
There is a ‘bad guy’ in pointB and he starts walking towards pointA.
If they meet they try to shoot each other. And by accident they might shoot neutral NPCs around them.
I can make this happen in real time. I can leave pools of blood where folks have been shot, or I can leave bullet casings. Even if the player did not witness the event as it unfolded, he will be able to see solid consequences of the shootout.
And let’s say the whole event takes 3 in-game hours. Cool.
— The problem —
Let’s say that the player went to bed just before the cop and the bad guy started walking towards each other. This event should still happen. But I also have to advance in-game time by 8 hours in about 3 seconds.
So, how do I ensure that the event takes place when the player goes to bed? And there could be several of such events. And some might event start while the player is sleeping, but continue when he awakes.How, in general, would you make sure that whatever unpredictable things that might happen in the game’s future will indeed happen?
How would you approach that sort of a problem (which is waaaaaaaaaay out of my beginner’s league)?
If the player is not witness to the events happening how would they ever know that the “shoot out” was in the wrong place, or 3hrs in rather than exactly 4hrs 13minutes in…
They’re likely going to be more annoyed by having to wait why the “sleep” processes all the actions and events hyper accurately, simplicity would be key here. (Just not to the level of teleporting pods like in firaxis’ first xcom…)
I hear you, but it’s not about how I can avoid that problem (easiest way would be not to advance the time), but how to go about solving it if it’s super-duper necessary for the simulation I’m trying to make.
And I honestly do not mind minor disparity in results.
like, if I run this scenario several times and they all yield different but yet logical results - I’m cool. I do not mind if the first time cop wins without a scratch, the second time he wins but barely makes it to the hospital, and in the third simulation cop dies. It’s all cool to me at very least because accuracy is the element of randomness. What I do care about is that the world operates based on same the principles, regardless of the absence / presence of the player . And some version of that event should happen.
Get all “actors” that are active
Check for combos (cop + robber => shoot out event at random ish point and time)
If no combo pick something from a list of the actors’ “events”
Process some random events for flavour
An “actor” being anything that might do something. A spawner, a unit, whatever
I have no idea if I will be able to explain it clearly, but be certain that it works in my head just fine so with high dose of probability it should work when implemented correctly in real life as well:
-Start it simple, build a class for a cat.
-Make it a state machine with states like walking, sleeping, hunting eating, running and transitions between them (like when sleeping and hungry then start hunting etc)
(More on state machines here : Simple state machine example in C#? - Stack Overflow)
-Each state should have its properties like for instance walking should have a direction, time it takes for action etc.
-Now make it work, see Debug.Log showing what your cats doing all day without actually doing anything graphics related, just raw text telling you whats happening.
-After that you can start working on visual presentation of whats happening. When cat’s eating, then there’s a sphere representing cat that grows in size, when he’s running, the sphere moves fast etc. Of course this can be refined further up to a cat 3d model with bone system and animations doing evertyhing like a real thing.
-Now that you have a state machine and visual presentation, do a loop that runs for infinity and prints cat states as he lives and translates from one state to another. That would be equivalent of your ‘sleep loop’ - simulation still works but without affecting visual representation. After its done you make sure visual is updated to what happened in simulation.
Or go super lazy way, pick up probabilities of events to happen to specific people ‘by their occupation’ per hour.
Then as you simulate sleep just roll a random number and see if any of the events accured, if so, affect the character, woho!
Thanks. Let’s call it a “back up” option =)
I do not want to sound dismissive of your advice (if I were to make a proper game, I most probably would act along the lines if your thinking). It’s just that I set out to make an honest simulation, where actions (even of the NPCs) have consequences and affect the world. So I do not want “out of the blue” events to happen.
Here’s an invariant of the first example.
Player has studied the behavior of the 'bad guy" for a month. And every day, at the same-ish time the ‘bad guy’ goes from pointB to pointA. Let’s say that player bugged both the cop and the bad guy, and these bugs allow players to store last 24 hours of their respective movement.
If the player was to “look” at the bug data in real time, he’d see the dots meet, stay for a while at the same spot wiggling around each other and depending on the outcome move elsewhere. Player should see the similar result if he “sleeps out” the event.
Or, another hypothetical example.The player knows that NPC_A gets into his car at 7:00 am and arrives at his job around 7:45 am So if the player were to place a mine under the door, it should blow up approximately at 7:45.
Let’s say that the player shredded the wheels of the NPC_A’s car. That event would mean that NPC_A will be late for his job at least by 2 hours, postponing the explosion till 9:45.
Let’s also say the player stays at the motel right across the street from the NPC_A’s job and should wake up earlier if there is an explosion nearby. How do I make sure that the explosion happens more or less at the correct time?
I guess my question is not how to fake “passage of the 8 hours” but hot to actually advance the world 8 hours forwards but to do so in just 3 seconds. Or at least somehow approximate all the actions that will should have happened.
Smoke and mirrors is probably best. Remember, a game is basically an ego trip for the player. Nothing happens if significance without the players involvement. And nothing that doesn’t enhance the players experience is worth simulating.
If you do want to simulate it, you need to seperate your simulation from the game visualisation. You basically generate a list of events that happens each day. Use general probabilities to figure out the outcome. Then whenever the player gets close, turn on the in game visualisation.
if you want to process the 8 hours accurately without approximation you’re going to need to be able to handle AI and game state totally independent of any visualization or scene based components so you can pass game state data between the AI and game manager to work through those 8 hours at discrete intervals (say ever “minute” or whatever “tick” you like).
Cool, thanks, that sounds like a good place to start.
It looks like ‘state machines’ solution will allow NPCs to “teleport” in time between important bits. Won’t it effectively make them ghosts? I still struggle to see how it would allow them to interact?.. ((
Alright so you don’t want probabilistic lies and going for state machines.
If you have few of them humans being state machines you could add some form of distance checker that would check how far all humans are from each other and if they are close change state to something representing interaction.
So for instance when it is detected that two humans are near and in line of sight you could change their state from walking, to approach, once on approach and reach even smaller distance switch from approach to conversation.
I remember years ago @ studies we had to implement state machine animals, they would randomly walk around until found food, if food found they would eat some of it and regain their health. If low on food they would go back to nearest known food source (whch could be depleted by then). If no food at known food source they would remove it from their internal food source list.
If two creatures met they would exchange and update their known food sources and its locations.
Everynow and then each animal would loose one health, once no health was present they all would perish.
Was fun!