Sunday, March 22, 2015

Introducing how event are written 3: Expressions and parameters

Hello,

this time we are going to write another simple events where I'll introduce the expressions and parameters.

Let's say I want to have a more dinamic event (relatively to the previous two we have seen). For example I want that the player need to pay a fee to enter a building.

We are looking for an event written like this:

If player doesn't have enough money
     Kick him out
else
     play the main event

To have something like this we need 2 things that we haven't still seen:

  • An if event step
  • A way to check player money
So let's start with the former and write a basic event


    <SetText>Bouncer: Halt!</SetText>
    <SetText>Bouncer: You need to pay the standard fee to enter.</SetText>
    <SetText>Bouncer: 50!$$</SetText>

    <If Expression="true">
        <SetText>!$player: I think I left my money in the other pants.</SetText>
        <SetText>The bouncer kick your butt making you fly outside of the place.</SetText>
        <End/>
    </If>

    <SetText>!$player: Here you are sir.</SetText>
    <AddMoney>
        <Reason>Expensive place entrance fee</Reason>
        <Amount>-50</Amount>
    </AddMoney>

    <!--The rest of the event here-->

As you can see the If event step is pretty simple. It has a Expression attribute where you write an expression that will be evaluated at runtime.
If the expression return true the event steps inside the If are played otherwise not.
In this case the expression is "true" which, DUN DUN DUN, always evaluate to true.
So in our basic event we always get kicked out.

Another thing to notice is the <End/> event step, which, as its name suggest, will end the event.

Sidenote: At the moment I'm not sure there will ever be an If Else event step because I'm not sure I             can achieve a clean way to write it in XML, but anyway an if else behaviour can be simulated             using a Switch that has both true and false cases.


Okay, having a bouncer that will always kick your butt, is not that great. So, how can we check player money?
Other than using normal mathematical construct the expression can evaluate information called parameters. Parameters are specified inside [] brackets.
In our case we need the [Money] parameter. This return a number that represent the current player money.

So, knowing this we can now write:

    <SetText>Bouncer: Halt!</SetText>
    <SetText>Bouncer: You need to pay the standard fee to enter.</SetText>
    <SetText>Bouncer: 50!$$</SetText>

    <If Expression="[Money] &lt; 50">
        <SetText>You only have !$expr{[Money]}</SetText>
        <SetText>!$player: I think I left my money in the other pants.</SetText>
        <SetText>The bouncer kick your butt making you fly outside of the place.</SetText>
        <End/>
    </If>

    <SetText>!$player: Here you are sir.</SetText>
    <AddMoney>
        <Reason>Expensive place entrance fee</Reason>
        <Amount>-50</Amount>
    </AddMoney>

    <!--The rest of the event here-->

We have, unfortunately, to use the &lt; entity instead of < since in XML you can't write the < character blindly.
Anyway as you can see our expression is now [Money] < 50 and so we will enter in that if only if player money are less than 50.
Another interesting thing to notice is that you can use expression in text using !$expr{} writing your expression inside the brackets.
This way we can give feedback on how many money does the player have.

Now our event will work correctly. If we have less than 50$ we will get kicked out, we will enjoy the full event if we have 50 or more.


Sidenote: The expression used by the game are evaluated using NCalc. On their site you can find the         documentation, the sintax and some example.

When I'll make the first release the game will contain the documentation where all the available parameters are described.

Okay, that's it for this time.
Bye bye!





No comments:

Post a Comment