Do you know if hashing a string will give me the exact same hash value on different hardware/OS (given that the string is the same)?
Yes it should, if you use our hash function.
Are you experiencing trouble?
Maybe the wrong thread, but…
If I try to send a hash using the msg.post an error occures about “userdata”. how to send a hash-value. F.e. a go.property(“name”, hash(“Peter”)) sending using msg.post()??
Thanks Mathias. No, no problem, just trying to recreate states and id’s on client/server without having to send it all back and forth. I will try it out now.
@stephenkalisch Sending a hash message should be fine over messages. What you cannot do is to use hashes as keys.
Working:
msg.post("/some_object", "get_data", { data = hash("Peter")})
Not working:
msg.post("/some_object", "get_data", { [hash("Peter")] = 12345 })
thanks, but my problem is more like this:
at the beginning I define some
go.property(“name”, “Peter”)
later I am working with the variable in the way self.name.
then, I try to send some data to another skript in the way
msg.post(“main:/mygui#gui”, “infobox”, {name = self.name}) <- here comes an error about userdata. how to send a hashed string? do I have to hash it again like
msg.post(“main:/mygui#gui”, “infobox”, {name = hash(“name”)}) ???
Hmm, I just tried this without problems:
go.property("test", hash("my_value"))
function init(self)
msg.post("main:/gui#gui", "hello", { data = self.test })
end
and in the GUI script:
function on_message(self, message_id, message, sender)
print(message_id) --> DEBUG:SCRIPT: hash: [hello]
pprint(message) --> DEBUG:SCRIPT:
-- {
-- data = hash: [my_value],
-- }
end
Oh… That should be:
go.property("name", hash("Peter"))
Properties cannot be string values.
Oh, my bad!
Its not the “sending”… its the receiving and GUI stuff.
at the gui.guiscript its like this:
function on_message(self, message_id, message, sender)
if message_id == hash("unitInfo") then
gui.set_text(gui.get_node("a"), message.a)
gui.set_text(gui.get_node("b"), message.b)
gui.set_text(gui.get_node("c"), message.c)
end
end
these nodes are simple text.
if I use the msg.post said before
msg.post("main:/ingamegui#gui", "unitInfo", {a = self.hitpoints .."/".. self.maxhitpoints, b = self.energy .. "/" .. self.maxenergy, c = self.name})
the script ends with this error:
ERROR:SCRIPT: /main/ingameGUI/gui.gui_script:20: bad argument #2 to 'set_text' (string expected, got userdata)
stack traceback:
[C]: in function 'set_text'
/main/ingameGUI/gui.gui_script:20: in function </main/ingameGUI/gui.gui_script:16>
ERROR:GAMESYS: Error when dispatching message to gui scene: -2. Message 'unitInfo' sent from main:/collector#script to main:/ingamegui#gui.
Ok.
So first of all.
You should never use hashes string-representation in the code. It’s actually only working on the debug-version of dmengine and is also not really giving you the single string.
In this case maxhitpoints, energy etc is fine as they are numbers ( if you want for clarity you could wrap them in tostring() )
There is no easy/good way to pass a string into a gameobject via properties (which I believe is what you are doing with self.name). You should try to init it via messages or other ways. Now it results in the gui node tries to set the textfield with a hashobject instead of a string.
hm.
In one of the examples or other projects I saw the go.property to use strings as in-code as also in editor.
IMHO a good way of changing values at editor-side. Other game-engines work like this.
Should a table be a better way?
Defold is confusing. the defold/lua combi makes it harder than in other engines or languages to develop. I am trying to solve easy things but I am on it for days now. I already coded this project without any engine. the hope to do it well and more easy and efficiant in an engine will get gone step by step o.O
Explain what you are trying to do and I’ll create an example according to Defold best practices.
Thanks, but where to start? I cannot explain the whole game
Im new to lua and new to defold which makes everything much harder. First I got problems with the “non strikt variables” of lua. local dont mean local like in other oop languages. next is this “messsage-thing” in defold. Normally you got simply classes and methods to give them variables etc. the way of coding and developing is a “stony way”. but I’ll learn it. step by step (or better: error by error :D). Last I tried the Unity3d engine. IMHO more easy to work and learn, but to “blown up” for a simple 2D game and I guess to slow. Thats why I ran out to find another nice 2d engine and ended up here, at defold.
I’ve loaded your (downloadable) examples -thanks by the way- and try to learn from.
I am not sure if we really post all this in this thread or forum. I quess I got a couple of problems and questions, specially understanding the logic. And I quess you wont spend your time for all that. I always try to find a solution in all the examples and the forum, but sometimes I dont find something or its not “near enough” to my problem.
I wont take your time to solve my problems (or my stupidness ).
Thanks for your help! If you like to help me ( I hope you will), I’ll open a thread If I dont find anything in the forum.
I am just losing my motivation if everything I try ends up in ??? and errors
oh we lost the focus above…
why the gui-script cant handle my msg.post of self.name?
You are most likely trying to set the text of a box node using a hash and not a string. A hash is generated from a string and once it’s become a hashed string it can no longer be used as a string. In debug mode we reverse the hash to get the string back, but that is for debugging only.
It seems to me that you are trying to find a way to use go.property() to define a string on a game object and then pass that string on to a gui to set the text of a text node. Is this correct?
My suggestion for your general troubles is: read the manual first.
You’re not saving any time by not reading the manuals. In fact you’re making it a longer harder process than it needs to be, even if some of the reading seems boring. If you had read the lua manual you would know everything about lua that you need and skipped all of that frustration. Likewise for most of your other issues.
correct.
I am (again) studying your examples. I really like your “one room” game ;).
In that code you define some variables using the go.property. For example in the bullet.script to define some values like damage etc.
I use this for having different kind of units (means: tank, rocketlauncher, jeep etc). to define all values like name, movespeed, hitpoints etc.
In this I can attach the script (called unit.script) to all the units gameobjects. After that, I enter different values for each gameobject/unit. The tank have more HP but slower etc.
Using this method looks good for me and is similar to unity (by the way). BUT, I also try to give them a name -which is a string of cause. Giving a simple string using go.property is not possible (manual), solution: hash(“name”). But this makes trouble later on… thats the point.
//EDIT:
I saw you are not really using the go.property values ingame. you got everything in the tables.
I’ll recode the units using tables instead.
You could map a hash property to a table with additional data (for instance the name):
local unit_data = {
[hash("tank")] = {
name = "M4 Sherman",
speed = 200,
damage = 150,
hitpoints = 300,
cost = 1000,
},
[hash("infantry")] = {
name = "Cannon fodder",
speed = 50,
damage = 40,
hitpoints = 15,
cost = 50,
}
}
go.property("type", hash("tank"))
function init(self)
local data = unit_data[self.type]
print(data.speed)
print(data.name)
msg.post("#gui", "some_message", { name = data.name })
end
go.property() is useful for some things, especially stuff that you wish to change when using factory.create() since you can pass those values in the call.
thanks for the hints! and the example. I will do some tryouts with this method.
@britzl
Looks good so far. Added 2 units (tank & rocketlauncher) with different values and the hash-tag.
attached the script at 2 (new) gameobjects and “switched” the hash for each GO. Nice thanks.
Next is to implement a function in that table for the different “projectiles”/bullets whatever.