Scripting lua trying get WHILE

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Deviss
Master of the Force
Master of the Force
Posts: 3772
Joined: Tue Aug 12, 2008 7:59 pm
Projects :: Clone Wars Extended
Games I'm Playing :: BF2

Scripting lua trying get WHILE

Post by Deviss »

hi there :D yeah i am trying learn scripting thanks to myers and i am trying While humancrachter enter on barcspeeder do addhealth so i made this code:

Code: Select all

onfirstspawn = OnCharacterEnterVehicle(
        function(player, vehicle)
            while IsCharacterHuman(player) and GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") do
                SetProperty(GetCharacterVehicle(player), "AddHealth", 200)
            end
        end
    )
and when i use barcspeeder bf2 crash and not message of severity asociated to it :S please help :)
User avatar
bobfinkl
Rebel Colonel
Rebel Colonel
Posts: 593
Joined: Sun Jul 13, 2008 9:01 am
Projects :: Lots of unreleased stuff
Games I'm Playing :: Life
xbox live or psn: No gamertag set
Location: The quaint little city gametoast.

Re: scripting lua trying get WHILE

Post by bobfinkl »

Code: Select all

onfirstspawn = OnCharacterEnterVehicle(
        function(player, vehicle)
            while IsCharacterHuman(player) and GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") do
                SetProperty(GetCharacterVehicle(player), "AddHealth", 200)
            end
        end
    )
Should be:

Code: Select all

onfirstspawn = OnCharacterEnterVehicle(
        function(player, vehicle)
            if IsCharacterHuman(player) and GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
                SetProperty(GetCharacterVehicle(player), "AddHealth", 200)
            end
        end
    )
Remember, there is no such thing as a "While Do" it's always "If Then", and before you go making up code look up what it should be first.
myers73
Lieutenant General
Lieutenant General
Posts: 690
Joined: Fri Apr 03, 2009 11:04 pm
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set
Location: Atlanta, GA xfire=myers73 IngameName=mYers

Re: scripting lua trying get WHILE

Post by myers73 »

he want it to only have addhealth when you are in it. Better to just also add a function for if the vehicle is empty to give it AddHealth -200
Deviss
Master of the Force
Master of the Force
Posts: 3772
Joined: Tue Aug 12, 2008 7:59 pm
Projects :: Clone Wars Extended
Games I'm Playing :: BF2

Re: scripting lua trying get WHILE

Post by Deviss »

myers73 wrote:he want it to only have addhealth when you are in it. Better to just also add a function for if the vehicle is empty to give it AddHealth -200
i dont know how make it without while and i dont know how using your way :S
User avatar
Maveritchell
Jedi Admin
Jedi Admin
Posts: 7366
Joined: Mon Aug 21, 2006 11:03 pm

Re: scripting lua trying get WHILE

Post by Maveritchell »

bobfinkl wrote:Remember, there is no such thing as a "While Do" it's always "If Then", and before you go making up code look up what it should be first.
"While" is a conditional statement just like "if," and yes, it's perfectly valid. The original code posted above doesn't have a break condition to the while loop, and it's probably getting some kind of overflow error. I wouldn't use "while" to define perpetual states of being like the original post's code does - only use while to iterate through something with definable steps.

To fix the code, make two conditional statements, one for when a character is on the vehicle and one for when it is not.
Deviss
Master of the Force
Master of the Force
Posts: 3772
Joined: Tue Aug 12, 2008 7:59 pm
Projects :: Clone Wars Extended
Games I'm Playing :: BF2

Re: scripting lua trying get WHILE

Post by Deviss »

Maveritchell wrote:To fix the code, make two conditional statements, one for when a character is on the vehicle and one for when it is not.
i tried with help of myers but i didnt can get it, any idea suggestion or little lines :D?
myers73
Lieutenant General
Lieutenant General
Posts: 690
Joined: Fri Apr 03, 2009 11:04 pm
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set
Location: Atlanta, GA xfire=myers73 IngameName=mYers

Re: Scripting lua trying get WHILE

Post by myers73 »

hey, hit me up on xfire tomorrow afternoon and ill help. working in the morning though.
mswf
Master Bounty Hunter
Master Bounty Hunter
Posts: 1674
Joined: Tue Mar 31, 2009 3:40 pm
Location: Twello, The Netherlands
Contact:

Re: scripting lua trying get WHILE

Post by mswf »

Deviss wrote:
Maveritchell wrote:To fix the code, make two conditional statements, one for when a character is on the vehicle and one for when it is not.
i tried with help of myers but i didnt can get it, any idea suggestion or little lines :D?
The code would be a partial script that says
"while playerisinvehicle, do addhealth+200"
and a different script that says
"while playerisnotinvehicle, do addhealth-200"
or something like that.I think
User avatar
lucasfart
Sith
Sith
Posts: 1440
Joined: Tue Feb 24, 2009 5:32 am
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set
Location: Australia

Re: Scripting lua trying get WHILE

Post by lucasfart »

I'm thinking maybe addhealth 0 when outside the vehicle? If you set it to -200, you'd lose health.

There's not much point in doing this anyway is there? as the vehicle would still lose health and die....vehicles have a differenent health bar i'm pretty sure. If you destroy a speeder, you die no matter how much health you have, same with hovercraft and space vehicles, unless the addhealth affects the vehicle instead?
User avatar
Maveritchell
Jedi Admin
Jedi Admin
Posts: 7366
Joined: Mon Aug 21, 2006 11:03 pm

Re: scripting lua trying get WHILE

Post by Maveritchell »

mswf wrote:
Deviss wrote:
Maveritchell wrote:To fix the code, make two conditional statements, one for when a character is on the vehicle and one for when it is not.
i tried with help of myers but i didnt can get it, any idea suggestion or little lines :D?
The code would be a partial script that says
"while playerisinvehicle, do addhealth+200"
and a different script that says
"while playerisnotinvehicle, do addhealth-200"
or something like that.I think
No. No. Don't use a while loop for this. AddHealth is already a self-repeating property, there's no reason to apply it over and over and over. Unless you are using while to iterate through something (just like I said above) don't use it.

If you were dead-set on using a while loop (why, I don't know), you could write something like this:

Code: Select all

vehicleaddhealth = OnCharacterEnterVehicle(
	function(player, vehicle)
		if IsCharacterHuman(player) and GetEntityClass(vehicle) == FindEntityClass("rep_hover_barcspeeder") then
			SetProperty(GetEntityName(vehicle), "AddHealth", 200)
			vehloop = GetCharacterVehicle(player)
			while vehloop do
				vehloop = GetCharacterVehicle(player)
				if not GetCharacterVehicle(player) then
					SetProperty(GetEntityName(vehicle), "AddHealth", 0)
				end
			end
		end
	end
)
It iterates through nothing, but whenever the player is no longer on a vehicle it breaks the loop and removes the addhealth. It still might give you a crash; I don't know if the while loop will handle that continual referencing that you're asking it to do.
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: Scripting lua trying get WHILE

Post by [RDH]Zerted »

I'm not exactly sure what you're trying to do. Do you want the vehicle to gain health/heal at a rate of 200 per second while a player is in it or do you want the vehicle's health to increase by 200 only once when a player enters the vehicle?

Also, is this the only type of vehicle you have on your map?
Deviss
Master of the Force
Master of the Force
Posts: 3772
Joined: Tue Aug 12, 2008 7:59 pm
Projects :: Clone Wars Extended
Games I'm Playing :: BF2

Re: Scripting lua trying get WHILE

Post by Deviss »

[RDH]Zerted wrote:I'm not exactly sure what you're trying to do. Do you want the vehicle to gain health/heal at a rate of 200 per second while a player is in it or do you want the vehicle's health to increase by 200 only once when a player enters the vehicle?

Also, is this the only type of vehicle you have on your map?
the first one, only i want vehicle have regen 200 per seconds when i (player ) am using it :D when i out i want this havent regen 200 :mrgreen:

MAV: you are a master really, i will test your code now ;) many thanks really and is the first time i see vehloop code :P

EDIT: this crash :S
myers73
Lieutenant General
Lieutenant General
Posts: 690
Joined: Fri Apr 03, 2009 11:04 pm
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set
Location: Atlanta, GA xfire=myers73 IngameName=mYers

Re: Scripting lua trying get WHILE

Post by myers73 »

try this

Code: Select all

vehRegen = OnCharacterEnterVehicle(
    function(player, vehicle)
        if IsCharacterHuman(player) and GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
            SetProperty(GetCharacterVehicle(player), "AddHealth", 200)
        end
    end
)

vehStopRegen = OnCharacterExitVehicle(
    function(player, vehicle)
        if IsCharacterHuman(player) and GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
            SetProperty(GetCharacterVehicle(player), "AddHealth", -200)
        end
    end
)
Deviss
Master of the Force
Master of the Force
Posts: 3772
Joined: Tue Aug 12, 2008 7:59 pm
Projects :: Clone Wars Extended
Games I'm Playing :: BF2

Re: Scripting lua trying get WHILE

Post by Deviss »

myers73 wrote:try this

Code: Select all

vehRegen = OnCharacterEnterVehicle(
    function(player, vehicle)
        if IsCharacterHuman(player) and GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
            SetProperty(GetCharacterVehicle(player), "AddHealth", 200)
        end
    end
)

vehStopRegen = OnCharacterExitVehicle(
    function(player, vehicle)
        if IsCharacterHuman(player) and GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
            SetProperty(GetCharacterVehicle(player), "AddHealth", -200)
        end
    end
)
trying now ;) i cant discover which is the different between GetEntityClassPtr("rep_hover_barcspeeder") and FindEntityClass("rep_hover_barcspeeder")

EDIT: this work but again when i exit of vehicle this kept his regen :S
myers73
Lieutenant General
Lieutenant General
Posts: 690
Joined: Fri Apr 03, 2009 11:04 pm
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set
Location: Atlanta, GA xfire=myers73 IngameName=mYers

Re: Scripting lua trying get WHILE

Post by myers73 »

Deviss on Xfire wrote: work but when i exit this kept regen :S
try this then

Code: Select all

vehRegenBigAssFunction = OnCharacterSpawn(
    function(player)
        if IsCharacterHuman(player) then
            vehRegen = OnCharacterEnterVehicle(
                function(player, vehicle)
                    if GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
                        theVeh = (GetCharacterVehicle(player)
                        SetProperty(theVeh, "AddHealth", 200)
                    end
                end
            )
            vehStopRegen = OnCharacterExitVehicle(
                function(player, vehicle)
                    if GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
                        SetProperty(theVeh, "AddHealth", -200)
                    end
                end
            )
        end
    end
)
Deviss
Master of the Force
Master of the Force
Posts: 3772
Joined: Tue Aug 12, 2008 7:59 pm
Projects :: Clone Wars Extended
Games I'm Playing :: BF2

Re: Scripting lua trying get WHILE

Post by Deviss »

myers73 wrote:
Deviss on Xfire wrote: work but when i exit this kept regen :S
try this then
Hidden/Spoiler:
vehRegenBigAssFunction = OnCharacterSpawn(
function(player)
if IsCharacterHuman(player) then
vehRegen = OnCharacterEnterVehicle(
function(player, vehicle)
if GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
theVeh = (GetCharacterVehicle(player)
SetProperty(theVeh, "AddHealth", 200)
end
end
)
vehStopRegen = OnCharacterExitVehicle(
function(player, vehicle)
if GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
SetProperty(theVeh, "AddHealth", -200)
end
end
)
end
end
)
agian this still keep his regen after i exit of vehicle :S , i will try make one mix between your and mav one :P

EDIT: i tried this using mav one and i got no crash, but anyway keep regen after i exit vehicle :(

Code: Select all

        vehicleaddhealth = OnCharacterEnterVehicle(
           function(player, vehicle)
              if IsCharacterHuman(player) and GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
                 SetProperty(GetEntityName(vehicle), "AddHealth", 200)
                 vehloop = GetCharacterVehicle(player)
                 if vehloop then
                    vehloop = GetCharacterVehicle(player)
                    if not GetCharacterVehicle(player) then
                       SetProperty(GetEntityName(vehicle), "AddHealth", 0)
                    end
                 end
              end
           end
        )
any idea :S?
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: Scripting lua trying get WHILE

Post by [RDH]Zerted »

There is no good way to determine when a player leaves a vehicle. I only have a minute so I can't do any code, but what you want to do is when you enter the vehicle, setup and start a repeating timer. Each time the timer goes off check to see if the player is still in the same vehicle. If so, do nothing. If not, then remove the AddHealth.

A different way if the player is always the same class would be to make that class have a high auto-repair rate for vehicles. I don't know if that fits into your map or not.
Deviss
Master of the Force
Master of the Force
Posts: 3772
Joined: Tue Aug 12, 2008 7:59 pm
Projects :: Clone Wars Extended
Games I'm Playing :: BF2

Re: Scripting lua trying get WHILE

Post by Deviss »

[RDH]Zerted wrote:There is no good way to determine when a player leaves a vehicle. I only have a minute so I can't do any code, but what you want to do is when you enter the vehicle, setup and start a repeating timer. Each time the timer goes off check to see if the player is still in the same vehicle. If so, do nothing. If not, then remove the AddHealth.

A different way if the player is always the same class would be to make that class have a high auto-repair rate for vehicles. I don't know if that fits into your map or not.
oh yeah i never think it, addhealth finish when i change of vehicle name, but how will be the code xD? (of course when you have time :) )
User avatar
Maveritchell
Jedi Admin
Jedi Admin
Posts: 7366
Joined: Mon Aug 21, 2006 11:03 pm

Re: Scripting lua trying get WHILE

Post by Maveritchell »

Deviss wrote:
myers73 wrote:
Deviss on Xfire wrote: work but when i exit this kept regen :S
try this then
Hidden/Spoiler:
vehRegenBigAssFunction = OnCharacterSpawn(
function(player)
if IsCharacterHuman(player) then
vehRegen = OnCharacterEnterVehicle(
function(player, vehicle)
if GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
theVeh = (GetCharacterVehicle(player)
SetProperty(theVeh, "AddHealth", 200)
end
end
)
vehStopRegen = OnCharacterExitVehicle(
function(player, vehicle)
if GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
SetProperty(theVeh, "AddHealth", -200)
end
end
)
end
end
)
agian this still keep his regen after i exit of vehicle :S , i will try make one mix between your and mav one :P
EDIT: i tried this using mav one and i got no crash, but anyway keep regen after i exit vehicle :(
Hidden/Spoiler:
[code] vehicleaddhealth = OnCharacterEnterVehicle(
function(player, vehicle)
if IsCharacterHuman(player) and GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
SetProperty(GetEntityName(vehicle), "AddHealth", 200)
vehloop = GetCharacterVehicle(player)
if vehloop then
vehloop = GetCharacterVehicle(player)
if not GetCharacterVehicle(player) then
SetProperty(GetEntityName(vehicle), "AddHealth", 0)
end
end
end
end
)[/code]
any idea :S?
If you used the code you just posted, you didn't use the code I posted. I wrote it so that it has to use the while loop. Here's the code again:

Code: Select all

vehicleaddhealth = OnCharacterEnterVehicle(
	function(player, vehicle)
		if IsCharacterHuman(player) and GetEntityClass(vehicle) == FindEntityClass("rep_hover_barcspeeder") then
			SetProperty(GetEntityName(vehicle), "AddHealth", 200)
			vehloop = GetCharacterVehicle(player)
			while vehloop do
				vehloop = GetCharacterVehicle(player)
				if not GetCharacterVehicle(player) then
					SetProperty(GetEntityName(vehicle), "AddHealth", 0)
				end
			end
		end
	end
)
Deviss
Master of the Force
Master of the Force
Posts: 3772
Joined: Tue Aug 12, 2008 7:59 pm
Projects :: Clone Wars Extended
Games I'm Playing :: BF2

Re: Scripting lua trying get WHILE

Post by Deviss »

Maveritchell wrote:
Deviss wrote:
myers73 wrote:
Deviss on Xfire wrote: work but when i exit this kept regen :S
try this then
Hidden/Spoiler:
vehRegenBigAssFunction = OnCharacterSpawn(
function(player)
if IsCharacterHuman(player) then
vehRegen = OnCharacterEnterVehicle(
function(player, vehicle)
if GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
theVeh = (GetCharacterVehicle(player)
SetProperty(theVeh, "AddHealth", 200)
end
end
)
vehStopRegen = OnCharacterExitVehicle(
function(player, vehicle)
if GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
SetProperty(theVeh, "AddHealth", -200)
end
end
)
end
end
)
agian this still keep his regen after i exit of vehicle :S , i will try make one mix between your and mav one :P
EDIT: i tried this using mav one and i got no crash, but anyway keep regen after i exit vehicle :(
Hidden/Spoiler:
[code] vehicleaddhealth = OnCharacterEnterVehicle(
function(player, vehicle)
if IsCharacterHuman(player) and GetEntityClass(vehicle) == GetEntityClassPtr("rep_hover_barcspeeder") then
SetProperty(GetEntityName(vehicle), "AddHealth", 200)
vehloop = GetCharacterVehicle(player)
if vehloop then
vehloop = GetCharacterVehicle(player)
if not GetCharacterVehicle(player) then
SetProperty(GetEntityName(vehicle), "AddHealth", 0)
end
end
end
end
)[/code]
any idea :S?
If you used the code you just posted, you didn't use the code I posted. I wrote it so that it has to use the while loop. Here's the code again:

Code: Select all

vehicleaddhealth = OnCharacterEnterVehicle(
	function(player, vehicle)
		if IsCharacterHuman(player) and GetEntityClass(vehicle) == FindEntityClass("rep_hover_barcspeeder") then
			SetProperty(GetEntityName(vehicle), "AddHealth", 200)
			vehloop = GetCharacterVehicle(player)
			while vehloop do
				vehloop = GetCharacterVehicle(player)
				if not GetCharacterVehicle(player) then
					SetProperty(GetEntityName(vehicle), "AddHealth", 0)
				end
			end
		end
	end
)
i know and i tested by make crash :S , maybe because WHILE? but supposly while must work without problems in lua, but always i use it cause crash :S , for this reason i need if zerted can save me, or yes or yes i need use IF for no crash xD
Post Reply