attempting to make a plugin to spawn giants
#1
Hi,
I am new to Lua and I wanted to modify the tutorial code a bit to create a plugin to spawn giants (1.12.2). I think I got most of it down, but I am having an issue with two different parts of it. 
Console Errors:

[12:58:58] LUA: Plugins/SpawnGiants/spawngiants.lua:28: attempt to call method 'SpawnMob' (a nil value)
[12:58:58] Stack trace:
[12:58:58]  Plugins/SpawnGiants/spawngiants.lua(28): (no name)
[12:58:58]  [C](-1): FindAndDoWithPlayer
[12:58:58]  Plugins/SpawnGiants/spawngiants.lua(35): (no name)
[12:58:58] Stack trace end
[12:58:58] Error in <attached> calling function <callback>()


Code:
Code:
PLUGIN = nil

function Initialize(Plugin)
    Plugin:SetName("GiantSpawn")
    Plugin:SetVersion(1)

    -- Hooks

    PLUGIN = Plugin -- NOTE: only needed if you want OnDisable() to use GetName() or something like that

    -- Command Bindings
    cPluginManager.BindCommand("/spawngiant", "SpawnGiants.main", SpawnGiant, " ~ Spawn a Giant at someone's location")


    LOG("Initialised version " .. Plugin:GetVersion())
    return true
end
function SpawnGiant(Split, Player)
    if (#Split ~= 2) then
        -- Send the proper usage to the player and exit
        Player:SendMessage("Usage: /spawngiant [PlayerName]")
        return true
    end

    local CommandSent = false
    local GiantSpawnPerson = function(Giantee)
        if (Giantee:GetName() == Split[2]) then
            Player:SpawnMob(giant)
            Player:SendMessageSuccess(Split[2] .. " has been attacked by a giant")
            CommandSent = true
            return true
        end
    end
    cRoot:Get():FindandDoWithPlayer(Split[2], GiantSpawnPerson)

    if not(CommandSent) then
        Player:SendMessageFailure:(Split[2] .. " was not found")
    end

    return true
end

function OnDisable()
    LOG("Shutting down...")
end


I am new to Lua and new to programming in general. Any help would be appreciated (This is running on a raspberry pi, I am not sure if that makes any difference.)
Reply
Thanks given by:
#2
Hi,

The Player doesn't contain a SpawnMob Method.

You need to get the world object from the player and call the spawn method from there.

Code:
Player:GetWorld():SpawnMob(Player:GetPosition(), mtGiant, false)
´

Edit: This wall cause the giant to spawn at the position of the player using the command. If you replace "Player" with "Giantee" you should get your desired result
Reply
Thanks given by: Code_Blue
#3
Hi, welcome to the forum!

The reason it's giving an error is because you're calling 'SpawnMob' on a player object. What you want is the world object, probably the world of the player on which you're trying to spawn a monster on. So this line:

Player:SpawnMob(giant)

would have to be replaced with something like this:
Giantee:GetWorld():SpawnMob(Giantee:GetPosX(), Giantee:GetPosY(), Giantee:GetPosZ(), mtGiant, false);
Reply
Thanks given by: Code_Blue
#4
(07-14-2021, 03:15 AM)NiLSPACE Wrote: Hi, welcome to the forum!

The reason it's giving an error is because you're calling 'SpawnMob' on a player object. What you want is the world object, probably the world of the player on which you're trying to spawn a monster on. So this line:

Player:SpawnMob(giant)

would have to be replaced with something like this:
Giantee:GetWorld():SpawnMob(Giantee:GetPosX(), Giantee:GetPosY(), Giantee:GetPosZ(), mtGiant, false);

Doesn't the SpawnMob method take a vector as a position parameter?
Reply
Thanks given by: Code_Blue
#5
I thought so as well, but the documentation showed individual coordinates: https://api.cuberite.org/cWorld.html#SpawnMob_1 But perhaps it's out of date.
Reply
Thanks given by:
#6
It should still work. The vector object just pushes three integers and then the API takes just three integers.

I don‘t think anyone took the time to add this explicitly
Reply
Thanks given by: Code_Blue
#7
Success! Giant spawned.
Thanks for your help everybody!
Reply
Thanks given by:




Users browsing this thread: 3 Guest(s)