All right.
I hope you're ready for some coding because there's quite a bit to get through.
And in fact, we probably won't even get to a point in this particular lecture where we can run anything.
But what we're going to do is start with the very basics and create some really basic classes that we
can build onto.
And that's going to be the world states, the agents and also the actions.
So let's go ahead and create those files.
So down in your project, let's create a C-sharp file.
I'm going to call it G agent.
Then we're going to create another one and we're going to call that one g action.
And another one.
Which we'll call G World.
And one more.
And we will call that world states.
Okay.
So let's open up world states and we'll edit that one first.
At the very top, I'm going to include just above the actual class itself a another little tiny class
and we're going to make it serialize able so that we can see it inside of the inspector.
So if system serialize visible and then we're going to have a public class world state.
And in that state what we're going to do is create a public string key and a public int value.
Now we're doing it so that you can actually edit these items in the inspector, because once we start
building the system, you'll want to drag and drop a lot of things around as much as possible.
Now the reason that we're creating a world state with a key value and a value is that we're going to
create a dictionary with these.
And the key itself is actually the world state.
And then I've also added in this value and the value is some kind of integer that you want associate
with the key.
Should you want to associate with the key down the track?
For the most part, we're not actually going to use that, but we're using a dictionary.
So we do need to have these two values in there.
But let's say that down the track you wanted to have a world state was like free cubicles.
And then you could have in here that there were five free cubicles or something and then you could test
it when you extend this system.
All right.
So that is our little class with the world state, and we will use it inside of other classes.
So let's go ahead and now do our world state here.
Now, first of all, it's not going to be a mono behavior.
So you can get rid of that and all of the other code that's inside of here.
First of all, we're going to create something to hold all of the states of the world.
So public dictionary.
String int.
And this will be called our states.
So let's create our constructor, and that will be a public world states.
And inside of here, we're just set up our dictionary.
So states equals new dictionary.
String int.
All right.
So that's all that we need in there.
Okay.
Now we're going to add a whole bunch of methods to help us use the dictionary.
So to add things to the dictionary and to modify the dictionary and also set the states that are in
the dictionary.
First of all, let's ask if the dictionary has a particular state in it.
So public bool has state and we will pass through the key.
And then we will return states.
Dot contains key.
Okay.
Nice and simple.
There, you'll start to see why we're using dictionaries for this, because they're just so easy to
manipulate and add stuff to and to look for things without having to write an awful lot of code.
All right.
The next one will be to add a state to our dictionary.
If it's not there, so avoid add state string key int value.
And then this is simply a states dot add and then a key and a value.
Nice and simple.
Next we're going to add in a modifier state.
So public void modifier state string key int value.
And in this case I'm going to put.
If states dot contains key.
Then obviously we've already added that state to our dictionary.
Now, if it's in there, what I want to do is actually increase its value by whatever we pass through
here, which means you could also take away by sending through like a minus one from here.
So let me put in here if states contains key states at that particular key plus equals value.
If the state at that key is less than or equal to zero, I'm actually going to get rid of it.
Now, that's because of the way I'm using this particular dictionary.
If you're not going to use that and you actually do want negative values in there, then that is entirely
up to you how you deal with this.
So what we're going to do for this as an example, is if we have a state like free cubicles and there's
five free cubicles, and then you send through another free cubicle one, then it will update the number
of cubicles to six that are available.
If a cubicle is not free anymore, so you could send through a value of free cubicle minus one.
It will actually take away the number of cubicles that are available for use.
Okay, so once we've done that, we're just going to go else.
States add.
Key value.
So if it doesn't exist and we're adding our first cubicle side cubicle one, it's going to add that
in there rather than incrementing it.
Now, we haven't written remove states and we'll get to that right now.
So public void, remove state string key.
And then in here if states dot contains key key.
Then we will state stop.
Remove the key.
Easy.
Especially if you spell it correctly.
All right.
So that's the modifying and the remove.
Now, let's have an actual set state if we need to not modify it.
As far as adding a value to it, we just want to set what its value is.
Public void set state.
Again, we want the string key and the int value.
And in here we're going to if states dot contains key again.
Then we can go states.
Key equals value.
Else we can add that state in so state.
Stop add key with the value.
All right.
So that's all of the sort of management methods that we need.
Finally, I'm going to create one that we can return all of the states or return the actual dictionary.
So public dictionary string.
Ain't.
Get states.
Return states.
So this will actually be used by the planner when it needs to get hold of all the states of the world
and all of the states of the belief.
All right.
So I've just noticed I've got one little problem up here with that space in there.
So let me get rid of that.
And the rest of the code is good to go so we can save that now that we've written our world states class.
All right.
So just quickly back into unity so I can open up our G world class.
And that is also not going to be a mono behavior.
But what we're going to do with it is create a sealed class.
And this will help us a little later when we start using cues in that to queue things up so that we
don't get any sort of conflicts in trying to access particular parts of this class, which really should
only be accessed one thing at a time.
Okay.
So first of all, we're going to create a private static read only g world instance, which equals a
new G world.
Now, I didn't mention this, but we're also going to make this into a singleton so that we can access
it pretty much everywhere.
And we only ever want one version of the game world environment states because there's only one game
world.
So we don't want to have copies of it with different values in it.
So private static world, states, world.
That's our dictionary holding all of their states.
Okay, so let's get rid of those starts and updates because we don't actually need them.
Then we will have our constructor.
So Static G world and in here we're going to create World Equals New World States, which is just a
new dictionary.
And that's all we need in there.
Okay, then let's create a private G world and set everything up so that we can actually grab this with
a singleton public.
Static.
G world instance.
And this will be a gift return instance.
Okay.
And then one last thing we'll put in here is a public world status for get world so that we can actually
return the status of the world.
Return world.
Okay.
So that's all we need currently in world.
We'll come back and create a few more methods and other dictionaries in here as we need them as we start
building the simulation.
Okay, so that's our basic world and our world states.