What can replace hashes in Release mode? (SOLVED)

Referring to Ragnar_Svensson: “The thing that differs in release mode is that we throw away all the reverse-hash info to save memory. If you ever do things like local s = str(my_hash), that won’t work in release mode.”

This is sad :smiley: Because I have a lot of “local s = str(my_hash)”. For example I send message with hash of the object to identify from where exactly my message has come from. I compare hashes and If message comes from one object, I’ll do one thing, if from another - other thing, etc. And now, all my logic is kinda broken with release build. :frowning:

Here is the brief example of code (this code is ugly with lot of copy-pasting, I know :frowning: ):

self.id = tostring(go.get_id())
	if self.id == "hash: [/turret1]" then
		if energy > 0 then
			msg.post("laser1#laser", "enable")
			msg.post("laser1#scr_laser", "enabled")
		end
		msg.post("laser1#scr_laser", "turret_upgraded", {turretState = self.state})
	elseif self.id =="hash: [/turret2]" then
		if energy > 0 then
			msg.post("laser2#laser", "enable")
			msg.post("laser2#scr_laser", "enabled")
		end
		msg.post("laser2#scr_laser", "turret_upgraded", {turretState = self.state})
	elseif self.id =="hash: [/turret3]" then
		if energy > 0 then
			msg.post("laser3#laser", "enable")
			msg.post("laser3#scr_laser", "enabled")
		end
		msg.post("laser3#scr_laser", "turret_upgraded", {turretState = self.state})
	elseif self.id =="hash: [/turret4]" then
		if energy > 0 then
			msg.post("laser4#laser", "enable")
			msg.post("laser4#scr_laser", "enabled")
		end
		msg.post("laser4#scr_laser", "turret_upgraded", {turretState = self.state})	
	end

So the question is how to rewrite my code? How one should identify from where the message comes from, except the use of hashes? Maybe there are some other approaches here? Thanks!

You can create a hash from a string by simply calling

hash("some_string")

and use that instead of the string value in your code.

For example in your code do something like (untested):

self.id = go.get_id()
	if self.id == hash("turret1") then
    ...
3 Likes

I’ve made a quick test of this and it works. Thank you!

2 Likes