+ Reply to Thread
Results 1 to 5 of 5

Thread: local become global wtf? :o

  1. #1
    Rift Chaser Ferather's Avatar
    Join Date
    Jun 2011
    Posts
    341

    Default local become global wtf? :o

    I recently intentionally created an error with a local function, In-game reports the error as a GLOBAL?


    local random = ""

    Error global random is nil

    random = ""

    no error :S

    what happened to local to function names
    Last edited by Ferather; 12-03-2011 at 02:15 PM.

  2. #2
    Rift Chaser Ferather's Avatar
    Join Date
    Jun 2011
    Posts
    341

    Default

    heres the code:

    Code:
    	if params.overkill then
    		if currentCaster == nil then
    			do return end
    		end
    		Attacker = currentCaster.name
    		if currentTarget == nil then
    			do return end
    		end
    		Victim = currentTarget.name
    		if params.abilityName == nil then
    			do return end
    		end
    		abilityName = params.abilityName
    		if params.damage == nil then
    			do return end
    		end
    		damageDone = params.damage 
    		if params.crit then
    			Critical = " Critically"
    		else
    			Critical = ""
    		end
    		if params.damageAbsorbed then
    			Absorbed = " (" .. params.damageAbsorbed .. " Absorbed)"
    		else
    			Absorbed = ""
    		end
    		if params.damageBlocked then
    			Blocked = " (" .. params.damageBlocked .. " Blocked)"
    		else
    			Blocked = ""
    		end
    		if params.damageDeflected then
    			Deflected = " (" .. params.damageDeflected .. " Deflected)"
    		else
    			Deflected = ""
    		end
    		if params.overkill then
    			Overkill = " (" .. params.overkill .. " Overkill)"
    		else
    			Overkill = ""
    		end
    		
    		if currentTarget then
    			if currentTarget.player then
    			data = Attacker .. "'s " .. abilityName .. Critical .. " Hits " .. Victim .. " For " .. damageDone .. " " .. damageType .. " Damage" .. Absorbed .. Blocked .. Deflected .. Overkill .. ".\n"
    			table.insert(MyAddon.state.all, data)
    			table.insert(encounter(death), data)
    			text = table.concat(encounter(death))
    			MyAddon.UI.Deathlog:Show(text)
    			end
    		end
    	end
    just put local in front of say Overkill then error GLOBAL = nil :P

    btw devs my combatlog is more accurate :P
    Last edited by Ferather; 12-03-2011 at 02:23 PM.

  3. #3
    Plane Walker Rizz's Avatar
    Join Date
    Jan 2011
    Location
    Stockholm, Sweden
    Posts
    437

    Default

    That's because you don't know how local works.

    Code:
    local file_local = "a" -- This one "exists" in this file, and only this file.
    
    function foo_bar()
    	local foo = "b" -- This one "exists" in this function.
    	
    
    	if true then
    		local cake = "c" -- This one "exists" in this if statement only.
    		bar = "doughnut" -- This one exists globally now
    	end
    	-- cake does no longer exist
    	-- doing print(cake) would give the error "global cake is nil" (or similar)
    	-- doing print(bar) would however work.
    end
    -- foo does no longer exist
    Please correct me if I did any mistakes, pretty tired atm, so I probably did.
    Last edited by Rizz; 12-04-2011 at 12:07 PM.
    Rathi@Gelidra <Invictus>
    IRotP: Conqueror | NM TD: 4/4

  4. #4
    Shield of Telara Adelea's Avatar
    Join Date
    Mar 2011
    Posts
    734

    Default

    Quote Originally Posted by Ferather View Post
    heres the code:

    Code:
    	
    		if params.crit then
    			Critical = " Critically"
    		else
    			Critical = ""
    		end
    just put local in front of say Overkill then error GLOBAL = nil :P

    btw devs my combatlog is more accurate :P
    As the above poster says, what you have is a scope issue.


    Code:
    	
    		if params.crit then
    			local Critical = " Critically"
    		else
    			Critical = ""
    		end
    Here Critical is local only its scope which is basically the if .. else block.

    What you want is something like:


    Code:
    	
    		local Critical = ""
    		if params.crit then
    			Critical = " Critically"
    		end
    Here Critical is local to anything that is in the same scope as the if statement itself.
    http://forums.riftgame.com/image.php?type=sigpic&userid=125779&dateline=13553  38065

  5. #5
    Plane Touched Levva's Avatar
    Join Date
    Jan 2011
    Location
    Aberdeen, Scotland
    Posts
    243

    Default

    Quote Originally Posted by Ferather View Post
    I recently intentionally created an error with a local function, In-game reports the error as a GLOBAL?


    local random = ""

    Error global random is nil

    random = ""

    no error :S

    what happened to local to function names
    As others have said if you are going to have lines like

    Code:
    if x then
       local y ="abc"
    else 
       local y = ""
    end
    
    print("value of y is "..y)
    This will ALWAYS fail and it will fail with the error that the GLOBAL value of the variable y is nil. This is because the ONLY variable called y in scope at that point would be one the global y.

    what your code should be is

    Code:
    local y = ""
    if x then
       y ="abc"
    end
    
    print("value of y is "..y)
    now the LOCAL variable y has been defined within the scope of your block of code and thus is valid to print with.

    In your code your used local WITHIN an if or else statement and so the local "wore off" (ie; went out of scope) once it was finished with the if statement ie: when it got to the the end statement. Since your variable went out of scope all that the print statement could be referring to is the global version of your variable and therefore the error said that the global version was nil.

    So your code should be something like ...

    Code:
    	if params.overkill then
    		local Attacker, abilityName, Critical, Victim, damageDone, damageType, Absorbed, Blocked, Deflected, Overkill
    		if currentCaster == nil then
    			return 
    		end
    		Attacker = currentCaster.name
    		if currentTarget == nil then
    			return 
    		end
    		Victim = currentTarget.name
    		if params.abilityName == nil then
    			return 
    		end
    		abilityName = params.abilityName
    		if params.damage == nil then
    			return 
    		end
    		damageDone = params.damage 
    		if params.crit then
    			Critical = " Critically"
    		else
    			Critical = ""
    		end
    		if params.damageAbsorbed then
    			Absorbed = " (" .. params.damageAbsorbed .. " Absorbed)"
    		else
    			Absorbed = ""
    		end
    		if params.damageBlocked then
    			Blocked = " (" .. params.damageBlocked .. " Blocked)"
    		else
    			Blocked = ""
    		end
    		if params.damageDeflected then
    			Deflected = " (" .. params.damageDeflected .. " Deflected)"
    		else
    			Deflected = ""
    		end
    		if params.overkill then
    			Overkill = " (" .. params.overkill .. " Overkill)"
    		else
    			Overkill = ""
    		end
    		
    		if currentTarget then
    			if currentTarget.player then
    			data = Attacker .. "'s " .. abilityName .. Critical .. " Hits " .. Victim .. " For " .. damageDone .. " " .. damageType .. " Damage" .. Absorbed .. Blocked .. Deflected .. Overkill .. ".\n"
    			table.insert(MyAddon.state.all, data)
    			table.insert(encounter(death), data)
    			text = table.concat(encounter(death))
    			MyAddon.UI.Deathlog:Show(text)
    			end
    		end
    	end
    note I replaced the overkill - do return end - with just - return - which works just as well.
    Last edited by Levva; 12-04-2011 at 06:03 PM.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts