How to get the string value from a hash("string")


#1

Hi! Need a little bit of help please.
Give hash(“stringvaluehere”), how to do i extract the string inside the hash? I would like to use it for an operation that requires a string.


#2

You can use hash_to_hex on a hash to get that value returned as a string.


#3

do you mind giving me an example?
I tried doing hash_to_hex(hash(“string”))
wanted to get “string”, but instead got a hex value (as expected from the function name)


#4

Hashing a string is one way. When you send a hashed value that’s the kind of value sent, because they are meant to be unique IDs for message passing.


#5

Thats what i thought. Thanks for confirming that. So i guess my real question is why i cant seem to declare a string property. I’d like to pass a string to another script but go.property wont allow me strings.


@Pkeod do u mind jumping to my other question?


#6

By the way, hashing the same string will always give the same hash. You can pre-hash strings too.

I’ll answer that on other thread sure.


#7

Sorry for bumping this topic, but I just try to solve that maybe will help out how to get the answer…
Since I have a same problem with it, but I solve with tricky way, here my example:

local texture = gui.get_flipbook(self.node) -- example, need to get the animation from that node
print(texture) -- > DEBUG:SCRIPT: hash: [Button_Red]
self.texture = string.sub(tostring(texture), 8, #tostring(texture)-1)
print(self.texture) -- > DEBUG:SCRIPT: Button_Red

why 8 and -1? You can count it from tostring console output that string you wanted to get


#8

You should not do this. It will only work in debug builds. In a release build there’s no reverse hash look-up enabled and your code will not work.

In your case of matching a flipbook animation id to a string you’d need to create the look-up table from hash to string yourself, or even better always work with the hashed values.


#9

Sorry for asking in this thread, but can I get the reference/example for look-up table from hash or get hash value? As you said. Because I need to return val/string that texture/animation’s node have. (Same like title thread)


#10

May I ask why you need this?


#11

Based on this button implementation:


I want to make button implementation that behave like 2 sprite when pressed and not

And for implement this, then I have 1 node on .gui with not-pressed flipbook/animation or texture(?) in component, called “button_red”.
So, when it clicked, it will change to “button_red2”, but since I have no idea to return that hash into string because I get_texture from that node, then im doing this:

-- button.lua
function Button.new(context, node_name, type, handler)
…
if self.button_type == SPRITE_TYPE then
	local texture = gui.get_flipbook(self.node)
	self.texture = string.sub(tostring(texture), 8, #tostring(texture)-1)
	self.next_texture = string.sub(tostring(texture), 8, #tostring(texture)-1).."2"
...

later, I call this string value on variable to change node

-- button.lua
function Button:press()
	if self.button_type == SPRITE_TYPE then
		gui.play_flipbook(self.node, self.next_texture)
...

Sorry for long explanation, maybe its bit weird grammar, but yeah, I have no idea in the first place to make polished button on GUI, then this is why im doing it.


#12

I think you’re complicating things a bit. I’ve created an example of how to create buttons with visual states using only a couple of lines of reusable Lua code:

Code: https://github.com/britzl/publicexamples/tree/master/examples/simple_button
HTML5: http://britzl.github.io/publicexamples/simple_button/index.html

There’s also the button example: https://www.defold.com/examples/gui/button/
And if you want something more advanced then check out Gooey: https://www.defold.com/community/projects/79521/