loadResource from string

A search (quite a lot), but could you confirme me that it is not possible ?

WatchUi.loadResource("iconX_Y");

Actually I load desired resource with a function :

function getIcons(p_id, p_size) {
	if (p_id == 0) {
		if (p_size == 32) { return Rez.Drawables.icon0_32;}
		if (p_size == 25) { return Rez.Drawables.icon0_24b;}
		if (p_size == 24) { return Rez.Drawables.icon0_24;}
	}
	if (p_id == 1) {
		if (p_size == 32) { return Rez.Drawables.icon1_32;}
		if (p_size == 25) { return Rez.Drawables.icon1_24b;}
		if (p_size == 24) { return Rez.Drawables.icon1_24;}
	}
	if (p_id == 2) {
		if (p_size == 32) { return Rez.Drawables.icon2_32;}
		if (p_size == 25) { return Rez.Drawables.icon2_24b;}
		if (p_size == 24) { return Rez.Drawables.icon2_24;}
	}
...

Note, I tested also with a pre-fteched array, but even if you get better size for "application code", you use more memory, and finally it's better with a function.

Thanks for your confirmation, or new ideas Wink

  • Oups, I thought strange that a static array was a worse solution... but I was comparing the "if" function and an array as a global ... Finally I put the array in the function, and the array is loaded quickly in memory just when it's needed then unloaded. So the solution based on an array in a function is better. 

    function getIcons(p_id, p_size) {
    	var Icons=[
    	Rez.Drawables.icon0_32,
    	Rez.Drawables.icon0_24b,
    	Rez.Drawables.icon0_24,
    	Rez.Drawables.icon1_32,
    	Rez.Drawables.icon1_24b,
    	Rez.Drawables.icon1_24,
    ...
    	Rez.Drawables.icon67_24b,
    	Rez.Drawables.icon67_24
    	];
    
    	return Icons[p_id * 3 + p_size];
    }
    

    Anyway, if there is a function that can directly load a resource from a string representing is name, perhaps it could be better for memory usage ?

    Regards.

  • There are other threads about this.  When you're loading a resource, you are using a symbol, and there's no way to convert a string into a symbol.

  • Thanks for confirmation, as I said, I just wanted a confirmation (threads about this are 2 years and 5 years old Wink)

  • From one of those threads that Jim mentions...

    I found significant savings once I found out this works:

    var icons = [:symbol1, :symbol2 ...etc. ];

    Then:

    var icon = Rez.Drawables[icons[index]]:

    It turns out that all the repeated X.Y consumes compiled bytes so separating the values when you’ve a long list saves a significant chunk (at the expense of ease of maintaining code).

  • Not sure exactly what you're trying to do here, but if it's related to whuch icons are used based on the display size, you might want to look are doing resource overrides (you don't even need to set any thing in jungles)

    something like

    resources

    --icons for 240x240 (id=icon1, icon2,icon3)

    resources-round-260x260

    --icons for 260x260 (id=icon1, icon2,icon3)

    resources-round-280x280

    --icons for 280x280 (id=icon1, icon2,icon3)

    And in your code, you just always use icon1,icon2,icon3 and you always get the proper one based on the display size.

    -----

    CIQ 4.0 makes it even easier, but you won't be able to use it for production apps for a while - bitmap scaling:

    <bitmap id="id_monkey" filename="../drawables/monkey.png" scaleX="10%" scaleY="10%" scaleRelativeTo="screen"/>

  • So the solution based on an array in a function is better. 

    The solution is mentioned above... This doe doesn't require the virtual-machine to lookup each of the resource identifiers when building the array:

    function getIcons(p_id, p_size) {
        var icons = [
            :icon0_32,
            :icon0_24b,
            :icon0_24,
            :icon1_32,
            :icon1_24b,
            :icon1_24,
            ...
            :icon67_24b,
            :icon67_24
        ];
    
        return Rez.Drawables[Icons[p_id * 3 + p_size]];
    }

    This will use a lot less code space to create the array and requires the virtual machine to execute less code too.

  • Many thanks, to you Travis and 9635560 ! 3ko gained !

  • Hi, thanks for these propositions. I have a set of 67 various icons that the user can choose. Then, I have 3 variants per icon (32x32, 24x24 and 24x24-inverted-colors) that are used at several moments. So, the first proposition doesn't help, but the second will help in the future, at least for 32x32 and 24x24 variants. 

  • Well, one of the interesting ways might be using custom fonts