Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

What's the best way to organise your on_command{} handlers?

A topic by Garry Francis created Jun 11, 2020 Views: 102 Replies: 7
Viewing posts 1 to 4
Submitted

I know there's probably no ideal answer to this, but I was wondering what's the best way to organise your command handlers in the on_commands{} section. In the past, I organised them by room, then object in room, then commands for each object. The advantage of this is that all commands for a particular object are grouped together. For example:

: match "examine door" {
   : if (is_present "door") {
      // Do something
   }
}
: match "open door" {
   : if (is_present "door") {
      // Do something
   }
}
: match "close door" {
   : if (is_present "door") {
      // Do something
   }
}

Alternatively, you could do something like this:

: if (is_present "door) {
   : match "examine door" {
      // Do something
   }
   : match "open door" {
      // Do something
   }
   : match "close door" {
      // Do something
   }
}

I'm now wondering if it's better to organise them by verb, then statements for each object within the verb. For example:

: match "examine _" {
   : if (noun1_is "door" && is_present "door") {
      // Do something
   }
   : if (noun1_is "cupboard" && is present "cupboard") {
      // Do something
   }
}

What do others think?

Host

The third approach is neatest I think (personally speaking).

For 8-bit compatibility sake, this works, and was specifically coded to map to DAAD. You are permitted to have matches inside an outer match so long as every element of an outer match is also a match:

: match "examine _" {
   : match "_ door" {
      : if (is_present "door") {
         // Do something
      }
   }
   : match "_ cupboard" {
      : if (is_present "cupboard") {
         // Do something
      }
   }
}


POINT OF NOTE : There is an open issue (in the mysterious issue tracker) for being able to combine PRESENT, CARRIED, WORN, BESIDE, NOTWORN in match statements as a short form for the if statement within the match. This should eliminate the need for an inner if per item (in many  cases).

Submitted(+1)

I think, I'll start by managing the GET/DROP objects globally, and the others in locations.  Then, I'll see how I organize my code inside the Global and the Local.

on_command {
   : match "_ _"  {
      : if (is_at "room1") {
         : gosub "room1Sub" ;
         : done ;
      }
      : if (is_at "room2") {
         : gosub "room2Sub" ;
         : done ;
      }
      : if (is_at "room3") {
         : gosub "room3Sub" ;
         : done ;
      }   }
}

I organise mine as in Gary's first example as I just find it easier to find things in the all the reams of code that way. I found keeping track of things  a bit of a struggle at first and spent a lot of time scrolling up and down and trying to figure out what I'd done...but I'm a bit better at it now. I suppose some sort of GUI might come along at some point and make life easier?

Host (1 edit)

Are you using the search function?

Personally, I find it quite easy navigating with CONTROL + F, and searching for the concept I'm dealing with (e.g. river).

You can just keep tapping enter to cycle through matches, until you find the one (or ones) you are interested in.

I recently added a section to the document specifically to do with searching.

https://adventuron.io/documentation/#Searching

There is also the "Jump to section" functionality that is (somewhat hidden) behind the title of the text editor window.

Submitted

Can you please make the jump-to-section functionality work when there's an error? This is the one time that you really need it, but you can't use it.

Host

Noted.

Thanks for documenting  this (very useful). The 'jump to' stuff will help hugely - I hadn't realised that it was there. As it is, I do use the search to find most things - more often that not, I remember some distinct word that I used in a message in the section that I need to get back to and search on that.