Game Development Reference
In-Depth Information
When you run this code, you will see that we save the data we create as a string to a file called
mytable . In the example, we read the data back and decode the JSON-encoded string to an
expanded Lua form table. Then we list the fields available in that table to prove that the code works.
Note Though this code is completely valid, if you run the code in the Lua interactive shell, you might
face issues such as error messages, as there is no json.lua file in the path.
Dynamic Variables
number = 3
print("Object" .. number)
and the expectation is that it should output Cat . However, what it outputs is Object3 . Most beginning
developers assume that it should have worked as expected. If you need to access the variable in
this manner, it is not by creating multiple variables. The proper way to do so is to create an array. The
array can be accessed by passing it the number as we expected to do.
Objects = {"Apple", "Ball", "Cat", "Dog"}
number = 3
print(Objects[number])
This works perfectly fine for what we need. This time around we see the output Cat .
Summary
In this chapter you learned about the implicit and explicit functions relating to file I/O. You also saw
how to read and write data as singular values, and how to write an entire table of data onto the
storage space on the device. Files are good for saving scraps of data; however, when you need to
access larger amounts of data, it would be better to use a database.
In the next chapter we shall look at the math namespace, which offers functions that we can use to
do all of the calculations required for our apps.
 
Search WWH ::




Custom Search