Beginner Coders tutorial to Hollywood

The place for any Hollywood tutorials
Post Reply
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Beginner Coders tutorial to Hollywood

Post by Bugala »

As Yasu was asking for Hollywood tutorial for someone without any progarmming background, i decided to give it a try at least. This might not be best possible, since I am doign this half hearted, and I might stop at any moment. But At least I believe this will help something. I will also not be thorough in everything.

I suppose that you know how to use text editor, and suppose you know how to write there piece of program and then make it run.


Lesson 1: Flow basics.

Every program is executed, line by line. And from top to bottom.

Hence:

Code: Select all

1. debugprint("My name is")
2. debugpring("Hollywood")
3. debugprint("im a programming language")
Would print out:
My name is
Hollywood
im a programming language

as it is executed line 1 first, then second and last the third line.


I will be at start using this Debugpring() command, since that way we dont need to open screens or anything.


1.1 - Variables and Debugprint() command.

variables are plaves where you can store information.

you are free to name your variables into anything you like, as long as they dont happen to be named same as some Hollywood command is.

we could for example have variable "my_name"

we would make this my_name variable, but putting some info inside it:

Code: Select all

my_name = "hollywood"
Now this "my_name" variable holds inside it text "hollywood". Notice that everything i write inside " and " will be stored into that my_name variable.

You could think these variables as envelopes, where you put notes inside. When you need that info, you open the envelope where it reads on top "my_name" and you can read note inside it that reads "hollywood". This is good when you reach certain state of dementia that you can store all kinds of important ingo like your name inside these envelopes.


By default, Debugprint command prints out everything that you write inside " and ", they will be displayed exactly as you wrote them.

However, we have anotehr option, we dont necessary have to put everything inside " and ", but we can also put stuff outside of it, which Hollywood will treat bit differently.

Lets take example:

Code: Select all

1. my_name = "hollywood"
2. debugprint("My name is")
3. debugpringt(my_name)
4. debugprint("im a programming language")
What happens now is, that it still prints out the very same thing as before:
My name is
Hollywood
im a programming language

But how it happened this time is biot different.

Notice in line 3, I am not using " and " at all inside those ( and ). But instead, I am simply writing there "my_name". However, as you can see, isntead of reading
My name is
my_name
im a programming language

It actually reads Hollywood in that "my_name" place.

This is because when i wrote that my_name, Hollywood instead oepened up the envelope that read "my_name" on top of it, and wrote whatever was reading inside that note inside that envelope. Which in this case was "Hollywood".


Now, to show you about how the program flow works, I am showing one example again:

Code: Select all


1. debugprint("My name is")
2. debugpringt(my_name)
3. debugprint("im a programming language")
4. my_name = "hollywood"
It is easy for human to understand that idea is that this would read the same way again as before, and beginner might even think so, but in reality, it will print out:
My name is
0
im a programming language

Reason for this is, that since program flow is from top to bottom, you havent put any note inside that envelope until line 4, since line 2 is alreaduy telling to open the envelope, it will result in Hollywood displaying 0 instead of what you meant, since if there is no value assigned to one of the envelopes, then Hollywood just treats it as 0.


There is also another way to use Debugprint command, you can combine variables and stuff between " and ".

For example:

Code: Select all

1. my_name = "programmer"
2. debugprint("My name is")
3. debugpringt("hollywood and "..my_name)
4. debugprint("im a programming language")
This would now result in:
My name is
hollywood and programmer
im a programming language



And this can be extended to even more complex systems like:

Code: Select all

