oceanarts wrote: ↑Thu Jul 10, 2025 1:36 am
Sorry for my constant nagging...
In my quest to minimize global variables, I can't for the life of me figure out how to pass values between functions.
If for instance I want one function to pick a set of random words, how do I pass those words to a local table in a different function to print them in a specific order? I just get errors, or a blank display whatever I do.
Thanks, again for your time.
This question is not answered easily, but goes to the core of coding, which is in how to make things the best way.
If you look at any big AAA game, you can look at some part of it, and figure out that "hey, I could do that". To take an example, you see in some 3D shooter enemies moving for example normally on ground and then there are ladders they can use to climb up. It is not that hard to figure out how that would work.
Or you see a car accelerating, and once again you can quite easily see how it would be coded that pushing button A, speed increases, puch RIGHT button, it steers right etc.
It is not hard to figure out how to make any small piece in even bigger games, how you could get them done. Problem is, when you try to get them all done in that same program, for then it becomes a mess.
And this is why we have these different ways to do things, it is not so much to make things possible to do, since even beginning coder can get done pretty much anything as long as he has time, it is just a question of what is the better way to get it done, and way to get it done in shorter time, etc.
Like if you want to print out "Hello World" 100 times, you can do that by writing Print("Hello World") 100 times, but it makes much more sense to have:
For n = 1 to 100
print("Hello World")
next n
Also, in case you wish to change it into "Hello Mom!" instead, in first case you have to change 100 lines, in second case, only once.
Problem is not in getting single thing to work, problem is when you have lots of things that all need to work together.
At this point problems start stacking up. Like if you want to change something, how hard is it to do, or do you even understand your own code anymore.
More stuff there is, more conflicts there start coming too.
Like lets say you are making shooting part for your player character.
Well, you could simply have variable called SHOOTINGSAMPLE
Then later you decide to start making enemies, and then you start wondering, what do I call enemies shooting sounds, cant use SHOOTINGSAMPLE anymore, or maybe you do, and then you are accidentally changing your player characters SHOOTINGSAMPLE as well.
Because this kind of conflicts are constantly happening when your program gets bigger, this is why all these systems to for example avoid using Global Variables have been made, since if you dont have a single Global Variable, then you dont have any conflicting Variables either, which is one less problem.
Then there comes things like how do you make sure something you didnt intend to be happening / running, isnot happening / running. Or if you have a bug, it might be hard to find it, if code keeps going through a dungeon of loops, since it will be hard to backtrace the source.
For these reasons, less stuff functions change, the better.
But there are different kind of approaches to these problems, and even different coding languages in themselves has been made with the idea of approaching these problems through certain ways.
But, to give you bit of idea on ways to approach the problem of sending stuff to functions:
Code: Select all
Player = {name = "oceanarts"}
Function Get_Player()
Return(Player)
EndFunction
Function PrintPlayersName(PlayerObject)
Print(PlayerObject.Name)
EndFunction
Repeat
PlayerToUse = Get_Player()
PrintPlayersName(PlayerToUse)
Forever
Although this example doesnt make much sense, the idea is to demonstrate the idea of where functions dont change anything. As in, you use Get_Player() to get the player, and then throw that result to PrintPlayersName-function which simply prints its name.
Another bit similar, with the idea that this time the function does change something:
Code: Select all
Player = {Y = 100}
Enemy = {Y = 200}
Function GravityEffect(object)
object.y = object.y + 1
endfunction
Function PrintY(object)
print(object.y)
EndFunction
Repeat
GravityEffect(Player)
GravityEffect(Enemy)
PrintY(Player)
PrintY(Enemy)
Forever
Now we have a gravity which affects any object thrown into it, be it player or enemy, or anything else too, by increasing Y value by 1 each time.
Basically to do your random words from another function, it would work like this:
Code: Select all
Function GetRandomWords()
Words = DoStuffToDetermineRandomWordsToBeReturned
Return(Words)
EndFunction
Function PrintWords(Words)
foreach(Words, Function(ID, Content)
print Content
EndFunction)
EndFunction
SetOfWords = GetRandomWords()
PrintWords(SetOfWords)
(
Function SetOfWords decides what words to use, and RETURNS them to the SetOfWords-variable, and then I throw that variable to PrintWords() - function.