0

I am building a text-based adventure game as follows: I have 2 models Node and Message, in Node will contain:

  • 1 list of messages
  • 1 list of branching conditions
  • 1 list of child nodes My idea is to use Listview to print to the screen and when the user interacts with the conditions the data in that listview will be updated accordingly. The problem I have here is that if I use If - else it will take a long time to process all the cases if the plot is long. And the nodes are nested in a tree model, so it's quite complicated. Let me know the best solution to implement it. Thank you! enter image description here
An Tran
  • 119
  • 9

2 Answers2

0

For the presentation I would not care about any if else statement. I would completely separate the story logic from the presentation. Thus, you could have a field in your UI which is always holds the latest message and you could always show that to the UI. Or a list of all previous messages and you always show the last element of the list.

Additionally, I would write a story manager class which handles the different routings of the story tree and updates the field in your UI which holds the latest message.

tmaihoff
  • 2,867
  • 1
  • 18
  • 40
  • For the interface I use a list containing all previous messages and show the new element when there is a change. The problem I'm having is dealing with logic so that it can be easy to add new elements to that list. – An Tran Jul 22 '22 at 08:45
  • I see that your `Node` has a list of other nodes as `children`. These are the ones that might show up next? Maybe you can have one location where you setup all your nodes and chain them together via the `children` property. Then everything is setup. Then you need a central place where your currently active node and maybe also the node history is saved. The UI can always show the latest one. – tmaihoff Jul 22 '22 at 08:49
  • That's right, I use children as a list of next nodes. – An Tran Jul 22 '22 at 08:56
  • Can you tell in more detail? What should I do? Do I have to modify my model? I would like to know a more optimal way than mine. – An Tran Jul 22 '22 at 08:59
  • I must go afk now, but I try to come back later the day. I think your model is fine because it allows chaining. – tmaihoff Jul 22 '22 at 09:15
0

There's no need to use any if-else if your condition points to the specific id of the next message and next conditions from two big lists with all messages and all conditions.

So, my humble solution is to have a list of all Messages and a list of all Conditions. Each Message has an id, and each Condition has an id. So the click on condition will invoke loading of the specific Message.id and several Condition.id. Condition model looking like:

class Condition {
final String id;
final String nextMessage; // here will be an id of the next message to display
final List<String> nextConditions; // and then you display next conditions

So in reality each condition will look like:

Condition(id: 'A1', nextMessage: 'B', nextConditions: ['B1','B2','B3']);

The architecture management of lists you can always improve and maybe come up with some clever ids so it is easier to understand which lead to which.

Bohdan
  • 128
  • 6