1. my_firstname = "Andreas"
2. my_lastname = "Falkenhahn"
3. my_approval = "Wife"
2. debugprint("My name is")
3. debugpringt("hollywood")
4. debugprint("and i was made by"...my_firstname.." "..my_lastname".." with approval from "..my_approval)

Last line would now print out:
and i was made by Andreas Falkenhahn with approval from wife


Just notice that the idea is that everytime you want to combine variable and stuff between " and ", you need to use two dots ".." to separate them from each other.



1.3 more about variables.

Variables can have other stuff too inside them, than just text. Another typical usage for variables is having numbers inside them.

For example we could have:
my_weight = 200

Since we now have a number inside that variable, we can use it for mathematical operations.

For example:

Code: Select all

my_weight=200
your_weight=300
our_totalweight=my_weight + your_weight
debugprint(our_totalweight.." kg")
This would result in text being displayed that would say:
500 kg


you can aslo use mathematical calculations when assigning variables, even mixing variables and mathematical operations

example:

Code: Select all

mysalary=1000
mymoneyatendofmonth = mysalary - 100 - 50 - 30 - 400 - 400 - 200
salaryincreasepercentage=10
mynewsalary=mysalary * (1 + ( salaryincreasepercentage/100 ) )

debugprint("my salary is:"..mysalary.." Eur   what is left of it each month is:"..mymoneyatendofmonth.." Eur    Thats why i needed a rase, and got "..salaryincreasepercentage.." percent increase to my new salary, and hence my new salary is: "..mynewsalary.." Eur.")
and it would print out:
my salary is: 1000 Eur what is left of it each month is: -180 Eur Thats why i needed a rase, and got 10 percent increase to my new salary, and hence my new salary is: 1100 Eur.


One of the most common usafe for variables is following:

my_variable=5
my_variable=my_variable+1

which means that now the my_variable contains number 6 inside it.

Another common usage is to make:

my_variable=1-my_variable

This means, that my_variable will always be either 1 or 0.
Very useful when you need to go between two different states continuosly in your program.


Next time I will go a bit about how to jump around in progam. since naturally, if only thing you can do is go from top to bottom, but never be able to jump back to top or middle or somewhere, there isnt much use from program.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Beginner Coders tutorial to Hollywood

Post by Bugala »

Lesson 2: Functions

2.1 Principle of Functions place in code.


First I am going to tell a bit about how Basic -type languages usually work, since it is easier to understand through that way how you do the same in C-type languages (which Hollywood belongs to).

In Basic languages, at least in those old ones I have used (MSX Basic, Amiga Basic, Amos, Blitz Basic...), idea was that you were able to put labels to the code, and then at later point, you could jump to that label point using command.

As example: (this is not hollywood code)

Code: Select all

1. label BEGINNING:
2. Write "I will not write this text again"
3. goto BEGINNING
This would result in following:

I will not write this text again
I will not write this text again
I will not write this text again
I will not write this text again
I will not write this text again
...

all the way until user interrupts the program.


However, when program came bigger, there was better way to do this, since usually you only wanted to jump to that other part of program only for a while, and then later come back to continue the execution of program from the point where it made that jump.

This would happen using command like Gosub.

As example:

Code: Select all

1. label BEGINNING:
2. print "I am beginning"
3. gosub SUBSECTION
4. END
5.
6. label SUBSECTION:
7. print "I am subsection"
8. RETURN
What woudl happen now is, that first the first two lines of code would be executed normally, printing to screen "I am beginning".
After this, it would on line 3, jump to line 6 and then execute line 7 which would print out "I am subsection"
After this it would go t o line 8, which would tell the program to RETURN to the spot where it jumped from, meaning it goes back to line 3, and executes line 4, which would end the program.



In Hollywood, we have so called Functions. These Functions work principly similar way, as GOSUB command works in basic.

To make a function you need to do it the following way:

Code: Select all

FUNCTION my_function()
debugprint("I am my_function")
ENDFUNCTION

When you write FUNCTION, Hollywood knows, that now it is supposed to create a new function. Right after this FUNCTION command, comes the name of the function, which can once again be anything as long as it doesnt iterfere with any existing Hollywood commands, for that reason it is good to add something to the beginning of name, as example my, like my_ownfunction, my_drawline, since this will prevent you with colliding even with future commands. Actaully, Andreas have given more specific instruction. If you want to be sure not to collide with any current or future command, you may use p_ at start, as example p_function.

notice that there are also ( and ) at end of the name. You ahve to add these to the end of the name as well. Inside these two, can be put some more options actually, but I will tell about them at later point. Right now, all you need to know is, that to create function, first write FUNCTION, then the name of the function, and at end of your functions name, put "()".

After that you make the actual lines of code that are supposed to be executed in this function, and at end you put a line "ENDFUNCTION", this will tell Hollywood that this is the end of the function, and you can at this point think it to be same as that BASIC-examples RETURN command.


Example:

Code: Select all

1. FUNCTION p_commontext()
2. debugprint("This text is so common to be displayed, i decided to make it as a function")
3. ENDFUNCTION
4.
5. debugprint("Beginning of program")
6. p_commontext()
7. debugprint("Middle of program")
8. p_commontext()
9. debugprint("End of program")
10. end
Program would print out following:

Beginning of program
This text is so common to be displayed, i decided to make it as a function
Middle of program
This text is so common to be displayed, i decided to make it as a function
End of program


What happens each time line reads as p_commontext(), is that it jumps to that named fuction, and does what that function does, in this case, prints out "This text is so common to be displayed, i decided to make it as a function" and then returns to continue the program from teh place where the function was called from.

By other words, each time a function is called, you could simply Copy-paste everything inside the function, making the code look like:

Code: Select all

1. debugprint("Beginning of program")
2. debugprint("This text is so common to be displayed, i decided to make it as a function")
3. debugprint("Middle of program")
4. debugprint("This text is so common to be displayed, i decided to make it as a function")
5. debugprint("End of program")
6. end


Notice one thing when making functions. Functions must be introduced before you are calling them. Hence if you wold instead make the previous program this way:

Code: Select all

1. debugprint("Beginning of program")
2. p_commontext()
3. debugprint("Middle of program")
4. p_commontext()
5. debugprint("End of program")
6. end
7.
8. FUNCTION p_commontext()
9. debugprint("This text is so common to be displayed, i decided to make it as a function")
10. ENDFUNCTION
If you would run this program, it would report an error at line 2, when you are trying to call function p_commontext() since program havent executed the function forming part yet, and hence doesnt know such a thing exists.



2.2 Sending information to function


Now, as i mentioned, you can put something between ( and ), this is the stuff you can use to send that function.

This works the following way: (I wont use line numbering any more from this point on)

Code: Select all

FUNCTION p_myprintfunction(p_text)
debugprint(p_text)
ENDFUNCTION

p_programsname="Hollywood"
p_programmersname="Andreas Falkenhahn"
p_mypritnfunction("Hello World!")
p_myprintfunction(p_programsname)
p_myprintfunction(p_author)
This would result in following:
Hello World!
Hollywood
Andreas Falkenhahn


What happens in code level is, that first I am making to variables (p_programsname and p_author) And I am storing some text inside each of those variables.

Now when I call the p_muprintfunction the first time, I am sending text "Hello World!" with it. When we arrive to the FUNCTION, variable p_text, will have inside its envelope, what ever i sent there, in this case this text "Hello World!".

The second and third time I am calling this function, I am not sending text, but variable. Since these variables I am sending, contain text inside them, then in practice, p_text will store the same text, as i had inside these variables i used to send them from.


To have more useful approach to using functions, I will now send two different info to following example function, as well as make it return something back.

Code: Select all

FUNCTION p_addtwonumbers(p_a, p_b)
p_total = p_a + p_b
return (p_total)
ENDFUNCTION

firstnumber = 2
secondnumber = 3
p_myresult = p_addtwonumbers(firstnumber, secondnumber)
debupgrint("Result is: "..p_myresult)
If this program be executed, it would result in:
Result is: 5

Logic with this program is following. I am telling the function to expect two different variables to be coming. First variable will be stored in p_a variable, second variable will be stored in p_b variable.

Then in the function, these two numbers will added together in simple 1 plus 1 manner, and result will be stored to p_total.

After this comes the point where it tells to RETURN (p_total). This tells the program to stop executing the function, and instead return to the place where this function was called from. But in addition, it is also telling to return the variable p_total with it when it goes there.

Since when you called that function, you used p_myresult = p_addtwonumbers(firstnumber, secondnumber), this tells the program to cathc the returmed variable to p_myresult variable, which results in p_myresult having number 5 stored inside it.

When you now after it tell it to debugprint this p_myresult variable, it will print out number 5, since that is what is inside this p_myresult variable at that point.

Next time I will tell you about loops, which are the essential in making programs continuous. Right now I have only shown you how to make programs that will sooner or later end.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Beginner Coders tutorial to Hollywood

Post by Bugala »

Lesson 3: IFs and LOOPs

There are many ways to do loops, but most of them require first understanding IF, which is the core of any programming language.

IF is very simple, it is exactly that, IF something, THEN something.

example:

Code: Select all

a=1

IF a=1 THEN a=0

debugprint(a)
would print out:
0


Because in first line "a" is set to store value 1
then at IF point, it is checking, if "a" variable has number 1 stored in it. Since it has number 1 stored in it, it will THEN do so, that variable "a" will store number 0 inside it.
when after this debugprint is done, it will print out the current number stored inside variable "a", which is now 0.

If you change the first line to a=2, it will print "2" instead of "0", since the IF condition wouldnt be true then, and hence the THEN part wouldnt be executed.


Many times, you want to execute several lines if some condition applies, in this case it works out the following way:

Code: Select all

a=1
IF a=1 
   a=0
   debugprint(a)
ENDIF
Idea is, that now I am not having THEN command at all. You can only use THEN command, when you are going to be doing only one thing.
But in this case, there are two different things happening if IF condition applies.

It will first of all change variable "a"s stored number into 0.
After that, it will also debugprint variable "a"s stored info (in this case - a number)

System goes so, that first you use IF, after that you tell the condition, for example a=1.

After that you write everything you want to happen starting from next line on, in case that condition applies, after which you use "ENDIF" in its own line to let Hollywood know, that your IF part ends here.


You can also have multiple IF conditions going inside IFs:

Code: Select all

IF a=0
   IF b=0
       b=1
    ENDIF /* this ENDIF ends the if b=o IF
ENDIF /* and this ENDIF ends the if a=0 IF 


There is one more thing about IF condition. You can also have ELSE and ELSIF:

Code: Select all

IF a=0
   a=10
ELESIF a=1
   a=20
ELSEIF a=2
   a=30
ELSE
   a=0
ENDIF
This works so, that first you make normal IF condition. After that you put all the liens you wish to be executed in case the condition is true.

Instead of using ENDIF to end this IF condition, you will be putting ELSEIF, which means, that there is goign to be another option as well, in this case, in case a is not 0, but it is 1.

Then you similarly write all the lines of code you wish to be executed in this case, and after that, if you wish more condition check to happen, you can use another and another ELSEIF check.

OR, in case you have already used all the specific conditions, you can then either use ENDIF, to end this check, or, you can add ELSE, which means that in case none of the IF or ELSEIF conditions apply, then these lines in ELSE part will be executed.

In this case, if a is for example 3 instead of 0, 1 or 2.

Then finally you end it with ENDIF.


Now by understanding IF, you can start creating LOOPs to code.

There are couple of different ways to make LOOPs, which all have their own uses, but they all are basically just variations of same idea of using IFs to check wether to continue or not.

Here is one:

Code: Select all

a=0
REPEAT
a=a+1
debugprint(a)
UNTIL a=10
This would result in:
1
2
3
4
5
6
7
8
9
10


Since it starts from REPEAT place, and everytime it comes to that UNTIL place, it will jump back to that REPEAT, until the IF condition is met. Although in this case you dont use IF command at all, since it is included in that UNTIL command.


Another very useful loop is using FOR and NEXT, you will use this all the time when coding to go through some lists.

Code: Select all

FOR a=1 to 10
debugprint(a)
NEXT
This would print out same as previous program.

Login in this one is that you first put command FOR, then you tell which variable should be affected, and from which number to which number

What happens in practice is that when it first time comes to this spot, it will set variable a to store number 1.

Then it does the stuff between FOR and NEXT.

When it reaches NEXT, it will go back to beginning of FOR, and increase variable "a"s value, by one.

In case variable "a" is already 10, then it wont go back to beginning of FOR anymore, but instead continue the program from NEXT on.

Next time something else again.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Beginner Coders tutorial to Hollywood

Post by Bugala »

Lesson 4: Lists and Tables.

Lists and Tables are actually very much same thing in Hollywood and their difference is quite subtle.

To form a list (or rather - a table), you could use:

Code: Select all

my_list = {"a", "b", "c", "d", "e"}
debugprint(my_list[2]
this would result in:
c

First of all, to form a list, you name it just like any other variable, since list/table is essentially, just another variable, although it contains more than just the single piece of information as variables in examples before have had.

But when comes the part after "=", which tells what is inside this variable, you will instead of putting single info, like number or string, use opening bracker "{", this will tell, that everything inside here, will be included in this table.

When you put stuff inside table, you put one info at a time, and you use "," to separate different infos.

Although I have simply put strings using "a", "b"... you could put any kind of data inside it. This means, you could put numbers, text, or even other tables. And actually, even functions, but that function data is bit more advanced thing.

To end putting any more info inside, you will close the table using closing bracket "}".


To get access to one if these datas stored inside your table, you need to call the variables name, and also tell hollywood the position of the info where it is.

This works so, that first you put the variables name, and then you put index number inside "[" and "]".

As in example, I am using debugprint to print out the data that is contained at index number 2.

However, here you need to notice one thing. For maybe you are already wondering, that shouldnt index number 2 actually contain "b", and not "c"?

This is because of how Hollywood (and most other C-languages) handle list indexing. They start the numbering from 0. Therefore, index 0 = a, index 1 = b, index 2 = c...


As I already mentioned, Tables can have tables inside them, this makes it possible to have multideimensional tables.

One very typical usage would be to use it to make a map:

Code: Select all

mapdata = {
                 [0] =  {"a", "a", "b" },
                 [1] =  {"a", "c", "a"},
                 [2] =  {"a", "a", "a"}
                 }

x=1
y=2

currentlocationsletter = mapdata[x][y]

debugprint(currentlocationsletter)
This would print out:
a

since that is the letter residing in that spot.


Notice in this one couple of things. I started my mapdata table with {
after that I used indexnumber to tell which index number i wish to manipulate, in this case [0],[1] and [2], these are the mapdata[x] part. I could have used any number I want, but in this case, it wouldnt have been practical. For lets say I would use 1, 5 and 9, if i would then try to access data location 2, i would receive an error, since that wouldnt exist.

I also made tables inside tables in this one. For when i showed that this is the info to go into data location [0] for example, i was putting another table inside that location. And to access that locations subdata (so to say), i woudl need to use anotehr dimension, in this case - y. in practice [dimension1][dimension2], and this could continue to as many dimensions as i ever like.


Notice also, that at end of tables closing bracket "}", i used data separator ",", except on last one. This is because aftger everyone else, there was still more data put to the table (which needs to be separated by ","), except for the last table, after which there was no more data coming.

This will be more clear with following example:

Code: Select all

mytable = { [color=#FF0000]"a"[/color], [color=#0000FF]"b"[/color], [color=#00BF00]"c"[/color] }
I have now separated each data, with different color. Now, let me change "b", into table instead.

Code: Select all

mytable = { [color=#FF0000]"a"[/color], [color=#0000FF]{ "f", "g", "h"}[/color], [color=#00BF00]"c"[/color] }
As you can see, from the main tables point of view, this table in "b"s location, is simply one data, and if there are more data coming after that (in this case "c"), then you need to use the "," to separate that data to its own index.


Notice, that while you are forming the tables data, you can also use existing variables to form it dynamically.

By the way, notice that Static means something that stays the same all the time, while dynamic means something that keep changing.

And example of dynamic and static in practice:

Code: Select all

wage = 500 + bonus
anotherplaceswage = basewage + bonus
In first one, since it always has been, and always will be, that the wage is based upon 500 + bonus, we can as well use the static number of 500, since it will never change.
But in second one, since basewage is renegotiated each year, it can this year be 450, while next year it will be 460. For this reason, we will instead of using static 450 number, use variable "basewage" to make it possible for us to simply change the "basewage = x" at beginning of code, instead of manually change each appearance of 450.

This is the difference between static, and dynamic data. Static stays the same always, and you never need to change it. Dynamic depends upon situation. Today it might be this, tomorrow that.


Lets make Dynamic forming example:

Code: Select all

name = "Andreas"
weight = 500

mytemptable = {name, weight}

mytable = { mytemptable }

debugprint(mytable[0][1])
would result:
500

what happened here, was that first i created variables name and weight.
After that, i used those variables to form the mytemptable.
in this mytemptable, these data had ben accessible by mytemptable[index number]

After this i created "mytable" to which i simply put as teh index[0] item the mytemptable.

Hence, to access this "mytemptable" stuff that i put in "mytable", i need to access them through index[0], using multidimensinal method.

By other wors, to access these "mytemptable" stuff put in "mytable", i would access them by using mytable[0][n]

where n is either 0 or 1, since there are no more items than those two.


Notice also, that you could use names instead of numbers as indexes on tables.

As example:

Code: Select all

a=4
mytable = { 
               [0] = {},
               ["name"] = {},
               [a] = {}
               }
to access stuff at "name", you would use mytable["name"][n] to access its stuff, where n would be the index number of what you are wanting to access.

Then this a part, you would access by using mytable[4][n], where n is once again the index number of what you are wanting to access from there.
Reason for this is, that you created the number dynamically. And as you had set that "a" is 4, hence the index number became 4 as well.


as final example, an example on how using tables and functions could nicely be used for example in some sims type game, where you want to click one of the characters, and then, depending upon time of day, it would tell you what it is doing.

Code: Select all

FUNCTION p_whatsupdoc(character, currenttime)
debugprint(character.name.. " is right now "..character.timeofday[currenttime])
ENDFUNCTION

person1 = {name = "Andreas", timeofday = { "Eating Breakfast", "Coding", "Sleeping" }   }
person2 = {name = "Falcucci", timeofday = { "Sleeping late", "Making money", "staying up late"}   }

mysims = { person1, person2 }

p_timeofday = 1 /*  0 morning, 1 day, 2 night /*
p_currentsim = 1

p_whatsupdoc(mysims[p_currentsim], timeofday)

This would result in:
Falcucci is right now Sleeping

If you keep changing p_timeofday from 0 - 2 and p_currentsim between 0 and 1, you will receive different results each time.

Notice also, that I both used new method to create table data, as well as used new method to access them.

Now, usually when you put for example "name" inside a table forming, it will dynamically change that name into what ever that variable "name" holds, but in this case i used "name = stuff", in this case it works differently. In this case it puts inside a variable. Like mytable.name which is actually a noirmal single variable, that you can use same way as normal variables, but you can access it through using myvariable.name

Similarly, if you use the "=" and form a table, this table becomes accessible by that name, in this example it was mytable.timeofday table

Notice also, that when i called the function, i sent both a table, and single variable. When i catched that table into variable "character" that variable got the whole table, and everything inside that table became accessible to it, by sijmply referring with character.anything.

As example, character.name woudl result in the name of the table item i sent.

Notice also, that I only sent one part of the table, not the whole table. I didnt send whole mysims table, but i only sent mysims[1] in there.

Therefore everytime i refer to for example character.name, it is same as referring to mysims[1].name


Next time finally some graphics.


edit: almost forgot to explain the difference between lists and tables.

Lists basically refer to one dimensional tables. There are some commands that use word list in them. For example, there is command that goes through every item on one list, and goign through this list means going through only one dimension, not subdimensions.

For example, if you would use this go through each item in mysims table, it would only see items 0 and 1 being tables, but it wouldnt go through to see what is inside these tables.

To see what is inside these subitems, you would need to use that comman again by using it to go through one of the subtables.
Post Reply