ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Display image depending on the zone (https://www.esoui.com/forums/showthread.php?t=4854)

ZunaSW 07/02/15 08:35 AM

Display image depending on the zone
 
Hello,

I'm now trying some other things, mainly I want to display an image depending on the zone the player is.
My idea is this:
If for example the player is in Eastmarch zone, Image1 will be displayed as a key is pressed. If then the player changes to Stonefalls zone, Image2 will be displayed as the same key is pressed.

I got no problems with the keybindings, but I'm having problems displaying the different images. For now, when I press the key, I get a 1024x1024 blank image, as I stated here:

Code:

local wm = GetWindowManager()
local MainImage = wm:CreateTopLevelWindow(nil)
MainImage:SetDimensions(1024,1024)
MainImage:SetAnchor(CENTER,GuiRoot,CENTER,0,0)
MainImage0 = wm:CreateControl(nil, MainImage, CT_TEXTURE)
MainImage0:SetAnchorFill(MainImage)
MainImage0:SetHidden(true)

I got this code from other of my addons, I'm no expert, so that might be wrong :/ But for now, works.

Now I got the keybinding code:

Code:

function IM.OnKeyDown()
        MainImage0:SetHidden(false)
end

function IM.OnKeyUp()
        MainImage0:SetHidden(true)
end

That works perfectly.

And here is the code that I think it is wrong, even when I don't get any errors in game:

Code:

function IMOnZoneChanged(eventCode, zoneName, subZoneName, newSubzone)
        if zoneName == "Eastmarch" then
                MainImage0:SetTexture("IM/textures/Image1.dds")
        end
        if zoneName == "Stonefalls" then
                MainImage0:SetTexture("IM/textures/Image2.dds")
        end
end

EVENT_MANAGER:RegisterForEvent(IM.name, EVENT_ZONE_CHANGED, IMOnZoneChanged)

For now, as I said, this last part of the code seems to not work, or at least do what I want it to do. I press the key and get a blank image, instead of the images I stated in the last code.

Any ideas on how to make this work? Maybe I'm using the wrong event, I'm not sure, as I said, I'm no expert.

Thank you.

merlight 07/02/15 08:56 AM

If you added/changed any .dds files while the game was running, you might need to restart the client. It caches textures (and their absence as well).

ZunaSW 07/02/15 09:11 AM

Hm, nothing, didn't work :c

Must be something from the code.

votan 07/02/15 11:07 AM

Quote:

Originally Posted by ZunaSW (Post 21813)
Hm, nothing, didn't work :c

Must be something from the code.

hmm, the first zone after loading does not trigger a "change".

You could try to call your zone change function like this in EVENT_PLAYER_ACTIVATED:
Code:

IMOnZoneChanged(0, GetUnitZone("player"))

ZunaSW 07/02/15 11:23 AM

Hm, nothing, doesn't seem to work, I'm using this code, might be wrong though:

Code:

function IMOnZoneChanged(0, GetUnitZone("player"))
        if zoneName == "Eastmarch" then
                MainImage0:SetTexture("IM/textures/Image1.dds")
        end
        if zoneName == "Stonefalls" then
                MainImage0:SetTexture("IM/textures/Image2.dds")
        end
end

And I'm getting this error:

Code:

user:/AddOns/IM/IM.lua:30: <name> or "..." expected near '0
I might have done something wrong with the event. I'm pretty new on this, so I don't really know :S

votan 07/02/15 11:36 AM

Quote:

Originally Posted by ZunaSW (Post 21819)
Hm, nothing, doesn't seem to work, I'm using this code, might be wrong though:

Code:

function IMOnZoneChanged(0, GetUnitZone("player"))
        if zoneName == "Eastmarch" then
                MainImage0:SetTexture("IM/textures/Image1.dds")
        end
        if zoneName == "Stonefalls" then
                MainImage0:SetTexture("IM/textures/Image2.dds")
        end
end

And I'm getting this error:

Code:

user:/AddOns/IM/IM.lua:30: <name> or "..." expected near '0
I might have done something wrong with the event. I'm pretty new on this, so I don't really know :S

I was not precise (or lazy?) :D Calling, not declaring.
Lua Code:
  1. local function IMOnZoneChanged(eventCode, zoneName, subZoneName, newSubzone)
  2.     if zoneName == "Eastmarch" then
  3.         MainImage0:SetTexture("IM/textures/Image1.dds")
  4.     end
  5.     if zoneName == "Stonefalls" then
  6.         MainImage0:SetTexture("IM/textures/Image2.dds")
  7.     end
  8. end
  9.  
  10. local function PlayerActivated()
  11.   EVENT_MANAGER:UnregisterForEvent(IM.name, EVENT_PLAYER_ACTIVATED)
  12.   IMOnZoneChanged(0, GetUnitZone("player"))
  13. end
  14.  
  15. EVENT_MANAGER:RegisterForEvent(IM.name, EVENT_ZONE_CHANGED, IMOnZoneChanged)
  16.  
  17. EVENT_MANAGER:RegisterForEvent(IM.name, EVENT_PLAYER_ACTIVATED, PlayerActivated)
^^ Typed without testing

About names from API you should read this.

ZunaSW 07/02/15 11:57 AM

Ohhh, I think I get it.
I'm checking that link you put there, it is useful, I think I'm getting things, slowly, but I am ^^.

I tried this code you put there, but I'm getting another error D:

Code:

user:/AddOns/IM/IM.lua:35: attempt to index a nil value
stack traceback:
        user:/AddOns/IM/IM.lua:35: in function '(main chunk)'

I never get what this error means, and I've got it in many addons I tried to do. The line this error reffers to is:

Code:

EVENT_MANAGER:RegisterForEvent(IM.name, EVENT_ZONE_CHANGED, IMOnZoneChanged)
Been checking, but I don't see what is wrong there. Trying hard on making my first well done addon.

Sorry for all these questions x'D

votan 07/02/15 12:15 PM

Quote:

Originally Posted by ZunaSW (Post 21824)
Been checking, but I don't see what is wrong there. Trying hard on making my first well done addon.

Sorry for all these questions x'D

Sure, no problem :)
If it is you first: Welcome! :)

