GetWorld parameter mismatch - Printable Version +- Cuberite Forum (https://forum.cuberite.org) +-- Forum: Plugins (https://forum.cuberite.org/forum-1.html) +--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html) +--- Thread: GetWorld parameter mismatch (/thread-2181.html) |
GetWorld parameter mismatch - heroldini - 10-26-2015 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'. 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? Can anyone help me? RE: heroldinis beginner questions^^ - Seadragon91 - 10-26-2015 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. RE: heroldinis beginner questions^^ - xoft - 10-26-2015 "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 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 -- I edited the title of your post to be more descriptive of the thread contents. RE: GetWorld parameter mismatch - worktycho - 10-27-2015 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"); ? RE: GetWorld parameter mismatch - DrMasik - 10-27-2015 I think, you must use World object from player. In multiworld instance you do not have problems in future. local Welt = Player:GetWorld(); RE: GetWorld parameter mismatch - xoft - 10-27-2015 @worktycho the bindings know, the error messages distinguish between "class cRoot" (static) and "cRoot" (instance-bound) |