Hello, I'm trying to learn scar and am a rookie to lua/programming in general. Can someone tell me what is wrong with the following? Note: I'm doing this for CoH1, but I assume it should be roughly the same for CoH2.
The wincondition I'm trying to make is something that balances resource rate based on team size. So if it's a 1v2, then the team with less people will get more resources.
win condition that will balance resources based on team size
29 May 2019, 02:09 AM
#1
Posts: 60
30 May 2019, 14:38 PM
#2
Posts: 756 | Subs: 8
So, the assignment operator is not the same as the equality operator.
should be
Also, unless it just cut off, you don't have an end for your function.
You should really use local variables to store anything that you use more than once. For example, you call World_GetPlayerAt multiple times. Function calls can become expensive in loops. You should also indent for readability. I highly suggest using Visual Studio Code or some other IDE to help with that.
Here is a cleaned up version of your script:
if Player_GetTeam(i)=1 then
should be
if Player_GetTeam(i)==1 then
Also, unless it just cut off, you don't have an end for your function.
You should really use local variables to store anything that you use more than once. For example, you call World_GetPlayerAt multiple times. Function calls can become expensive in loops. You should also indent for readability. I highly suggest using Visual Studio Code or some other IDE to help with that.
Here is a cleaned up version of your script:
g_CheckAnnihilate = true
function OnInit()
team_a = 0
team_b = 0
for i = 1, World_GetPlayerCount() do
if (Player_GetTeam(i) == 1) then
team_a = team_a + 1
else
team_b = team_b + 1
end
end
if (team_a > team_b) then
b_ratio = team_a/team_b
a_ratio = 1
else
a_ratio = team_b/team_a
b_ratio = 1
end
for i = 1, World_GetPlayerCount() do
local player = World_GetPlayerAt(i)
Player_SetPopCapOverride(player, 350)
if (Player_GetTeam(i) == 1) then
Modify_PlayerResourceRate(player, RT_Manpower, a_ratio)
Modify_PlayerResourceRate(player, RT_Munition, a_ratio)
Modify_PlayerResourceRate(player, RT_Fuel, a_ratio)
else
Modify_PlayerResourceRate(player, RT_Manpower, b_ratio)
Modify_PlayerResourceRate(player, RT_Munition, b_ratio)
Modify_PlayerResourceRate(player, RT_Fuel, b_ratio)
end
end
end
Scar_AddInit(OnInit)
31 May 2019, 03:42 AM
#3
Posts: 60
Thank you so much for the response! This makes sense. Ah I feel embarrassed, you are right I am missing an end on the function.
After I've added the clean version of your code, I still get a fatal scar error. The only additional line that I didn't include in the original post was
import("ScarUtil.scar")
at the beginning of the code. Note, that I did this in CoH1 if that adds any nuance.
Another fundamental question, when would I need to put something in parenthesis and when wouldn't I? I don't quite understand why you put parenthesis around the if condition (Player_GetTeam(i) == 1)
After I've added the clean version of your code, I still get a fatal scar error. The only additional line that I didn't include in the original post was
import("ScarUtil.scar")
at the beginning of the code. Note, that I did this in CoH1 if that adds any nuance.
Another fundamental question, when would I need to put something in parenthesis and when wouldn't I? I don't quite understand why you put parenthesis around the if condition (Player_GetTeam(i) == 1)
31 May 2019, 05:09 AM
#4
Posts: 756 | Subs: 8
Yeah, you always need that import due to maps using the functions it provides.
You don't need to use parentheses, but it's not a bad habit to get into if you ever wanted to program in a different language. It is entirely optional in Lua, so up to you in the end. I just did so out of habit and I like the way it looks. *shrug*
You don't need to use parentheses, but it's not a bad habit to get into if you ever wanted to program in a different language. It is entirely optional in Lua, so up to you in the end. I just did so out of habit and I like the way it looks. *shrug*
31 May 2019, 05:14 AM
#5
Posts: 756 | Subs: 8
Also, just noticed something else:
should be
Since it expects a player and not a number.
Player_GetTeam(i)
should be
Player_GetTeam(player)
Since it expects a player and not a number.
PAGES (1)
1 user is browsing this thread:
1 guest
Livestreams
118 | |||||
29 | |||||
9 | |||||
1 | |||||
63 | |||||
54 | |||||
17 | |||||
7 | |||||
1 | |||||
1 |
Ladders Top 10
-
#Steam AliasWL%Streak
- 1.829222.789+35
- 2.34857.859+13
- 3.587233.716+3
- 4.1095612.641+19
- 5.882398.689+4
- 6.280162.633+8
- 7.996645.607+4
- 8.379114.769+1
- 9.300113.726-1
- 10.717439.620+1
Replay highlight
VS
- cblanco ★
- 보드카 중대
- VonManteuffel
- Heartless Jäger
Einhoven Country
Honor it
9
Download
999
Board Info
701 users are online:
701 guests
3 posts in the last 24h
4 posts in the last week
23 posts in the last month
4 posts in the last week
23 posts in the last month
Registered members: 48722
Welcome our newest member, asherllc
Most online: 2043 users on 29 Oct 2023, 01:04 AM
Welcome our newest member, asherllc
Most online: 2043 users on 29 Oct 2023, 01:04 AM