The order of declaration is important in Lua. Is IM declared before line 35?
If the error persists, post the whole source.

ZunaSW 07/02/15 12:35 PM

Oh great! It works now! Now when I press the key while in Eastmarch, it shows Image1, when I press the key while in Stonefalls it shows Image2. Though, I think I'm not done yet, as if I travel from Stonefalls to Eastmarch and press the key again, it shows still Image2, when it should show Image1. I imagine this will be some kind of update event, right? Works perfectly if after travelling I make reloadui.

I think I should use EVENT_ZONE_UPDATE, but I'm not sure how to make this :S

Any ideas?

Many thanks for the help, really :)

votan 07/02/15 12:44 PM

Quote:

Originally Posted by ZunaSW (Post 21827)
Oh great! It works now! Now when I press the key while in Eastmarch, it shows Image1, when I press the key while in Stonefalls it shows Image2. Though, I think I'm not done yet, as if I travel from Stonefalls to Eastmarch and press the key again, it shows still Image2, when it should show Image1. I imagine this will be some kind of update event, right? Works perfectly if after travelling I make reloadui.

I think I should use EVENT_ZONE_UPDATE, but I'm not sure how to make this :S

Any ideas?

Many thanks for the help, really :)

Ah, yes. Traveling is player deactivate and activate rather than zone change. I guess you just need to omit the unregister of EVENT_PLAYER_ACTIVATED

ZunaSW 07/02/15 12:54 PM

Omg, it works!!!
Now it works perfectly! :D Thank you for your help, really :D I hope to finish this addon and do it good! ^^

sirinsidiator 07/02/15 01:20 PM

Welcome to EsoUI! :D

Just wanted to say that if you want to make this more robust in the future you should not depend on zoneName to determine which zone you are in, because it will be a different string for different client languages.
There is a small library that votan and I made which makes finding out the zone independent of the client language a bit easier amongst other things.
You might want to look into that if you plan to share your addon with others that are not playing in the same language as yourself.

ZunaSW 07/02/15 05:51 PM

Thanks Sirinsidiator : P

And yes, that is true, didn't think about that, hrm. I'll check the link you put there, and see what can I do!
Thank you! :D


All times are GMT -6. The time now is 05:23 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI