Results 1 to 7 of 7

Thread: This global spawnObject script won't run the callback function

  1. #1

    This global spawnObject script won't run the callback function

    Here's a snippet of my script in which I spawn an object with a callback parameter, but it's not running the callback function at all. Note that this is on the global script, and in this case, i = 1 so v = "$10,000", and bets = 10000. So it should spawn one stack of benjamins with the name "$10,000".

    Code:
    	updatePotPosition()
    	local r = potobject.getRotation()
    	r.x = 0
    	r.z = 0
    	local values = {"$10,000", "$5,000", "1000", "500", "100"}
    	local benjamins = {["mesh"] = 'http://pastebin.com/raw.php?i=iPCMFDe4', ["diffuse"] = 'https://675c7842d189adfeb2f8a165972e0fe8aa64f028.googledrive.com/host/0B5tpZ_GxRg4IZk83Ync0YVRxNm8/hundred_stack_wrapped.jpg'}
    	local grants = {["mesh"] = 'http://pastebin.com/raw.php?i=iPCMFDe4', ["diffuse"] = 'https://675c7842d189adfeb2f8a165972e0fe8aa64f028.googledrive.com/host/0B5tpZ_GxRg4IZk83Ync0YVRxNm8/fifty_stack_wrapped.jpg'}
    	local params = {}
    	local custom = {}
    	params.rotation = r
    		
    	for i, v in ipairs (values) do
    		
    		if i == 1 then
    			params.type = 'Custom_Model'
    			params.scale = {0.75, 0.75, 0.75}
    			params.callback = 'applyName'
    			params.params = {v}
    			custom.mesh = benjamins.mesh
    			custom.diffuse = benjamins.diffuse
    			custom.type = 5
    			custom.material = 1
    			
    			while bets >= tonumber(v) do
    				updatePotPosition()
    				params.position = mainpotchips["misc"]
    				local obj = spawnObject(params)
    				obj.setCustomObject(custom)
    				bets = bets - tonumber(v)
    				mainpotchips["misc"].height = mainpotchips["misc"].height + (mainpotchips["misc"].yoffset * 2)
    			end				
    		end
    		...
    	end
    And this is the callback function:

    Code:
    function applyName(object, pname)
    	print ("callback")
    	object.setName(pname[1])
    end
    I have almost this exact same code on an object, where callback_owner = self, and it works perfectly fine. I've tried also using callback_owner = nil and callback_owner = Global, and even callback_owner = an object with the same function.

    To me, the issue appears to be related to the script being on the global script. Here, I varied the script only slightly and pasted it on a random object. Upon copy/pasting that object, it properly spawns the stack of benjamins with the name "$10,000". The changes I made are: removed references to external variables, set position to {0, 5, 0} and rotation to {0, 0, 0}, added local bets variable equal to 10000, and added params.callback_owner = self.
    Code:
    function onload()
    	local values = {"$10,000", "$5,000", "1000", "500", "100"}
    	local benjamins = {["mesh"] = 'http://pastebin.com/raw.php?i=iPCMFDe4', ["diffuse"] = 'https://675c7842d189adfeb2f8a165972e0fe8aa64f028.googledrive.com/host/0B5tpZ_GxRg4IZk83Ync0YVRxNm8/hundred_stack_wrapped.jpg'}
    	local grants = {["mesh"] = 'http://pastebin.com/raw.php?i=iPCMFDe4', ["diffuse"] = 'https://675c7842d189adfeb2f8a165972e0fe8aa64f028.googledrive.com/host/0B5tpZ_GxRg4IZk83Ync0YVRxNm8/fifty_stack_wrapped.jpg'}
    	local params = {}
    	local custom = {}
    	params.rotation = {0, 0, 0}
    	local bets = 10000
    		
    	for i, v in ipairs (values) do
    		
    		if i == 1 then
    			params.type = 'Custom_Model'
    			params.scale = {0.75, 0.75, 0.75}
    			params.callback = 'applyName'
    			params.callback_owner = self
    			params.params = {v}
    			custom.mesh = benjamins.mesh
    			custom.diffuse = benjamins.diffuse
    			custom.type = 5
    			custom.material = 1
    			
    			while bets >= tonumber(v) do
    				params.position = {0, 5, 0}
    				local obj = spawnObject(params)
    				obj.setCustomObject(custom)
    				bets = bets - tonumber(v)
    				
    			end				
    		end
    		
    	end
    end
    
    function applyName(object, pname)
    	print ("callback")
    	object.setName(pname[1])
    end
    Last edited by Skorpion9x; 08-17-2016 at 02:10 AM.

  2. #2
    Thanks to the convenient necro of another thread, I learned that I can apply a name to a spawned object without using a callback function, so I've got it working now. But I'd still like to know why the callback function doesn't work, and would prefer to use it if it's something that can be fixed, just to be safe.

  3. #3
    Join Date
    May 2016
    Posts
    1,072
    I tested this script. It works just fine to run the function in global. All of this code was in global. I made a bag with 3 random objects in it. It printed as expected.

    Code:
    function onload()
        bag = getObjectFromGUID('3423fa')
        bag.takeObject({
            position = {0,5,0},
            callback = "functionName",
            callback_owner = Global,
            params = {"cat", "hat", "sat"}
        })
    end
    
    function functionName(objectTaken, callbackTable)
        print(objectTaken)
        for i, v in pairs(callbackTable) do print(v) end
    end
    If I had to guess what your problem was, it may have been something minor. Like forgetting to capitalize Global or some such. At least that is what usually gets me. In your example code (first batch), for instance, you didn't have a callback_owner set, so it was trying to activate that callback function in the bag's scripting (I think that is the default.). If you add in a callback_owner = Global, I imagine it should work fine.
    Last edited by MrStump; 08-16-2016 at 10:22 PM.

  4. #4
    The API says that it defaults to Global if a callback_owner is not defined, so it should work without that line. But I did say that I tried callback_owner = global in the OP as well. However, you gave me the idea to test the script in a fresh save, and it does work there. So there's probably some kind of conflict in the original code somewhere.

  5. #5
    Join Date
    May 2016
    Posts
    1,072
    From the knowledge base:

    Code:
    params.callback_owner = Object callback_owner –Optional. Defaults to the container that the Object was taken from.
    Not sure what other issue you had, but callback_owner was def required. Anyway, glad it is working for you!
    Last edited by MrStump; 08-17-2016 at 10:22 AM.

  6. #6
    Quote Originally Posted by MrStump View Post
    From the knowledge base:

    Code:
    params.callback_owner = Object callback_owner –Optional. Defaults to the container that the Object was taken from.
    Not sure what other issue you had, but callback_owner was def required. Anyway, glad it is working for you!
    You're looking at takeObject. I'm talking about spawnObject.

  7. #7
    Join Date
    May 2016
    Posts
    1,072
    Ah, sorry, my mistake. I haven't ever used spawnObject, didn't realize it has a callback (although it makes sense)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •