How to organize code into readable, that preforms many tasks

Hey developers! I am a scripter that has been scripting for ~3 years and I am still confused when it comes to organizing code. Currently I have a script with 1,200 lines, that I am trying to rewrite and organize into readable, reliable code. This 1,200 line script , while insane of a number, does preform a lot of actions. Here are some of the tasks it has:

  • Room generation & teleporting rooms to certain locations

  • Opening the doors and the proximity prompts

  • Knowning when everything is done and opening exit door

There are 2 types of rooms which the script has the burden of providing:

  • Wrong rooms. When the room is opened if it is a wrong room, the player must stand still for 5 seconds. Failure to do that will make them die. Monster disappears after 5 seconds.

  • Puzzle rooms. 5 unique puzzle rooms are required to be generated (There are more then 5 possible puzzles choices, 5 are required to be selected) . These puzzles are very different. Some include Simon Says Type game, Wordle Symbol type game, Picture order puzzle, Rotation puzzle, Bringing items to something etc.

This script is long, and hard to understand, which is why I am doing a rewrite. However I need tips to be more organized and a tad flexiable so I can understand what I am doing eaiser. Also this script uses a lot of remote events, and so its also connected to local scripts which are hard to understand. So how can I organize myself better in this script, as well as in connects to other scripts?

1 Like

Try to put code in module scripts, for different unique things.
Such as a script for proximity prompts on doors
then one for room generation, then one for puzzles, things like that.

I know thats not much help, but to be honest, I have been coding since the 80’s and I still have trouble with getting things organized and cleanly written.

2 Likes

A few things I do is…

  • If possible, divide a script up into more than one script and organize them by name and folders. I find global variables useful in allowing me to divide my code.
  • Utilize ModuleScripts if possible / if it can help (this may probably be your biggest reliever).
  • Use comments to organize / label / divide sections of your script so you can easily come back and tell what section handles what.
  • As for the remote events, I usually include something like a “request” variable (which would be a string value) being passed so that one remote event can handle a variety of tasks.
1 Like

I do have something like this already … The main issue I have is handling when certain things can be called. For example, in my “puzzles” most of them have a camera set up so the players pov is focused on the puzzle and not on their character. The issue with this is that I collect input at this time only, and need to disconnect and reconnect things. This is mostly the issue in being flexible and messy in the local scripts that I need to collect input after x, y happen, but once z happens then need to disconnect

I see both of you recommended this, but I fear it causing more of mess with cross commincating between those scripts and using remote / requiring modules together. An example is when a puzzle is done, lets say it goes to an exit script, which checks if they can leave. This causes more connects between the 2 which could be a bigger mess.

These 2 are ideas I did have for my rewrite. I’m newer to module scripts, but they do make things so much eaiser !

Code design isn’t a problem specific to game development. There are many resources you can look at to improve your code design.

You can do some googling yourself, a quick search on stack overflow brought up https://refactoring.guru

Also maybe read up on some programming patterns, one book I like is Game Programming Patterns by Robert Nystrom (Table of Contents · Game Programming Patterns, you’ll need some knowledge of C++), but there are a lot of resources to choose from.

Is the string a word or word sequence (with length depending on the words)? Sending natural language strings for classifying remote event firings can cause unnecessarily much network traffic.

This won’t be a problem at all if you are not firing the remotes often, but if you’d like to have the amount of data sent independent of the type of remote event firing, you could send the type in another way.

You could have a unique integer (in a table) for each of your remote firing type strings and write a centralized function for remote firing. This function would find the integer and turn it into a single character. This allows 256 (2^8) different remote firing types to be sent using just a single character there is a character for every byte (every sequence of 8 bits). Then, on the other side, this one character string could be converted back to an integer which would then be converted back to the natural language string.

You can use string.char to convert an integer to a one character string and string.byte to convert that back to the integer. The integer must be in the range [0, 255]. There are also characters whose size is more than 1 byte because there are more than 256 characters but I believe all characters that can be returned by string.char are 1 byte character because that feels logical. I’m not sure about this, though.

So basically, you would still be using natural language strings to identify the type of remote firing but the string would be compressed for sending and decompressed on the receiving side.