Cassandra Posted March 4, 2013 Share Posted March 4, 2013 Well, I want to sync the ped's position, rotation, animation and control state. How will I go about doing that? Link to comment
PaiN^ Posted March 4, 2013 Share Posted March 4, 2013 setPedRotation setElementPosition setPedAnimation Link to comment
DakiLLa Posted March 4, 2013 Share Posted March 4, 2013 and control state getPedControlState Link to comment
Cassandra Posted March 4, 2013 Author Share Posted March 4, 2013 and control state getPedControlState But isn't it clientside? Link to comment
DakiLLa Posted March 4, 2013 Share Posted March 4, 2013 Well, in my opinion, syncing such stuff as peds shouldn't be only a server side task. Link to comment
Cassandra Posted March 4, 2013 Author Share Posted March 4, 2013 Well, in my opinion, syncing such stuff as peds shouldn't be only a server side task. I know but how will it sync between other players if the code is executed clientside only? Link to comment
Jaysds1 Posted March 4, 2013 Share Posted March 4, 2013 well, if you set the control state and it's moving then you could try triggering an event every players computer to set the ped moving, and while it's moving, you could just get the position,rotation, and animation. Link to comment
Cassandra Posted March 4, 2013 Author Share Posted March 4, 2013 Could you possibly show me an example/point me on the right direction on how to sync this? addEventHandler("onClientRender", root, function() local botid for _, bot in ipairs(getElementsByType("ped")) do botid = getElementData(bot, "bot.id") if(botid ~= false) then local px, py, pz = getElementPosition(localPlayer) local x, y, z = getElementPosition(bot) local rot = findRotation(x, y, px, py) local dist = getDistanceBetweenPoints3D(x, y, z, px, py, pz) local mode = getElementData(bot, "bot.mode") local running = getPedControlState(bot, "forwards") local sprinting = getPedControlState(bot, "sprint") local jumping = getPedControlState(bot, "jump") local health = getElementHealth(bot) local clearpath = isLineOfSightClear(px, py, pz, x, y, z, true, true, false, true, false, true, false) if((clearpath == false) and (jumping == false) and ((getTickCount() - lastjumptime) < (500/getGameSpeed()))) then outputChatBox("Jump!") setPedAnimation(bot) setPedControlState(bot, "jump", true) end if((getTickCount() - lastjumptime) > (500/getGameSpeed())) then setPedControlState(bot, "jump", false) lastjumptime = getTickCount() end if(health < 1) then setTimer(destroyElement, 2000, 1, bot) end if(mode == "follow") then setPedRotation(bot, rot) setPedLookAt(bot, px, py, pz) -- Sprinting = Running and Sprinting -- Running = Running if(dist < 1) then -- If they are too close. if((running) or (running and sprinting)) then -- If they are either running or sprinting. --Stop them. outputChatBox("Should stop") setPedControlState(bot, "sprint", false) setPedControlState(bot, "forwards", false) end setPedControlState(bot, "fire", true) elseif(dist > 15.0) then -- If they are too far. if(sprinting == false) then -- If they are not sprinting. --Sprint! outputChatBox("Should sprint.") setPedControlState(bot, "sprint", true) setPedControlState(bot, "forwards", true) end setPedControlState(bot, "fire", false) else -- If they are not too close or too far. if((running and sprinting) or (running == false)) then -- If they are sprinting or idle. --Run! outputChatBox("Should run.") setPedControlState(bot, "sprint", false) setPedControlState(bot, "forwards", true) end setPedControlState(bot, "fire", false) end end end end end) Link to comment
Cassandra Posted March 5, 2013 Author Share Posted March 5, 2013 Sorry for posting repeatedly but how do you sync ped's control state? Link to comment
Cassandra Posted March 6, 2013 Author Share Posted March 6, 2013 Alright managed to sync the ped but I still have a problem. The bot shoots in an awkward angle and I believe it's happening because of the animation or something. The rotation, the bot is facing is alright but the bot doesn't shoot at the player's body location. Server: local id = 0 addCommandHandler("createbot", function(player, command, skin) skin = skin or 28 local x, y, z = getElementPosition(player) id = id + 1 local bot = createPed(28, x, y, z) setElementData(bot, "bot.id", id) setElementData(bot, "bot.mode", "none") setElementData(bot, "bot.action", "none") setElementData(bot, "bot.animation", "none") setElementData(bot, "bot.clear", false) setElementData(bot, "bot.target", player) outputChatBox("Bot Created: " .. tostring(id), player) end) addCommandHandler("givewep", function(player, command, botid, weapon) botid = botid or 1 weapon = weapon or "Knife" local bot = botGet(botid) or outputChatBox("Invalid Bot.", player) local weaponid = getWeaponIDFromName(weapon) or outputChatBox("Invalid Weapon.", player) if(bot and weaponid) then giveWeapon(bot, weaponid, 99999, true) outputChatBox("Bot " .. tostring(botid) .. " given weapon " .. weapon, player) end end) addCommandHandler("setfollow", function(player, command, botid) botid = botid or 1 local bot = botGet(botid) or outputChatBox("Invalid Bot.", player) local x, y, z = getElementPosition(player) local rot = getPedRotation(player) if(bot) then setPedAnimation(bot) setElementData(bot, "bot.mode", "follow") outputChatBox("Bot " .. tostring(botid) .. " set to follow you", player) end end) addEventHandler("onResourceStart", resourceRoot, function() setTimer( function() local botid local px, py, pz local x, y, z local rot local dist local mode local action local animation local clear local target local health for _, bot in ipairs(getElementsByType("ped")) do botid = getElementData(bot, "bot.id") if(botid ~= false) then mode = getElementData(bot, "bot.mode") action = getElementData(bot, "bot.action") animation = getElementData(bot, "bot.animation") clear = getElementData(bot, "bot.clear") target = getElementData(bot, "bot.target") px, py, pz = getElementPosition(target) x, y, z = getElementPosition(bot) rot = findRotation(x, y, px, py) dist = getDistanceBetweenPoints3D(x, y, z, px, py, pz) health = getElementHealth(bot) setPedRotation(bot, rot) --setPedLookAt(bot, x, y, z) if((action == "none") and (animation ~= "none")) then outputChatBox("Playing no animation", target) setElementData(bot, "bot.animation", "none") setPedAnimation(bot) elseif(action == "run") then outputChatBox("Playing running animation", target) setElementData(bot, "bot.animation", "run") setPedAnimation(bot, "ped", "run_civi") elseif(action == "sprint") then outputChatBox("Playing sprinting animation", target) setElementData(bot, "bot.animation", "sprint") setPedAnimation(bot, "ped", "sprint_civi") end if(mode == "follow") then if(dist > 5 and dist < 30) then -- Running Range outputChatBox("Should Run", target) if(clear) then -- Path Clear outputChatBox("Running!", target) setElementData(bot, "bot.action", "run") end elseif(dist > 30) then outputChatBox("Should Sprint", target) if(clear) then outputChatBox("Sprinting!", target) setElementData(bot, "bot.action", "sprint") end else outputChatBox("Should shoot", target) setElementData(bot, "bot.action", "none") if(getPedWeapon(bot) > 0) then triggerClientEvent("cFire", bot) end end end end end end , 50, 0) end) function botGet(botid) for _, bot in ipairs(getElementsByType("ped")) do if(getElementData(bot, "bot.id") == tonumber(botid)) then return bot end end return nil end function findRotation(x1, y1, x2, y2) local t = -math.deg(math.atan2(x2-x1,y2-y1)) if t < 0 then t = t + 360 end; return t; end Client: --[[addEventHandler("onClientPedDamage", root, function(attacker, weapon, bodypart) if(getElementData(attacker, "bot.id") ~= false) then if(weapon == getWeaponIDFromName("Knife")) then if(bodypart == 9) then setPedHeadless(source, true) else setElementHealth(source, getElementHealth(source) - 40) end end else if(bodypart == 9) then setPedHeadless(source, true) setElementHealth(source, 0) end end cancelEvent() end)--]] addEventHandler("onClientRender", root, function() local botid local px, py, pz local x, y, z for _, bot in ipairs(getElementsByType("ped")) do botid = getElementData(bot, "bot.id") px, py, pz = getElementPosition(localPlayer) x, y, z = getElementPosition(bot) if(botid ~= false) then local clearpath = isLineOfSightClear(px, py, pz, x, y, z, true, true, false, true, false, true, false) if(clearpath == false) then setElementData(bot, "bot.clear", false) else setElementData(bot, "bot.clear", true) end end end end) addEvent("cFire", true) addEventHandler("cFire", root, function() setPedControlState(source, "fire", true) setTimer(setPedControlState, 800, 1, source, "fire", false) end) Link to comment
Moderators IIYAMA Posted March 6, 2013 Moderators Share Posted March 6, 2013 Elementdata you only use on server side will lagg you bandwidth. It will give you more code+time, but it will reduce lots of ping lagg in the future. Since you use bots, it will require double+ of players their internet speed. Add to much Element data and you have teleporting peds. Use instead Tables: local botsTable[bot] = {} local botsTable[bot]["bot.animation"] = true -- or anim etc. if botsTable[bot]["bot.animation"] then local anim = botsTable[bot]["bot.animation"] end -- you can also put it in "array"(mta does have a kind of array, but it also not array) when you need it. table.insert (botsTable,bot) --or local i = 1 botsTable[i]=bot -- i = index 1,2,3,4,5 --[[NOTE:]] botsTable[#botsTable+1] = bot --[[]] table.insert(botsTable,bot) botsTable[i]["bot.animation"]= "lol" for i, bot in ipairs(botsTable) do -- botsTable[i] = bot if isElement(bot) and thisBot == bot then outputChatBox(botsTable[i]["bot.animation"]) botsTable[i]["bot.animation"]= "new lol" end end -- clear elements when they got destroyed, don't loop while to much. While can crash your server when your code might be incorrect. for i, bot in ipairs(botsTable) do while botsTable[i] and not isElement(bot) do table.remove (botsTable, i) end end Link to comment
Anderl Posted March 6, 2013 Share Posted March 6, 2013 Just a side note: You can do your script in OOP and create a new element type for bots, so you don't have to loop through all players. Link to comment
Jaysds1 Posted March 13, 2013 Share Posted March 13, 2013 The only way I could think about this, is to trigger server-side then trigger client-side... Link to comment
Moderators IIYAMA Posted March 13, 2013 Moderators Share Posted March 13, 2013 Yes that is the best way. - The information that have been moved from client to server is 5 kb (dunno, but lets make this sample big) - 25 players are in the server. Elementdata: elementdata * playersInServer = bandwidth of all players. 25 * 5kb = 125kb! Server data travel. ( 24 players client download) Trigger: tabledata * 1 = bandwidth of one player. 5 * 1 = 5 kb Server data travel. (no client download) But nobody is ever listening... -_-" BTW(client): https://wiki.multitheftauto.com/wiki/SetPedAimTarget -- get the ped rotation and send this to the server, not sure if the syncs are 100%. even so you can calculate the syncs by the client, move the index number through the server. That would be the smallest way. 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