This site requires JavaScript, please enable it in your browser!
Greenfoot back
Julian
Julian wrote ...

2012/6/5

Call the method of an actor, where the actor's name is the contents of a string.

Julian Julian

2012/6/5

#
I want to call an object’s method from a string containing the name of the object. There is an Object “sh” It has a method “play” There is a string “grapheme” whose value is “sh” I could code: sh.play(); I want to code something like: grapheme.play(); Or to put it another way: getActorNamed(grapheme).play(); I could solve this with a large “switch” block, but there must be a better way. Thanks.
Archtects Archtects

2012/6/5

#
i dont quite understand what you mean, but couldnt u make an int grapheme = sh? or something im kinda confused i suck at explain and reading explanations lol, i think i got wrong end of the stick
Archtects Archtects

2012/6/5

#
or a variable?
danpost danpost

2012/6/5

#
You must have quite a few object that are named with string variables. Are all the objects of the same type? That is, are they all created with the same class, or are they of different types (created from different sub-classes of Actor)?
Julian Julian

2012/6/6

#
Thank you for replying. Yes I have 45 objects, all of the actor class "Grapheme". I all ready have two long blocks each more than 50 lines; one block to create the grapheme objects and one block to add them to the world. The scenario is: A text file contains a number of lines of text. Each line is read in sequentially. As each line of text is read, it is parsed into an string-array. I then have an array of strings, each element of which is the name of one of the grapheme objects. Each grapheme object has a method “play”. I wish to use the elements of the array to call the grapheme’s method “play”. I could create a “switch” block to compare each array element’s value with each object’s name, then call the object's play method e.g. “b.play()” So to the question: If "parsedString = "b"", Is there a way of turning a string-array’s element into a object-reference that can be used to call the object’s method e.g. “stringToObjectReference(parsedString).play()”. In short: Can I use a variable to call an object’s method, without having to create a large “switch” block?
davmac davmac

2012/6/6

#
You should use a map.
import java.util.*;
...
Map<String,Grapheme> graphemeMap = new HashMap<String,Grapheme>();
To initialise the map:
graphemeMap.put("objectName1", objectName1);
graphemeMap.put("objectName1", objectName2);
...
To access a named object:
graphemeMap.get(parsedString[1]).play();
Duta Duta

2012/6/6

#
davmac wrote...
You should use a map.
import java.util.*;
...
Map<String,Grapheme> graphemeMap = new HashMap<String,Grapheme>();
To initialise the map:
graphemeMap.put("objectName1", objectName1);
graphemeMap.put("objectName1", objectName2);
...
To access a named object:
graphemeMap.get(parsedString[1]).play();
Just a typo correction to avoid confusion: The second item in the map should be
graphemeMap.put("objectName2", objectName2);
//rather than
graphemeMap.put("objectName1", objectName2);
Julian Julian

2012/6/6

#
Thank you both for your help. Without your help I do not thing I would have found "map". But I have discovered after many years of learning new things, that the best way to learn something is to ask an expert. I now have two maps: one mapping graphemes to objects, and one mapping keyboard keys to graphemes. So I end up with the quite interested & complex line... graphemeMap.get(kbdBindingMap.get(key)).play(); If in six months I still understand the above line, I will feel I am on my way to being an expert. Thank you.
You need to login to post a reply.