Forthwind Posted April 15, 2018 Share Posted April 15, 2018 (edited) Greetings, I've got a small problem with getElementsWithinColShape, the script for some reason doesn't seem to be able to detect if there are any items in the colshape. I've tried some bugfixing myself and the only way I could sort of make it work was by first creating the object and afterwards create the colshape. Any other way of doing it, it tells me there's no object inside the colshape. I've gone through the wiki and all it says is: "Please note that for legacy reasons, a colshape created on the client does not collide with elements already existing at that location until they first move". My code is created in serverside, and the objects are created -after- the colshape is created. On line 30 it checks if checkColz is above 0. It never continues from here because it never counts any object that is inside the colshape. batspawns = {} function allRandomz(source, commandName, dimension) maxBats = 25 spawnedBats = 0 spawnedKnives = 0 spawnedRifles = 0 spawnedShotguns = 0 if (#batspawns == 0) then for i, v in pairs(bats) do checkCol = createColSphere(bats[i][1], bats[i][2], bats[i][3], 3) finalBats = {x = bats[i][1], y = bats[i][2], z = bats[i][3], col = checkCol} table.insert(batspawns, finalBats) end end while spawnedBats < maxBats do randombat = math.random(1, #bats) taken = false for i, v in pairs(batspawns) do if i == randombat and isElement(v['col']) then checkColz = #getElementsWithinColShape(v['col'], 'object') foundDimension = getElementsWithinColShape(v['col'], "object") if checkColz > 0 then outputChatBox("is it different? " .. checkColz) -- Never outputs this, checkColz is always 0 (no elements found in colshape) for i, v in pairs(foundDimension) do if getElementDimension(v) == dimension then taken = true break else taken = false end end elseif checkColz == 0 then taken = false end end end if taken == false then newbat = createObject(336, batspawns[randombat]['x'], batspawns[randombat]['y'], batspawns[randombat]['z']) setElementDimension(newbat, dimension) spawnedBats = spawnedBats + 1 end end end addCommandHandler("spawnit", allRandomz) Edited April 15, 2018 by Forthwind Link to comment
Moderators Patrick Posted April 15, 2018 Moderators Share Posted April 15, 2018 If you create the col, after the object, it's works. I tried it before. Example. local object = createObject(336, 0,0,4) local col = createColSphere(0,0,4,5) addCommandHandler("countit", function() local objects = getElementsWithinColShape(col, "object") outputChatBox(#objects) end) 1 Link to comment
Forthwind Posted April 15, 2018 Author Share Posted April 15, 2018 23 minutes ago, Patrick2562 said: If you create the col, after the object, it's works. I tried it before. Example. local object = createObject(336, 0,0,4) local col = createColSphere(0,0,4,5) addCommandHandler("countit", function() local objects = getElementsWithinColShape(col, "object") outputChatBox(#objects) end) If this's the only way, then I'll revamp my code so it'll create the object first and afterwards the colshape. Thanks. Link to comment
Moderators Patrick Posted April 15, 2018 Moderators Share Posted April 15, 2018 (edited) You want to do an item pickup script? Edited April 15, 2018 by Patrick2562 Link to comment
Forthwind Posted April 15, 2018 Author Share Posted April 15, 2018 Yeah I am doing everything from scratch, I just ran into this problem and couldn't figure out why the colshape couldn't detect the item. (item created after colshape was created). I'm guessing it's not possible for this way to be detected so I'll revamp my code so it'll create the items before the colshape. Long answer short. Yeah, making a pickup script. Link to comment
Moderators Patrick Posted April 15, 2018 Moderators Share Posted April 15, 2018 Just now, Forthwind said: Yeah I am doing everything from scratch, I just ran into this problem and couldn't figure out why the colshape couldn't detect the item. (item created after colshape was created). I'm guessing it's not possible for this way to be detected so I'll revamp my code so it'll create the items before the colshape. Long answer short. Yeah, making a pickup script. And you want to respawn this pickups, with a command. (only with command?) Link to comment
Forthwind Posted April 15, 2018 Author Share Posted April 15, 2018 No, this was just a test run. After everything works fine I'm ofc going to switch over from commands to events. Link to comment
Moderators Patrick Posted April 15, 2018 Moderators Share Posted April 15, 2018 (edited) 16 minutes ago, Forthwind said: No, this was just a test run. After everything works fine I'm ofc going to switch over from commands to events. Try this: -- SERVER SIDE local spawns = { {0,0,4}, {5,0,4}, {10,0,4}, {15,0,4}, {20,0,4}, } local pickups = {} function pickupObject(i) outputChatBox("pickuped! | objectid: "..getElementModel(pickups[i].object)) destroyElement(pickups[i].object) destroyElement(pickups[i].pickupCol) pickups[i] = "pickuped" end function createPickup(i) local object = createObject(336, spawns[i][1], spawns[i][2], spawns[i][3]) local col = createColSphere(spawns[i][1], spawns[i][2], spawns[i][3], 2) pickups[i] = {object = object, pickupCol = col} addEventHandler("onColShapeHit", col, function() pickupObject(i) end) end function respawnPickups() for i, v in ipairs(pickups) do if v == "pickuped" then createPickup(i) end end end addCommandHandler("respawn", respawnPickups) for i, v in ipairs(spawns) do createPickup(i) end Edited April 15, 2018 by Patrick2562 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now