@[cB]SplatterGuts: The file you running is getting executed on the player itself so you have to use self too
You only can use that way if you sending the player from another thread.
@[cB]SplatterGuts: The file you running is getting executed on the player itself so you have to use self too
You only can use that way if you sending the player from another thread.
Wow, that's awesome Smithy. Thanks for your help. I added local.player = self and the check is working. Everything is good now!
My script right now is just 700 if statements, and I know it can be condensed, but I don't have the knowledge to do that. Here it is if you wanted to take a look (in .txt since this site doesn't allow .scr uploads) :
It makes reference to this tool:
help.png
which assigns variables for cb_*weapon*_give, _allow, _default, _swap
weapontrade.txt
Yikes! Yeah, that ain't pretty :P.
Why is the swap cvar set as the weapon class name and not just a boolean 1 or 0? The statements seem in the wrong order and there is way too much duplication. You're also evaluating a hell of a lot of statements unnecessarily because there's no 'else if' or 'else'.
But I'll take a look properly later.
I never claimed to be good at this lol. I was using a string vs a boolean because I wanted multiple options for the swap variable. I want to be able to pick if the Sniper or Rifle or Rocket or Shotgun or MG or SMG are the swap weapon without having to make 6 checkboxes on the menu. This was the best way I knew how.
If you wouldn't mind showing me the proper way to do one of those checks, I'd really appreciate it.
It's alright, most of us have been where you're at. I'm sure there will be a time in future where you'll give the same reaction to someone else :P.
I'm sure even better people than me do the same to my stuff. It's a process.
I suppose what I didn't understand is why you're checking all the weapon swap cvars have the player's current weapon class as the string, instead of checking for all weapon classes like:
if(getcvar(cb_rifle_swap) == "Rifle")
{
// code
}
else if(getcvar(cb_rifle_swap) == "Sniper")
{
//code
}
else if(getcvar(cb_rifle_swap) == "SMG")
{
//code
}
You're also swapping them with the opposite team's weapons which confused me. No swapping for your own team's weapons?
Do you need to give players the weapon they already have if allow is 1? And with default weapons if it's 0, do you want to take everything and then provide the default weapon? Because currently it looks like they will keep their current one as well as the default.
Edit, just put this together for weapontrade.scr, won't be exactly what you want yet but almost there. Try it out:
main:
local.player = self
if(local.player.freshspawn == 0)
end
iprintln "fresh 1 in wep"
// set class names
local.weaponclass[1] = "rifle"
local.weaponclass[2] = "sniper"
local.weaponclass[3] = "smg"
local.weaponclass[4] = "mg"
local.weaponclass[5] = "rocket"
local.weaponclass[6] = "grenade"
local.weaponclass[7] = "shotgun"
local.weaponclass[8] = "pistol"
// set model names
local.weapon[local.weaponclass[1]][allies] = "models/weapons/m1_garand.tik"
local.weapon[local.weaponclass[2]][allies] = "models/weapons/springfield.tik"
local.weapon[local.weaponclass[3]][allies] = "models/weapons/thompsonsmg.tik"
local.weapon[local.weaponclass[4]][allies] = "models/weapons/bar.tik"
local.weapon[local.weaponclass[5]][allies] = "models/weapons/bazooka.tik"
local.weapon[local.weaponclass[6]][allies] = "models/weapons/m2frag_grenade.tik"
local.weapon[local.weaponclass[7]][allies] = "models/weapons/shotgun.tik"
local.weapon[local.weaponclass[8]][allies] = "models/weapons/colt45.tik"
local.weapon[local.weaponclass[1]][axis] = "models/weapons/kar98.tik"
local.weapon[local.weaponclass[2]][axis] = "models/weapons/kar98sniper.tik"
local.weapon[local.weaponclass[3]][axis] = "models/weapons/mp40.tik"
local.weapon[local.weaponclass[4]][axis] = "models/weapons/mp44.tik"
local.weapon[local.weaponclass[5]][axis] = "models/weapons/panzerschreck.tik"
local.weapon[local.weaponclass[6]][axis] = "models/weapons/steilhandgranate.tik"
local.weapon[local.weaponclass[7]][axis] = "models/weapons/shotgun.tik"
local.weapon[local.weaponclass[8]][axis] = "models/weapons/p38.tik"
local.team = local.player.dmteam
// get the player's gun
waitframe
local.weap_targetname = "w" + local.player.entnum
local.player weaponcommand dual targetname (local.weap_targetname)
local.weap = $(local.weap_targetname).model
// check if we need to swap any weapons out or set a default
for (local.i = 1; local.i <= local.weaponclass.size; local.i++)
{
local.class = local.weaponclass[local.i]
local.item = local.weapon[local.class][local.team]
if (getcvar("cb_" + local.class + "_swap") == "1")
{
local.player takeall // swap, give extras, and then end
local.player give local.item
local.player use local.item
waitthread GiveExtras local.weaponclass local.weapon local.weap local.player local.team
$(local.weap_targetname).targetname = NULL
end
}
if (getcvar("cb_" + local.class + "_default") == "1")
{
local.default_weapon = local.item
}
}
// no swaps found, proceed
// find the player's weapon class
for (local.i = 1; local.i <= local.weaponclass.size; local.i++)
{
local.class = local.weaponclass[local.i]
local.item = local.weapon[local.class][local.team]
if (local.item == local.weap && local.class != "grenade" && local.class != "pistol")
{
// found the player's weapon class, check if they are allowed
if (getcvar("cb_" + local.class + "_allow" ) == "0")
{
local.player takeall // not allowed, assign a default weapon
local.player give local.default_weapon
local.player use local.default_weapon
}
waitthread GiveExtras local.weaponclass local.weapon local.weap local.player local.team
break
}
}
$(local.weap_targetname).targetname = NULL // clear targetname so we can reuse
end
GiveExtras local.weaponclass local.weapon local.weap local.player local.team:
for (local.i = 1; local.i <= local.weaponclass.size; local.i++)
{
local.class = local.weaponclass[local.i]
local.item = local.weapon[local.class][local.team]
if (local.class != "grenade")
{
if (getcvar("cb_" + local.class + "_give") == "1")
{
if (local.item != local.weap) // make sure it isn't our current weapon
{
local.player give local.item
}
}
else if (getcvar("cb_" + local.class + "_give") == "0" && local.class == "pistol")
{
local.player take local.item // take the pistol if we are not allowed to give it
}
}
else
{
local.player give local.item // we give grenades by default, no checks needed
}
}
end
I'm more than happy to explain what it's doing in more detail if needs be.
But to help you in your own way of doing it, an example of unnecessary duplication is like the following:
if(getcvar(cb_rifle_allow) == "1")
{
self give local.weap
self use local.weap
waitframe
self give models/weapons/m2frag_grenade.tik
if(getcvar(cb_sniper_give) == "1")
{
self give local.springfield
}
if(getcvar(cb_smg_give) == "1")
{
self give local.thompsonsmg
}
if(getcvar(cb_mg_give) == "1")
{
self give local.bar
}
if(getcvar(cb_rocket_give) == "1")
{
self give local.bazooka
}
if(getcvar(cb_shotgun_give) == "1")
{
self give local.shotgun
}
}
if(getcvar(cb_rifle_allow) == "0")
{
self give local.default_weapon
self use local.default_weapon
waitframe
self give models/weapons/m2frag_grenade.tik
if(getcvar(cb_sniper_give) == "1")
{
self give local.springfield
}
if(getcvar(cb_smg_give) == "1")
{
self give local.thompsonsmg
}
if(getcvar(cb_mg_give) == "1")
{
self give local.bar
}
if(getcvar(cb_rocket_give) == "1")
{
self give local.bazooka
}
if(getcvar(cb_shotgun_give) == "1")
{
self give local.shotgun
}
}
The give and use lines are the only differences between those two blocks of code. I assume that either one of those blocks will always execute, so the majority of that code will always be read. In that case, does it need to be in both those if statements? With the duplication removed it behaves in the same way:
if(getcvar(cb_rifle_allow) == "1")
{
self give local.weap
self use local.weap
}
else
{
self give local.default_weapon
self use local.default_weapon
}
self give models/weapons/m2frag_grenade.tik
if(getcvar(cb_sniper_give) == "1")
{
self give local.springfield
}
if(getcvar(cb_smg_give) == "1")
{
self give local.thompsonsmg
}
if(getcvar(cb_mg_give) == "1")
{
self give local.bar
}
if(getcvar(cb_rocket_give) == "1")
{
self give local.bazooka
}
if(getcvar(cb_shotgun_give) == "1")
{
self give local.shotgun
}
That alone will cut many lines. Imagine the same changes you could make elsewhere?
You also don't need two separate blocks for allies and axis so that already halves your code. So what I mean is, because the player has an inbuilt property that gives you their team, you could create your arrays of weapons with that in mind, calling one set 'axis' and one set 'allies' like I did elsewhere above.
For example:
local.rifle[allies] = "models/weapons/m1_garand.tik"
local.sniper[allies] = "models/weapons/springfield.tik"
local.smg[allies] = "models/weapons/thompsonsmg.tik"
local.mg[allies] = "models/weapons/bar.tik"
local.rocket[allies] = "models/weapons/bazooka.tik"
local.grenade[allies] = "models/weapons/m2frag_grenade.tik"
local.shotgun[allies] = "models/weapons/shotgun.tik"
local.pistol[allies] = "models/weapons/colt45.tik"
local.rifle[axis] = "models/weapons/kar98.tik"
local.sniper[axis] = "models/weapons/kar98sniper.tik"
local.smg[axis] = "models/weapons/mp40.tik"
local.mg[axis] = "models/weapons/mp44.tik"
local.rocket[axis] = "models/weapons/panzerschreck.tik"
local.grenade[axis] = "models/weapons/steilhandgranate.tik"
local.shotgun[axis] = "models/weapons/shotgun.tik"
local.pistol[axis] = "models/weapons/p38.tik"
Then elsewhere do:
if(getcvar(cb_sniper_give) == "1")
{
self give local.sniper[self.dmteam]
}
if(getcvar(cb_smg_give) == "1")
{
self give local.smg[self.dmteam]
}
if(getcvar(cb_mg_give) == "1")
{
self give local.mg[self.dmteam]
}
if(getcvar(cb_rocket_give) == "1")
{
self give local.rocket[self.dmteam]
}
if(getcvar(cb_shotgun_give) == "1")
{
self give local.shotgun[self.dmteam]
}
self.dmteam will automatically resolve to either "axis" or "allies" depending on their team, so you're letting the game do more of the work for you.
Last edited by 1337Smithy; January 4th, 2019 at 04:17 AM.
Wow this is amazing. I'm looking forward to checking it out when I get home.
I have a few questions for you if you don't mind -
If "cb_rocket_give" is 0 for example, I would want the rocket to be taken away from the player. So I would just update this line:
else if (getcvar("cb_" + local.class + "_give") == "0" && local.class == "pistol")
{
local.player take local.item // take the pistol if we are not allowed to give it
}
To be this, right? (Remove the check to see if it's a pistol)
else if (getcvar("cb_" + local.class + "_give") == "0")
{
local.player take local.item // take the weapon if we are not allowed to give it
}
The second question I have is related to the _swap variable. The idea with this, is if cb_rifle_swap = rocket, then when I pick a rocket, I get the other teams rifle. If cb_sniper_swap = shotgun, when I pick a shotgun I get the other teams sniper. That's why I was checking the weapon of each player
if(local.weap = local.rocket)
{
if(getcvar(cb_rifle_swap == "Rocket"))
{
if(self.dmteam == "allies")
{
self give local.kar98
}
else if(self.dmteam == "axis")
{
self give local.m1_garand
}
}
There's already a check in the scmd for turning cb_rocket_allow and cb_rocket_give off when you set cb_anything_swap to rocket, so that shouldn't ever be an issue..
Is it possible to set something like
if(local.team = allies)
{
local.swapteam = axis
}
else if(local.team = axis)
{
local.swapteam = allies
}
then do the swap like this?
// check if we need to swap any weapons out or set a default
for (local.i = 1; local.i <= local.weaponclass.size; local.i++)
{
local.class = local.weaponclass[local.i]
local.item = local.weapon[local.class][local.swapteam]
if (getcvar("cb_" + local.class + "_swap") == "1")
{
local.player takeall // swap, give extras, and then end
local.player give local.item
local.player use local.item
waitthread GiveExtras local.weaponclass local.weapon local.weap local.player local.team
$(local.weap_targetname).targetname = NULL
end
}
if (getcvar("cb_" + local.class + "_default") == "1")
{
local.default_weapon = local.item
}
}
If that's a possibilty, then it looks like everything I want is right here? This is really awesome. I appreciate your instruction! That's way more valuable than "heres a script, good luck" haha this is great.
Hmm. On second glance, there's still something missing.
I'd need to have the variable here:
if (getcvar("cb_" + local.class + "_swap") == "1")
equal the classname (rifle, sniper, smg, mg, rocket, shotgun), then do a check below it to see if the player picked the weapon that matches.
An easier solution might just be to update my menu to show "Swap with Rifle" "Swap with Sniper"... for each weapon, so the variables are just set to 1 or 0, then I think the script I modified above would work? What are your thoughts? Is it too difficult to implement the way I'm thinking?
Yep, but we can just use 'else' as that expression is assumed if it isn't 1 :
else
{
local.player take local.item // take the weapon if we are not allowed to give it
}
Ah OK, I see.
Yep . In fact, I set aside a similar one as I thought you'd probably want it haha:
// get player's current team and the opposing team
local.team = local.player.dmteam
if (local.team = "allies")
{
local.opposite_team = "axis"
}
else
{
local.opposite_team = "allies"
}
As for your other queries, it's fairly easy I think as if I understand you right, I'd already done the logic elsewhere for the swap stuff. Here's the script with your instructions included as I understood them:
main:
local.player = self
if(local.player.freshspawn == 0)
end
iprintln "fresh 1 in wep"
// set class names
local.weaponclass[1] = "rifle"
local.weaponclass[2] = "sniper"
local.weaponclass[3] = "smg"
local.weaponclass[4] = "mg"
local.weaponclass[5] = "rocket"
local.weaponclass[6] = "grenade"
local.weaponclass[7] = "shotgun"
local.weaponclass[8] = "pistol"
// set model names
local.weapon[local.weaponclass[1]][allies] = "models/weapons/m1_garand.tik"
local.weapon[local.weaponclass[2]][allies] = "models/weapons/springfield.tik"
local.weapon[local.weaponclass[3]][allies] = "models/weapons/thompsonsmg.tik"
local.weapon[local.weaponclass[4]][allies] = "models/weapons/bar.tik"
local.weapon[local.weaponclass[5]][allies] = "models/weapons/bazooka.tik"
local.weapon[local.weaponclass[6]][allies] = "models/weapons/m2frag_grenade.tik"
local.weapon[local.weaponclass[7]][allies] = "models/weapons/shotgun.tik"
local.weapon[local.weaponclass[8]][allies] = "models/weapons/colt45.tik"
local.weapon[local.weaponclass[1]][axis] = "models/weapons/kar98.tik"
local.weapon[local.weaponclass[2]][axis] = "models/weapons/kar98sniper.tik"
local.weapon[local.weaponclass[3]][axis] = "models/weapons/mp40.tik"
local.weapon[local.weaponclass[4]][axis] = "models/weapons/mp44.tik"
local.weapon[local.weaponclass[5]][axis] = "models/weapons/panzerschreck.tik"
local.weapon[local.weaponclass[6]][axis] = "models/weapons/steilhandgranate.tik"
local.weapon[local.weaponclass[7]][axis] = "models/weapons/shotgun.tik"
local.weapon[local.weaponclass[8]][axis] = "models/weapons/p38.tik"
// get player's current team and the opposing team
local.team = local.player.dmteam
if (local.team == "allies")
{
local.opposite_team = "axis"
}
else
{
local.opposite_team = "allies"
}
// get the player's gun
waitframe
local.weap_targetname = "w" + local.player.entnum
local.player weaponcommand dual targetname (local.weap_targetname)
if ($(local.weap_targetname) != NULL)
{
local.weap = $(local.weap_targetname).model
}
else
{
end // player isn't holding a weapon
}
// check if we need to swap any weapons out or set a default
for (local.i = 1; local.i <= local.weaponclass.size; local.i++)
{
local.class = local.weaponclass[local.i]
local.item = local.weapon[local.class][local.team]
if (getcvar("cb_" + local.class + "_default") == "1")
{
local.default_weapon = local.item
}
if (local.weapon[local.class][local.opposite_team] == local.weap)
{
// his weapon equals that of the opposing team, so he's already swapped out
// this may never be a scenario but I'm just on the defence (plus it may help for testing purposes)
$(local.weap_targetname).targetname = NULL
end
}
if (local.item == local.weap)
{
local.player_class = local.class // found the player's class
if (local.player_class != "grenade" && local.player_class != "pistol")
{
// now check if anything need swapping
for (local.j = 1; local.j <= local.weaponclass.size; local.j++)
{
local.class = local.weaponclass[local.j]
if (getcvar("cb_" + local.class + "_swap") == local.player_class)
{
local.item = local.weapon[local.class][local.opposite_team]
local.player takeall // swap, give extras, and then end
local.player give local.item
local.player use local.item
waitthread GiveExtras local.weaponclass local.weapon local.weap local.player local.team
$(local.weap_targetname).targetname = NULL
end
}
}
}
}
}
// no swaps found, proceed to check if player's class is allowed
if (getcvar("cb_" + local.player_class + "_allow" ) == "0")
{
local.player takeall // not allowed, assign a default weapon
local.player give local.default_weapon // ** should we hard-code a default default_weapon if it is null?? **
local.player use local.default_weapon
local.weap = local.default_weapon // this is now his weapon
}
waitthread GiveExtras local.weaponclass local.weapon local.weap local.player local.team
$(local.weap_targetname).targetname = NULL // clear targetname so we can reuse
end
GiveExtras local.weaponclass local.weapon local.weap local.player local.team:
for (local.i = 1; local.i <= local.weaponclass.size; local.i++)
{
local.class = local.weaponclass[local.i]
local.item = local.weapon[local.class][local.team]
if (local.class != "grenade")
{
if (local.item != local.weap)
{
if (getcvar("cb_" + local.class + "_give") == "1")
{
local.player give local.item
}
else
{
local.player take local.item
}
}
}
else
{
local.player give local.item // we give grenades by default, no checks needed
}
}
end
Test it to your heart's content and let me know if I've forgotten anything or done something wrong.
Last edited by 1337Smithy; January 4th, 2019 at 01:53 PM.
I'm excited to test this out. Line by line, the script makes sense and seems like it will do everything I want. Thnk you again for your help. The comments are very useful.
One thing I don't quite understand is how the giveextras thread will work. For example if I'm playing in dual rifle mode so players get both a rifle and a sniper (cb_rifle_allow=1 and cb_sniper_allow=1) it looks like right now if I pick a rifle and a rifle is set to allow, I'll get the rifle. Is there a check for giving weapons that aren't the current weapon class?
I really appreciate the assistance again, I'm thrilled to be learning how to never make a script with 700 if statements again