Page 1 of 2

Scripting lua trying get WHILE

Posted: Mon Jun 21, 2010 12:27 pm
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 :)

Re: scripting lua trying get WHILE

Posted: Mon Jun 21, 2010 4:40 pm
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.

Re: scripting lua trying get WHILE

Posted: Mon Jun 21, 2010 4:51 pm
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

Re: scripting lua trying get WHILE

Posted: Mon Jun 21, 2010 5:04 pm
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

Re: scripting lua trying get WHILE

Posted: Mon Jun 21, 2010 5:24 pm
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.

Re: scripting lua trying get WHILE

Posted: Mon Jun 21, 2010 5:37 pm
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?

Re: Scripting lua trying get WHILE

Posted: Tue Jun 22, 2010 6:11 pm
by myers73
hey, hit me up on xfire tomorrow afternoon and ill help. working in the morning though.

Re: scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 8:03 am
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

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 9:03 am
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?

Re: scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 10:03 am
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.

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 10:08 am
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?

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 10:14 am
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

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 10:25 am
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
)

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 10:28 am
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

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 10:42 am
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
)

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 11:08 am
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?

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 11:46 am
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.

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 11:54 am
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 :) )

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 3:20 pm
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
)

Re: Scripting lua trying get WHILE

Posted: Wed Jun 23, 2010 9:50 pm
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