Parts of the message I am sending are nil

here is the code from the sender

function on_input(self, action_id, action)
	if action_id == hash("Note1") and action.pressed then
		factoryname = hash("#FireBall_factory")
		msg.post("/SpellFactory#SpellFactory", "create", {position = go.get_position(), rotation = vmath.quat(0,0,0,0), factoryname, components = {vel = vmath.vector3(0,0,1)}})
	end
	
end

here is the code in the receiver

function on_message(self, message_id, message, sender)
	if message_id == hash("create") then
		pprint(message.components)
                print(message.factoryname)
		makespell(message.factoryname, sender, message.rotation, message.components, message.position)
	end
end

both message.factoryname and message.components are nil and I am setting them in the table I sent through the message.

There are two problems with this, one per line, but first let me reformat it for readability:

factoryname = hash("#FireBall_factory")
msg.post("/SpellFactory#SpellFactory", "create", {
    position = go.get_position(),
    rotation = vmath.quat(0,0,0,0),
    factoryname,
    components = {vel = vmath.vector3(0,0,1)}
})

First problem is that you declare factoryname as a global function. You should always use the local keyword when declaring variables (unless you really know what you are doing):

local  factoryname = hash("#FireBall_factory")

Second problem is that the message you’re passing is a mixed table of key-value pairs and array indexed values. You have position = go.get_position(), rotation = vmath.quat(0,0,0,0) and components = {vel = vmath.vector3(0,0,1)}. All of these are key-value pairs in the message table but you don’t do the same for the factory name. You should do factoryname = factoryname:

msg.post("/SpellFactory#SpellFactory", "create", {
    position = go.get_position(),
    rotation = vmath.quat(0,0,0,0),
    factoryname = factoryname,
    components = {vel = vmath.vector3(0,0,1)}
})

Now, you might wonder what happened to the factoryname? On the receiving end you could do pprint(message) to see the contents. You will see that the factoryname is there, but it will have been assigned the key 1, since it is the only value assigned to the array part of the table. Read more about Lua tables here: Programming in Lua : 2.5

3 Likes

Thank you for the help of lua tables. When I pprint(message) i get:

DEBUG:SCRIPT: url: [main:/SpellFactory#SpellFactory]
INFO:DLIB: SSDP: Started on address 10.168.1.199
DEBUG:SCRIPT: 
{
  scale = 1,
  position = vmath.vector3(0, 0, 0),
  id = hash: [0 (unknown)],
  index = -1,
  scale3 = vmath.vector3(0, 0, 0),
  rotation = vmath.quat(0, 0, 0, 0),
}

I never added scale, index or scale3 to the message table, and Facotoryname and components are not there.

You could you please try renaming the message from “create” to maybe “create_spell”? I’m thinking that “create” is a message used internally by the engine.

wow that worked, thank you!

Aw. Are there more reserved messages like this?

Yes, quite a few. Most components are controlled via messages, but there are wrapper functions (with the same name) for convenience.