开发者

"Error: attempt to index local 'self' (a nil value)" in string.split function

开发者 https://www.devze.com 2023-04-07 04:00 出处:网络
Quick facts, I got this function from http://lua-users.org/wiki/SplitJoin at the very bottom, and am attempting to use it in the Corona SDK, though I doubt that\'s important.

Quick facts, I got this function from http://lua-users.org/wiki/SplitJoin at the very bottom, and am attempting to use it in the Corona SDK, though I doubt that's important.

function string:split(sSeparator, nMax, bRegexp)
    assert(sSeparator ~= '')
    assert(nMax == nil or nMax >= 1)

    local aRecord = {}

    if self:len() > 0 then
        local bPlain = not bRegexp
        nMax = nMax or -1

        local nField=1 nStart=1
        local nFirst,nLast = self:find(sSeparator, nStart, bPlain)
        while nFirst and nMax ~= 0 do
            aRecord[nField] = self:sub(nStart, nFirst-1)
            nField = nField+1
            nStart = nLast+1
            nFirst,nLast = self:find(sSeparator, nStart, bPlain)
            nMax = nMax-1
        end
        aRecord[nField] = self:sub(nStart)
    end

    return aRecord
end

The input: "1316982303 Searching server"

msglist = string.split(msg, ' ')

Gives me the error in the title. Any ideas? I'm fairly certain it's just the function is out of date.

Edit: lots more code Here's some more from the main.lua file:

multiplayer = pubnub.new({
    publish_key   = "demo",             
    subscribe_key = "demo",             
    secret_key    = nil,                
    ssl           = nil,                -- ENABLE SSL?
    origin        = "pubsub.pubnub.com" -- PUBNUB CLOUD ORIGIN
})


multiplayer:subscribe({
    channel  = "MBPocketChange",
    callback = function(msg)
        -- MESSAGE RECEIVED!!!
        print (msg)
        msglist = string.split(msg, ' ')
        local recipient = msglist[0]  --Get the value
        table.remove(msglist, 0)     --Remove the value from the table.
        local cmdarg = msglist[0]
        table.remove(msglist, 0)
        arglist = string.split(cmdarg, ',')
        local command = arglist[0]
        table.remove(arglist, 0)
        argCount = 1
        while #arglist > 0 do
            argname = "arg" .. argCount
            _G[argname] = arglist[0]
            table.remove(arglist, 0)
            argCount = argCount + 1
        end

Server.py: This is the multiplayer server that sends the necessary info to clients.

import sys
import tornado
import os
from Pubnub import Pubnub

## Initiat Class
pubnub = Pubnub( 'demo', 'demo', None, False )

## Subscribe Example
def receive(message) :
    test = str(message)
    msglist = test.split()
    recipient = msglist.pop(0)
    msg = msglist.pop开发者_如何学编程(0)
    id = msglist.pop(0)
    if id != "server":
        print id
        print msg
        commandHandler(msg,id)
        return True

def commandHandler(cmd,id):
    global needOp
    needOp = False
    global matchListing
    if server is True:
        cmdArgList = cmd.split(',')
        cmd = cmdArgList.pop(0)
        while len(cmdArgList) > 0:
            argument = 1
            locals()["arg" + str(argument)] = cmdArgList.pop(0)
            argument += 1
        if cmd == "Seeking":
            if needOp != False and needOp != id:
                needOp = str(needOp)
                id = str(id)
                pubnub.publish({
                    'channel' : 'MBPocketChange',
                    #Message order is, and should remain:
                    #----------Recipient, Command,Arguments, Sender
                    'message' : needOp + " FoundOp," + id + " server"
                })
                print ("Attempting to match " + id + " with " + needOp + ".")
                needOp = False
                matchListing[needOp] = id
            else:
                needOp = id
                pubnub.publish({
                    'channel' : 'MBPocketChange',
                    #Message order is, and should remain:
                    #----------Recipient, Command,Arguments, Sender
                    'message' : id + ' Searching server'
                })
                print "Finding a match for: " + id
        elif cmd == "Confirm":
            if matchListing[id] == arg1:
                pubnub.publish({
                    'channel' : 'MBPocketChange',
                    #Message order is, and should remain:
                    #----------Recipient, Command,Arguments, Sender
                    'message' : arg1 + ' FoundCOp,' + id + ' server'
                })
                matchListing[arg1] = id
            else:
                pass #Cheater.
        elif cmd == "SConfirm":
            if matchListing[id] == arg1 and matchListing[arg1] == id:
                os.system('python server.py MBPocketChange' + arg1)
                #Here, the argument tells both players what room to join.
                #The room is created from the first player's ID.
                pubnub.publish({
                    'channel' : 'MBPocketChange',
                    #Message order is, and should remain:
                    #----------Recipient, Command,Arguments, Sender
                    'message' : id + ' GameStart,' + arg1 + ' server'
                })
                pubnub.publish({
                    'channel' : 'MBPocketChange',
                    #Message order is, and should remain:
                    #----------Recipient, Command,Arguments, Sender
                    'message' : arg1 + ' GameStart,' + arg1 + ' server'
                })
            else:
                pass #hax
    else:
        pass


def connected():
    pass

try:
    channel = sys.argv[1]
    server = False
    print("Listening for messages on '%s' channel..." % channel)
    pubnub.subscribe({
        'channel'  : channel,
        'connect'  : connected,
        'callback' : receive
    })
except:
    channel = "MBPocketChange"
    server = True
    print("Listening for messages on '%s' channel..." % channel)
    pubnub.subscribe({
        'channel'  : channel,
        'connect'  : connected,
        'callback' : receive
    })

tornado.ioloop.IOLoop.instance().start()


This error message happens if you run:

string.split(nil, ' ')

Double check your inputs to be sure you are really passing in a string.

Edit: in particular, msglist[0] is not the first position in the array in Lua, Lua arrays start at 1.

As an aside, this function was written when the intention that you'd use the colon syntactic sugar, e.g.

msglist=msg:split(' ')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号