Not quite sure whats going on with state.GetStateStory()

Hi there

Why do we have to assign state to startingState first THEN write textComponent.text = state.GetStateStory()…? -

The ‘state’ in front of GetStateStory() will have the value of startingState. Does this mean that we are passing this value to the function…for it then to return it back??

Hi Scott,

The GetStateStory() returns a string. It gets the data from its own instance. And this string gets assigned to textComponent.text.

Is this what you wanted to know?

Thanks for the answer - If it gets the data from it’s own instance then why have we assigned ‘state’ to ‘starting state’ (which contains our data for the first state) then placed ‘state’ in front of GetStateStory as state.GetStateStory()… It’s this precise piece of syntax that I am struggling to comprehend.

Why state (DOT) GetStateStory () ???

state has been assigned all the data from the first state…

So we are essentially saying

(Once upon a time…blah blah) (DOT) GetStateStory ()

??

startingState gets assigned to state, not the other way round. Why the startingState variable? Because we want to restart our game at some point. The computer does not know what our first start is, and it does not remember anything unless we assign something to a persistent variable.

The variables startingState and state hold a reference to a State object. You assign something to state according to your logic. Unity does not care which State object is referenced. It does not read any stories, only humans do.

(Once upon a time…blah blah) (DOT) GetStateStory ()

Assuming you assign your A1 object to state. state.GetStateStory() will return the story stored in the A1 object. When you assign B192 to state, the same line of code, state.GetStateStory(), will return the story stored in the B192 object. And so on.

And that’s modern programming. You do not hard-code anything but write general code you can feed with data, and that data gets processed in accordance with your predefined rules.

Thanks a lot for the help and guidance. :slight_smile:

You’re welcome. :slight_smile:


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms