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
11 | |||||
0 |
Ladders Top 10
-
#Steam AliasWL%Streak
- 1.655231.739+15
- 2.842223.791+5
- 3.35258.859+1
- 4.599234.719+7
- 5.936410.695+2
- 6.278108.720+29
- 7.307114.729+3
- 8.645.928+5
- 9.10629.785+7
- 10.527.881+18
Replay highlight
VS
- cblanco ★
- 보드카 중대
- VonManteuffel
- Heartless Jäger
Einhoven Country
Honor it
11
Download
1263
Board Info
557 users are online:
557 guests
1 post in the last 24h
13 posts in the last week
27 posts in the last month
13 posts in the last week
27 posts in the last month
Registered members: 49985
Welcome our newest member, debetbot
Most online: 2043 users on 29 Oct 2023, 01:04 AM
Welcome our newest member, debetbot
Most online: 2043 users on 29 Oct 2023, 01:04 AM