![]() I created the “child” property in order to place objects inside or upon other objects. Instead, you just call your designated function/s and let it handle the method of accessing the data you need, without burdening the rest of the program to bother about the exact details involved or which table is used to achieve the result. Using a few central functions gives more flexibility than asking forįrom numerous other functions or places in the code. The local table data points to the global table g_objData and local variable data_id points to g_objData ![]() Notice that there is only one reference to the table g_objData at the start the rest of the function uses local pointers. The main part of the function returns the object properties of g_objData by using the local pointers. If any of them are nil then a zero value will be returned. The function above is called with two arguments first the long ID string, then the string type, here providing the object property key that have already been defined in function add_obj().įirst the function checks to make sure that three variables are not nil id_long, type and data, since variables that hold a nil property will throw an error when they are accessed. Local data = g_objData - 'data' points to table g_objDataĮlseif type = "held" or type = "carried" then Next, I would like to access the object data in the game through at least two functions īelow is an example of a function returning the object property data: function get_obj_property(id_long, type) ![]() The last line in the above example would print out “box” in the console window, provided that the table g_objData has been filled using the above function add_obj(), or a similar function. This table could also be shown as such: local id_long = "ID000001" It is visible and have a container property of 10, which could be used to define weight capacity, volume or a certain number of items.Įvery time the add_obj() function finishes, it increments the global variable g_nextObjectID with one so that whenever the function is called, the next object will have a unique ID.Īfter the function add_obj() is called, the global object property table g_objData will be filled like this: g_ = 1 - Object room location It has a property that tells us it can be carried, though the object is not yet held by anyone. G_nextObjectID = 1 - assigns ID 1 to the first objectĪbove, the function add_obj() is called with a few parameters that can be used for creating a simple box object. Object propertiesĪt this point I would like to store object properties in a global table, which we could call g_objData. With the object ID approach, moving objects from a room to the player inventory, or vice versa, is relatively simple and mainly involves moving the object IDs around, combined with changing a few properties. The double periods “.” concatenates the strings together as a single string. The string is comprised of “ID” + the number of zero’s + the short id number in string form. ![]() #num returns the length of the number string, similarly like writing string.len(num).įinally the function returns the ID. String.rep replicates the first argument “0” as many times as needed maxlength – #num. Local zeros = string.rep("0", maxlength - #num)Ībove, tostring(id_short) transforms the value of id_short into a string. function get_ID_format(id_short) - Returns a long ID object 1 = ‘ID000001’īelow is a simple function I wrote that transforms numbers into an ID code format like the above example. While you could compress the object IDs to hexadecimal format or using bits and bytes, I prefer having the IDs clearly readable for code debugging reasons. The benefit of this approach is that you don’t need to move around a lot of data when moving the objects in the game. Third part – Object IDs and object propertiesįor my interactive fiction game I want to access my objects via ID codes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |