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
65 | |||||
40 | |||||
39 | |||||
16 | |||||
12 | |||||
6 | |||||
1 | |||||
640 | |||||
12 | |||||
1 |
Ladders Top 10
-
#Steam AliasWL%Streak
- 1.653231.739+13
- 2.838223.790+1
- 3.35057.860+15
- 4.590233.717+6
- 5.278108.720+29
- 6.306114.729+2
- 7.645.928+5
- 8.922406.694+1
- 9.1118621.643-1
- 10.265138.658+2
Replay highlight
VS
- cblanco ★
- 보드카 중대
- VonManteuffel
- Heartless Jäger
Einhoven Country
Honor it
10
Download
1243
Board Info
614 users are online:
614 guests
0 post in the last 24h
2 posts in the last week
28 posts in the last month
2 posts in the last week
28 posts in the last month
Registered members: 49388
Welcome our newest member, KETTA
Most online: 2043 users on 29 Oct 2023, 01:04 AM
Welcome our newest member, KETTA
Most online: 2043 users on 29 Oct 2023, 01:04 AM