Peoples,
In a recent discussion with @CJW and @cup_half_empty (Simulating Objects) @CJW suggested the possibility of a cslib module to handle classes or objects.
The use case for this was:
I have a scene in which a dragon will appear but I don’t know beforehand which of the many dragons will appear.
I have to be able to code it generically such that it will work whatever dragon appears.
I couldn’t locate anything on the forum so what I came up with, and where I would appreciate comment, is the following:
The basic syntax for this is (per the dragon example)
*create dragon_proto_1 “name”
*create dragon_proto_2 “hp”
*create dragon_proto_max 2
*create dragon_name “”
*create dragon_hp “”
*create dragon_1_name “Wally”
*create dragon_1_hp 100
*create dragon_2_name “Dave”
*create dragon_2_hp 150
*create dragon_max 2
When I am coding my scene, I simply use dragon_name and dragon_hp.
When I find out what dragon it is, I use a"set" command which copies the relevant dragon information into dragon_name and dragon_hp.
Very simple. There are all sorts of funky things you could do like allowing inheriting fields from virtual classes but I’m not sure that level of complexity is desirable.
I’ve written the beginnings of a possible cslib_class file (incomplete and probably buggy) which I would attach if I could figure out how to do so. In default of which I am pasting the whole thing in here.
Thoughts?
Stephen
*comment cslib_class
*comment Allows the simulation of objects/classes (sort of)
*comment @StephenHart
*comment
*comment How it works:
*comment A class is always accessed through its main instance.
*comment When you “get” an instance of a class, the data for that particular instance
*comment is copied into the main instance
*comment
*comment A class definition has the following elements defined in startup.txt:
*comment 1. A class “prototype” array defining all the field names
*comment classname_proto_N fieldname
*comment 2. A main instance of this class. This is used to ‘return’
*comment the class (similar to cslib_ret).
*comment 3. An array of instances of the class
*comment
*comment Public methods
*comment get_by_index class index
*comment copy the instance of the class at index into main instance
*comment get_by_field class field value
*comment copy the first instance of the class that has specified field equal to specified value
*comment into the main instance
*comment get_random class
*comment select a random instance of the class and copy it into the main instance
*comment set_by_index class index field value
*comment set the specified field to the specified value in the instance
*comment of the class at index
*comment set_by_field class field oldvalue newvalue
*comment select the first instance of the class that has specified field equal to specified oldvalue
*comment and set that field to the newvalue
*comment
*comment ------------------------------------------------------------------
*comment EXAMPLE startup.txt - class “dragon”
*comment
*comment *create dragon_proto_1 “name”
*comment *create dragon_proto_2 “hp”
*comment *create dragon_proto_max 2
*comment
*comment *create dragon_name “”
*comment *create dragon_hp “”
*comment
*comment *create dragon_1_name “Wally”
*comment *create dragon_1_hp 100
*comment *create dragon_2_name “Dave”
*comment *create dragon_2_hp 150
*comment *create dragon_max 2
*comment
*comment EXAMPLES of getting a dragon instance
*comment
*comment cslib_class get_by_index “dragon” 2
*comment sets dragon_name to “Dave” and dragon_hp to 150
*comment cslib_class get_by_field “dragon” “name” “Wally”
*comment sets dragon_name to “Wally” and dragon_hp to 100
*comment
*comment used for passing around results in this file
*temp local_ret false
*comment GET_BY_INDEX
*comment ------------------------------------------------------------------
*comment Takes the instance of a class specified by the index and copies
*comment the data into the main instance
*comment ------------------------------------------------------------------
*comment
*comment params:
*comment p_classname (string): the name of the class
*comment p_index (integer): index of this class instance
*comment returns:
*comment updates main class instance
*comment usage example:
*comment cslib_class get_by_index “dragon” 2
*comment ------------------------------------------------------------------
*label get_by_index
*params p_classname p_index
*gosub _set_main_instance p_classname p_index
*return
*comment GET_BY_FIELD
*comment ------------------------------------------------------------------
*comment Gets the class instance that matches the given field value pair
*comment Does not check for duplicates and returns the first match it finds
*comment searching from lowest index to highest
*comment ------------------------------------------------------------------
*comment params:
*comment p_classname (string): the name of the class
*comment p_fieldname (string): the field to be matched
*comment p_value (string): the value to be matched
*comment returns:
*comment updates main class instance
*comment usage example:
*comment cslib_class get_by_field “dragon” “name” “Wally”
*comment ------------------------------------------------------------------
*label get_by_field
*params p_classname p_fieldname p_value
*comment TBD
*return
*comment GET_RANDOM
*comment ------------------------------------------------------------------
*comment Get a random instance of a class
*comment ------------------------------------------------------------------
*comment params:
*comment p_classname (string): the name of the class
*comment returns:
*comment updates main class instance
*comment usage example:
*comment cslib_class get_random “dragon”
*comment ------------------------------------------------------------------
*label get_random
*params p_classname
*temp max = {p_classname&"_max"}
*temp idx 0
*rand idx 1 max
*gosub _set_main_instance p_classname idx
*return
*comment SET_BY_INDEX
*comment ------------------------------------------------------------------
*comment Set the value of a particular field in a particular instance of a class
*comment ------------------------------------------------------------------
*comment params:
*comment p_classname (string): the name of the class
*comment p_index (integer): index of this class instance
*comment p_fieldname (string): the field to be matched
*comment p_value (string): the value to be matched
*comment returns:
*comment none
*comment usage example:
*comment cslib_class set_by_index “dragon” 2 “name” “Bruce”
*comment ------------------------------------------------------------------
*label set_by_index
*params p_classname p_index p_fieldname p_value
*set {p_classname&"${p_index}${p_fieldname}"} p_value
*return
*comment SET_BY_FIELD
*comment ------------------------------------------------------------------
*comment — find the instance of a field with the given name and replace it
*comment ------------------------------------------------------------------
*comment params:
*comment p_classname (string): the name of the class
*comment p_fieldname (string): the field to be matched
*comment p_oldvalue (string): the current value in the field
*comment p_newvalue (string): the new value for this field
*comment returns:
*comment none
*comment usage_example
*comment cslib_class set_by_field “dragon” “name” “Dave” “Bruce”
*comment ------------------------------------------------------------------
*label set_by_field
*params p_classname p_fieldname p_oldvalue p_newvalue
*comment TBD
*return
*comment ------------------------------------------------------------------
*comment — PRIVATE METHODS
*comment ------------------------------------------------------------------
*comment — ------------------------------------------------------------------
*comment — Copies the values in the instance at the given index into the main instance
*comment — ------------------------------------------------------------------
*label _set_main_instance
*params p_class p_index
*temp n 1
*temp field_count {p_class&"_proto_max"}
*label set_loop
*if (n <= field_count)
*temp field {p_class&“proto${n}”}
*set {p_class&"${field}"} {p_class&"${p_index}${field}"}
*set n + 1
*goto _set_loop
*return
*comment — ------------------------------------------------------------------
*comment clear all the fields in the main instance
*comment — ------------------------------------------------------------------
*label _clear_main_instance
*params p_class
*temp n 1
*temp field_count {p_class&"_proto_max"}
*label clear_loop
*if (n <= field_count)
*temp field (class&“proto${n}”)
*set {p_class&"${field}"} “”
*set n + 1
*goto _clear_loop
*return