We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Everything I find online, stackoverflow, says a HashMap<String, Float>(); should store only references to the floats.
When I store something in the hashmap, use hashmap.get() to get the float object, I can't seem to write to the original object. In this example I try to get and modify the original float1 variable without success. It seems to modify a new float stored in the hashmap in test2, test1 does nothing.
Float float1 = 1.1;
HashMap<String, Float> hm = new HashMap<String, Float>();
void setup() {
hm.put("float1", float1);
Float f1 = hm.get("float1");
f1 = 5.5;
println("test1");
println("float1: " + float1);
println("hashMap float1: " + hm.get("float1"));
println("both should should be " + f1);
println();
println("test2");
hm.put("float1", f1);
println("float1: " + float1);
println("hashMap float1: " + hm.get("float1"));
println("both should should be " + f1);
println();
}
Here's the output
test1
float1: 1.1
hashMap float1: 1.1
both should should be 5.5
test2
float1: 1.1
hashMap float1: 5.5
both should should be 5.5
Answers
when you put something in the HashMap it holds a copy of the value, not the reference to the old variable.
Technically, it uses a reference to a float variable but a copy of the old value, not the same pointer address as the old variable. Since the value not the reference
So is there a way it could be done?
I have a game style ` console window, and I want to be able to set and retrieve variables by typing in their names. I thought I could just add all the variables I wanted to make accessible to a hashmap. There has to be a better way than hard coding responses for every variable.
It wouldn't be terrible to do it with a switch(), but I'd like to retain tweak-mode functionality and it seems using switch breaks that.
Do you want to do that for debugging?
Did you look at the debugger / processing? In theory it does exactly that.
variables should be in an array, then you can easily show them
show your code so let's explain what you mean
Example:
float with a small f
Result
3.1415927 3.1415927
true
edited: https://files.fm/u/ajs3c7hf new version has better autocomplete various bugfixes
Here's my project. It's intended to be a reusable template to help speed up prototyping games.
The console can view and set variables, but it can also call functions with arguments, which I can't do from debugger. It also has a nice benchmarking tool. I have a bunch of snippets from other old game projects I want to add in as well like a graph logger to show variables changing over time. That's another case I wish I knew the java way around not having pointers, I'd like to be able to add any of those variables accessible from console to the graph display easily.
I'm looking for better ways to implement the way it references functions and variables, ideally more directly like I'm used to in c++.
Theres an autocomplete system for the console window, still a bit buggy.
impressive but I don't really have an idea for you...
@lmccandless Can you share your final solution?
To answer your question, what you need to do is not to use get but to set. Or you could get a copy of the current position, modify this value, and then set the position with that new value. I guess this is what you implemented at the end?
Kf
No, he tries to have a list of all his variables to be able to quickly view their values in a console
He want to use HashMap() for that but this won't work.
Idea
if you know the names of the variables you can hard code them
(js eval is NOT needed here at all):
https://github.com/lmccandless/gameTemplate Here's an update
My solution was to just use the variables stored in the hashmap. It's not ideal for performance or usability, but not all variables need to be accessible to the console, so I guess it's a manageable system.