GetWorld parameter mismatch
#1
hi,

i try to learn lua and write plugins

now i have the first question:

i use this function:
function OnPlayerRightClick(Player)
	local Welt = cRoot:GetWorld("world")
	Player:SendMessage(Welt:GetName())
end

And get this error:
Code:
Warn [10:15:58] LUA: Plugins/test2/main.lua:46: error in function 'GetWorld'.
argument #1 is 'class cRoot'; 'cRoot' expected.

Warn [10:15:58] Stack trace:
Warn [10:15:58]   [C](-1): GetWorld
Warn [10:15:58]   Plugins/test2/main.lua(46): (no name)
Warn [10:15:58] Stack trace end
Warn [10:15:58] Error in plugin test2 calling function <callback>()

but i have no idea why?

The API-doc says:
Quote:Returns the cWorld object of the given world. It returns nil if there is no world with the given name.
Did the "WorldName" mean not a string? Or whats wrong with me?Big Grin
Can anyone help me? Smile
Reply
Thanks given by:
#2
For cRoot you need to use Get() as access. That's written in the top of the cRoot documentation.

Use
cRoot:Get():GetWorld("world")
to access the world object.
Reply
Thanks given by:
#3
"cRoot" by itself is a class name, not an actual object. You wouldn't expect to write "cPlayer" and the server to magically know which one player you mean Smile
This class has a (static) method "Get()" that returns the one instance of the object that the server is using. So if you want a cRoot object, you need to call "cRoot:Get()"
Finally, on this object you call its (non-static) method "GetWorld()".

I know, it is needlessly complicated, we could have made the GetWorld() function static as well (same with most of the cRoot functions, they would just operate on the single cRoot instance ever created in the server). Historical reasons.

What is even more unfortunate is the error message produced by ToLua. It is very confusing, and whoever wrote it should be ashamed of themselves. Unfortunately it's part of ToLua and as such is quite difficult to change to anything more reasonable.

One thing you might want to remember from this: When you write "Object:Function(Param)" in Lua, it actually means "Object.Function(Object, Param)" (the Object is passed as a hidden first parameter to the function), that's why ToLua is complaining about the first parameter - it is not the world's name, it's the cRoot.

A final thought, you may want to write your code in English, you never know when you're gonna opensource it or when you need help from outside Smile

--
I edited the title of your post to be more descriptive of the thread contents.
Reply
Thanks given by:
#4
The error is not too difficult to change. The issue is that unless you want to go down the route of having lots of heuristics, you can't actually tell a static call from a method call by the time it reaches the bindings. Eg What would be an appropriate error for cRoot.GetWorld("cRoot"); ?
Reply
Thanks given by:
#5
I think, you must use World object from player. In multiworld instance you do not have problems in future.

local Welt = Player:GetWorld();
Reply
Thanks given by:
#6
@worktycho the bindings know, the error messages distinguish between "class cRoot" (static) and "cRoot" (instance-bound)
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)