Below is a simple example of something I am using at present to get rid of nest if else statements in story flow.
Basically any sort of comparison you can do that has a depth to it you can put into a tuple and do a single comparison
In my case I also have some such as
location and occupancy list
Various MC attributes and values
story name and positions(integer) and so on. This means I can control several sets of stories at the same time all cleanly without a bunch of ugly ass code.
The nice parts about the method is you can write your functions/ scripts in other files for each event you want.
Then you just need to add them in when you start the game
You can even add in events in the middle of the game remove events you only want to run once or a select number of times.
You can run all the checks in an update function.
Using multiple dictionaries for each event type allows you to keep easier track of the what is going on.
Another example of removing nested if else
This is a leap year test. Have a look online at the examples they give.
Understanding the underlying aspect or nature of what you are coding can allow you to write better code.
I'm not saying all if else statements are bad. They should be used sparingly.
There is almost always a better solution to a nest if else statement.
If the code doesn't run much then its not big deal.
How many times do I test if the year is a leapyear not that often. Even when updating time and date I only check if the year changes.
Most of my date time functions used the mod (%) to handle updating. Math functions are far faster than doing if checks.
I was planning on finishing up something other stuff before posting this on here.
But I was helping someone else out today. It got me to thinking that maybe I should go ahead and post it.
There are a number of games on this site that could easily make use of it and seriously clean stuff up and boost performance.
I won't list any names. I'm not hear to embarrass anyone.
I figured this solution was relatively simple and easy to follow.
It sure as hell makes for easier code to look at and read. Thus easier to trouble shoot and catch mistakes.
Basically any sort of comparison you can do that has a depth to it you can put into a tuple and do a single comparison
In my case I also have some such as
location and occupancy list
Various MC attributes and values
story name and positions(integer) and so on. This means I can control several sets of stories at the same time all cleanly without a bunch of ugly ass code.
You don't have permission to view the spoiler content.
Log in or register now.
Then you just need to add them in when you start the game
You can even add in events in the middle of the game remove events you only want to run once or a select number of times.
You can run all the checks in an update function.
Using multiple dictionaries for each event type allows you to keep easier track of the what is going on.
Another example of removing nested if else
This is a leap year test. Have a look online at the examples they give.
Python:
def leapTest(year):
p = not year%4
q = not year%100
r = not year%400
if not(q!=r) and p:
return 1
return 0
I'm not saying all if else statements are bad. They should be used sparingly.
There is almost always a better solution to a nest if else statement.
If the code doesn't run much then its not big deal.
How many times do I test if the year is a leapyear not that often. Even when updating time and date I only check if the year changes.
Most of my date time functions used the mod (%) to handle updating. Math functions are far faster than doing if checks.
I was planning on finishing up something other stuff before posting this on here.
But I was helping someone else out today. It got me to thinking that maybe I should go ahead and post it.
There are a number of games on this site that could easily make use of it and seriously clean stuff up and boost performance.
I won't list any names. I'm not hear to embarrass anyone.
I figured this solution was relatively simple and easy to follow.
It sure as hell makes for easier code to look at and read. Thus easier to trouble shoot and catch mistakes.