(coding) How to make user Interactive functions in a row?

Discuss any general programming issues here
Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: (coding) How to make user Interactive functions in a row

Post by Bugala » Tue Jul 11, 2017 1:35 pm

I just realised one more benefit worth mentioning about Observer pattern, although this can actually be used by Gamestates too.

For this is a way to avoid having any need to change the original functions code.

For so far i have thought if i have a:

Code: Select all

function IDontWantToChange()
   debugprint("This is the function I dont want to change")
endfunction
Then if i would want to use gamestate or observer, I would need to add at end of function either:

Code: Select all

Observer:Inform("Message to tell to observers")
or

Code: Select all

gamestate = #NEXT_PLAYER_TURN
But actually there is better way to do this, i can leave the original function as it is, and instead make a new function which calls that original function and then sends the necessary message or makes the gamestate change, ie.

Code: Select all

Function MyMessageSendingFunction()
   IDontWantToChange()
   Observer:Inform("MyMessageSendingFunction sending message here")
EndFunction
or even have several of them for different situations:

Code: Select all

function FirstCase()
   IDontWantToChange()
   gamestate = #FIRST_PLAYER_TURN
endfunction

Function SecondCase()
   IDontWantToChange
   gamestate = #SECOND_PLAYER_TURN
endfunction
Now depending upon situation, i can call FirstCase() or SecondCase()

Post Reply