Tables

Find quick help here to get you started with Hollywood
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Tables

Post by oceanarts »

Nice.
Development System : Imac G5, MorphOs 3.19
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Tables

Post by oceanarts »

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.
Development System : Imac G5, MorphOs 3.19
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Tables

Post by jPV »

oceanarts wrote: Thu Jul 10, 2025 1:36 am In my quest to minimize global variables, I can't for the life of me figure out how to pass values between functions.
Have you read this one? http://www.hollywood-mal.com/docs/html/ ... ions_.html
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.
Depends how you want to pass the words to a function. Every word as a single argument or as a table containing the words? Fixed amount of words? But basically function parameters become as local variables inside the function, so whatever you pass there are actually local variables inside the function already.

But remember, if you pass a table for a function (or assign a table to another variable otherwise), you only get a reference table on the new variable. It means that all table variables are pointing to the same data in memory, and if you modify your local table, the original passed one gets modified too. If you just read table values in a function, it doesn't matter, but if you'd need to modify the table contents only locally (not affecting the original table), you should make a copy of it with the CopyTable() function.

BTW. I just made a global variable free program just for fun :P But I use MUI (GUI) features to store some global-like data (which still isn't global) :)
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Tables

Post by oceanarts »

jPV wrote: Thu Jul 10, 2025 7:40 am BTW. I just made a global variable free program just for fun :P But I use MUI (GUI) features to store some global-like data (which still isn't global) :)
That's awesome!

Just as recent catchpas has left me wondering about the nature of reality, some of these concepts seem equally confusing and unsolvable. But thanks to your link I got it working. Just need to take the time to learn things step by step I believe, and not expect to implement a complete idea right away.

Thanks for your help! :)
Development System : Imac G5, MorphOs 3.19
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Tables

Post by jPV »

Yeah, each programming language has its own characteristics and you should learn basics before trying to do anything. When I started with Hollywood a few years ago, I knew that I must read the first 15 chapters from the documentation before even considering to code anything. I had quite strong knowledge about scripting languages beforehand, but when picking up a new language, you still have to learn how it does things. For instance, Lua/Hollywood uses complete different approach for Local/Global variables than, for example, Python. So, it's very important to learn about these differences, and even without them, it doesn't hurt to recollect the information.

And few lines of your own testing while reading doesn't hurt either, like testing simple functions/loops/blocks and checking if the variables will end up global or local etc.. after that you're more ready for actual programmming and you don't have to find out everything in the hard way later, it's much slower to find quirks in your programs later if you have been making systematic mistakes all the time. And if you're beginner on coding generally, it's important to learn in small pieces to make sure that you understand what they do. After all, these small pieces of code are the basis for everything.

When you take time once, then you'll at least know what information there is and whereabouts to find it again when/if you don't learn it with the first try.
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Re: Tables

Post by Bugala »

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.
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Tables

Post by oceanarts »

Thank you, Bugala. That's valuable information.
I hope to build a simple conversation system for my game, I fear it will be quite the task. :lol:
Development System : Imac G5, MorphOs 3.19
Post Reply