• Print

Author Topic: Need a tiny bit of help  (Read 6327 times)

0 Members and 1 Guest are viewing this topic.

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Need a tiny bit of help
« on: August 01, 2015, 06:35:19 am »
Code: Lua
  1. function ulx.forcerespawn( calling_ply, target_plys )
  2.         if calling_ply:Team( TEAM_PRISONER ) then
  3.                 for k, v in pairs( target_plys ) do
  4.                         if v:Alive() then
  5.                                 v:Kill()
  6.                                 v:Spawn()
  7.                         end
  8.                 end
  9.         elseif calling_ply:TEAM( TEAM_PRISONER_DEAD ) then
  10.                 for k, v in pairs( target_plys ) do
  11.                         calling_ply:SetTeam( TEAM_PRISONER )
  12.                         v:Spawn()
  13.                 end
  14.         end
  15. ulx.fancyLogAdmin( calling_ply, "#A respawned #T", target_plys )
  16. end
  17. local forcerespawn = ulx.command( "Utility", "ulx forcerespawn", ulx.forcerespawn, { "!forcerespawn", "!respawn"} )
  18. forcerespawn:addParam{ type=ULib.cmds.PlayersArg }
  19. forcerespawn:defaultAccess( ULib.ACCESS_ADMIN )
  20. forcerespawn:help( "Respawns a player." )

I hope you're familiar with Excl's jailbreak gamemode, and basically what I'm trying to do is when the player has died, the gamemode will put them in the TEAM_PRISONER, and respawn them, as respawning in the team TEAM_PRISONER_DEAD won't work, they'll just stay as a spectator, but it just doesn't work, I get no errors, it just doesn't work as I told it to :(

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need a tiny bit of help
« Reply #1 on: August 01, 2015, 07:48:11 am »
What do you have calling this function?
As it stands now, you manually have to call this function using "ulx forcerespawn", and, the person that calls it has to be a prisoner, and specify players they want to be prisoner dead.
ulx forcerespawn "list,of,players"

(I'm not familiar with jailbreak, or even gamemode functions really, but it's obvious typical Glua so far)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Need a tiny bit of help
« Reply #2 on: August 01, 2015, 07:59:20 am »
What do you have calling this function?
As it stands now, you manually have to call this function using "ulx forcerespawn", and, the person that calls it has to be a prisoner, and specify players they want to be prisoner dead.
ulx forcerespawn "list,of,players"

(I'm not familiar with jailbreak, or even gamemode functions really, but it's obvious typical Glua so far)

I've tryed out this, but this doesn't work either, but it looks cleaner and better.
Code: Lua
  1. function ulx.revive( calling_ply, target_plys )
  2.         if !calling_ply:Alive() and calling_ply:Team( TEAM_PRISONER_DEAD ) then
  3.                 calling_ply:SetTeam( TEAM_PRISONER )
  4.                 calling_ply:Spawn()
  5.                 ulx.fancyLogAdmin( calling_ply, "#A revived #T", target_plys )
  6.         elseif !calling_ply:Alive() and calling_ply:Team( TEAM_GUARD_DEAD ) then
  7.                 calling_ply:SetTeam( TEAM_GUARD )
  8.                 calling_ply:Spawn()
  9.                 ulx.fancyLogAdmin( calling_ply, "#A revived #T", target_plys )
  10.         elseif calling_ply:Team( TEAM_SPECTATOR ) then
  11.                 ULib.tsayError( calling_ply, "This player is in spectator mode, and can't participate in the game!", true )
  12.         else
  13.                 ULib.tsayError( calling_ply, "Something went horribly wrong!", true )
  14.         end
  15. end
  16. local revive = ulx.command( "Utility", "ulx revive", ulx.revive, { "!revive" } )
  17. revive:addParam{ type=ULib.cmds.PlayersArg }
  18. revive:defaultAccess( ULib.ACCESS_ADMIN )
  19. revive:help( "Forces a player into spectator mode." )  

It will respawn the prisoner, if they are dead, and are in the team TEAM_PRISONER_DEAD, but before we respawn the player, we want to set their team, so they will actually respawn in the team they are in, and not randomly spawn in the map.

Same goes for the guards.

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Need a tiny bit of help
« Reply #3 on: August 01, 2015, 04:13:06 pm »
You need for-loops if you're going to accept multiple targets. Parameter target_plys will be a table that contains all player entities that were targeted. You want to use a for-loop to iterate over that table and execute the correct functions on each entity. And make sure you're running these functions on the correct player entities: Right now you're running every function on the caller. (Or you're doing this for debugging purposes?)

This won't work:
Code: Lua
  1. calling_ply:Team( TEAM_PRISONER_DEAD )

As you can see on the wiki, Player:Team() does not take any arguments. Instead, it returns the id of the team the player is currently in. You can still use this function to make sure the player is in the right team but not quite like that. :)

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Need a tiny bit of help
« Reply #4 on: August 02, 2015, 02:33:33 am »
would I have to do target_plys:Team( TEAM_PRISONER_DEAD ) or do I edit the code I first put on the thread?

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Need a tiny bit of help
« Reply #5 on: August 02, 2015, 04:21:26 am »
would I have to do target_plys:Team( TEAM_PRISONER_DEAD )
You can not: target_plys is not a player but a collection of players. You can't run all those player functions like Player:Team() or Player:Alive() on a collection, no. So that's where for-loops come in! They allow us to go over each player entity inside a collection.

Here, use the code below as a base to build the rest of your command. I added in the loop, control structures and some comments. Now it's up to you to add the core functionality.

Code: Lua
  1. function ulx.revive( calling_ply, target_plys )
  2.    
  3.     -- Iterate over collection of targets
  4.     for _, target in ipairs( target_plys ) do
  5.        
  6.         if not target:Alive() then
  7.             -- Do stuff when target is dead
  8.            
  9.             if target:Team() == TEAM_PRISONER_DEAD then
  10.                 -- Do stuff when target is in dead prisoner team
  11.             elseif target:Team() == TEAM_GUARD_DEAD then
  12.                 -- Do stuff when target is in dead guard team
  13.             else
  14.                 -- Do stuff when target is in any other team
  15.             end
  16.  
  17.         else
  18.             -- Do stuff when target is alive
  19.         end
  20.        
  21.     end
  22.    
  23. end
  24. local revive = ulx.command( "Utility", "ulx revive", ulx.revive, { "!revive" } )
  25. revive:addParam{ type=ULib.cmds.PlayersArg }
  26. revive:defaultAccess( ULib.ACCESS_ADMIN )
  27. revive:help( "Respawn a dead player." )

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Need a tiny bit of help
« Reply #6 on: August 04, 2015, 04:25:55 am »
I don't know if this is exactly what you want, but this is some code that I made to respawn anyone. If they are a guard or a prisoner, it respawns them as the correct team in the correct locations. If you try to respawn a spectator, they either go and spectate an alive player or they just teleport to one of the guard or prisoner spawns. player._jb_forceRespawn = true is necessary because otherwise when attempting to respawn a player Jailbreak will just kill them and spawn them in spectate mode instead.

Code: Lua
  1. function ulx.respawn( calling_ply, target_plys )
  2.   local affected_plys = {}
  3.   for i=1, #target_plys do
  4.     local v = target_plys[ i ]
  5.     if !v:Alive() then
  6.       v._jb_forceRespawn = true
  7.       v:Spawn()
  8.       table.insert(affected_plys, v)
  9.     end
  10.   end
  11.   ulx.fancyLogAdmin( calling_ply, "#A respawned #T", affected_plys )
  12. end
  13. local respawn = ulx.command( CATEGORY_NAME, "ulx respawn", ulx.respawn, "!respawn" )
  14. respawn:addParam{ type=ULib.cmds.PlayersArg }
  15. respawn:defaultAccess( ULib.ACCESS_ADMIN )
  16. respawn:help( "Respawns target(s)." )
 
« Last Edit: August 04, 2015, 04:29:20 am by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Need a tiny bit of help
« Reply #7 on: August 07, 2015, 04:07:02 pm »
That doesn't work... :(
Maybe remove the one end at line 10, there's no need for it to be there.

Offline Caustic Soda-Senpai

  • Sr. Member
  • ****
  • Posts: 469
  • Karma: 54
  • <Insert something clever here>
    • Steam Page
Re: Need a tiny bit of help
« Reply #8 on: August 07, 2015, 11:58:17 pm »
Maybe remove the one end at line 10, there's no need for it to be there.

Yes, it needs to be there. The end is what completes the "for" loop...
Once you get to know me, you'll find you'll have never met me at all.

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Need a tiny bit of help
« Reply #9 on: August 08, 2015, 02:15:29 am »
Yes, it needs to be there. The end is what completes the "for" loop...
Oh, I'm not familiar with loops yet... didn't knew that.

but why exactly does the code not work?
It looks fine for me, and there's no errors, it just spawns you in spectator mode, and I think a lot of jailbreak owners would need this.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Need a tiny bit of help
« Reply #10 on: August 08, 2015, 02:39:00 am »
That doesn't work... :(

Saying "that doesn't work" doesn't help other people to figure out WHY it doesn't work so they can help YOU.

Looking at the code now I see I left my CATEGORY_NAME variable in there. I assume that is why it isn't working. Just replace the CATEGORY_NAME on line 13 with a string for the Category name. In my case, it is "Utilities".
« Last Edit: August 08, 2015, 02:40:47 am by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need a tiny bit of help
« Reply #11 on: August 08, 2015, 09:16:28 pm »
Additionally, leaving a variable blank would have indeed caused an error, likely at server startup.
Lua doesn't like nil definitions when you try to load/start them in a function.
Sure, you can assign a variable = nil, but, don't expect to use that variable without defining that and having the code expect that.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Need a tiny bit of help
« Reply #12 on: August 11, 2015, 03:09:40 am »
Saying "that doesn't work" doesn't help other people to figure out WHY it doesn't work so they can help YOU.

Looking at the code now I see I left my CATEGORY_NAME variable in there. I assume that is why it isn't working. Just replace the CATEGORY_NAME on line 13 with a string for the Category name. In my case, it is "Utilities".

I was looking at the code why it didn't work, but I didn't see that one, I'll try work on that now.

Additionally, leaving a variable blank would have indeed caused an error, likely at server startup.
Lua doesn't like nil definitions when you try to load/start them in a function.
Sure, you can assign a variable = nil, but, don't expect to use that variable without defining that and having the code expect that.

Yeah, I've kinda experienced that a lot since I've been working with lua.

"Coding is like a book, except when you miss out a single comma on page 126."

  • Print