Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - CharlieMcF

Pages: [1]
1
Scripting / Re: Problem writing magic token functions [SOLVED]
« on: March 03, 2017, 11:00:56 PM »
Brilliant, works like a dream! I've tried it with image, sprite and text elements and it works perfectly. Here's the sprite code:

Code: [Select]
local artwork = fe.add_image("staticsprite.png", 1000, 100, blip*0.405, blip*0.2 );

//sprite animation - use a spritesheet to animate specific frames of the sprite sheet
local sprite_cfg = {
    when = When.Always,
    width = 462,
    height = 200,
    frame = 0,
    time = 3000,
    order = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
    loop = true
}
animation.add( SpriteAnimation( artwork, sprite_cfg ) );

// Create transition that will toggle artwork visibility
fe.add_transition_callback("updateArtwork");
function updateArtwork(ttype, var, transition_time) {
if ((ttype == Transition.StartLayout) || (ttype == Transition.ToNewList) || (ttype == Transition.EndNavigation)){
    artwork.visible = randBool();
  }
}

Thanks heaps: I've learned a lot, hopefully this might help others out too.

2
Scripting / Re: Problem writing magic token functions
« on: March 03, 2017, 09:52:16 PM »
Thanks again! So at its simplest, if I just look at hiding/displaying a single image:

Code: [Select]
local artwork = fe.add_image("fanart.png", 1000, 100, blip*0.405, blip*0.2 );

// Create transition that will toggle artwork visibility
fe.add_transition_callback("updateArtwork");
function updateElement(ttype, var, transition_time) {
  if (ttype == Transition.StartLayout || Transition.ToNewSelection || Transition.ToNewList ) {
    artwork.visible = randBool();
  }
}

Running this always displays the image, regardless of transitions. I assume I need to call updateElement at some point, but not sure what parameters it's expecting?

3
Scripting / Re: Problem writing magic token functions
« on: March 03, 2017, 01:37:46 PM »
Sure: any approach which can show or hide an element (snap, animation, image, text etc) in response to navigation would be great. Sample code would be very helpful, thanks.

4
Scripting / Re: Problem writing magic token functions
« on: March 03, 2017, 01:23:03 AM »
Many thanks. So in my previous thread we ended up with this:

Code: [Select]
function iRand(max) {
    // Generate a pseudo-random integer between 0 and max
    local roll = (1.0 * math.rand() / RAND_MAX) * (max + 1);
    return roll.tointeger();
}

function bRand() {
  if (irand(1) == 1) return true;
  else return false;
}

function showStopper() {
  if (bRand()){
Showelement();
return "On";
}
else {
Hideelement();
return "Off";
}
}

local showText = fe.add_text(„[!showStopper]", 900, 870, 0, 0);

The functions to show and hide the elements look like this:

Code: [Select]
function showelement(){
local breakup = fe.add_image("staticsprite.png", 1000, 100, blip*0.405, blip*0.2 );
breakup.visible = true;
breakup.trigger = Transition.EndNavigation;
breakup.preserve_aspect_ratio = false;

//sprite animation - use a spritesheet to animate specific frames of the sprite sheet
local sprite_cfg = {
    when = When.Always,
    width = 462,
    height = 200,
    frame = 0,
    time = 3000,
    order = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
    loop = true
}
animation.add( SpriteAnimation( breakup, sprite_cfg ) );
}

function hideelement(){
local breakup = fe.add_image("staticsprite.png", 1000, 100, blip*0.405, blip*0.2 );
breakup.visible =false
breakup.trigger = Transition.EndNavigation;
breakup.preserve_aspect_ratio = false;

//sprite animation - use a spritesheet to animate specific frames of the sprite sheet
local sprite_cfg = {
    when = When.Always,
    width = 462,
    height = 200,
    frame = 0,
    time = 3000,
    order = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
    loop = true
}
animation.add( SpriteAnimation( breakup, sprite_cfg ) );
}

This approach works initially but AM will crash a few transitions later. OTOH if I have a variable such as myGlobal which I set to 1 or 0 and then do this:

Code: [Select]
local breakup = fe.add_image("staticsprite.png", 1000, 100, blip*0.405, blip*0.2 );
if (myGlobal == 1) breakup.visible = true;
else breakup.visible = false;
breakup.trigger = Transition.EndNavigation;
breakup.preserve_aspect_ratio = false;

//sprite animation - use a spritesheet to animate specific frames of the sprite sheet
local sprite_cfg = {
    when = When.Always,
    width = 462,
    height = 200,
    frame = 0,
    time = 3000,
    order = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
    loop = true
}
animation.add( SpriteAnimation( breakup, sprite_cfg ) );

It works fine, hence my enthusiasm for being able to set variable values using a magic token. Does this make sense now?

5
Scripting / Re: Problem writing magic token functions
« on: March 02, 2017, 06:18:59 PM »
Thanks for the prompt reply, I'm sure the problem is mainly my lack of understanding. It's not clear to me why showVariable shows the initial value of the variable, not the updated value. I thought the flow would be:

Call increment function via magic token [!incrementit] in showToken
Variable incrvar is incremented
showVariable displays the new value of incrvar

In other words it would be equivalent to

Code: [Select]
incrementit();
local showVariable = fe.add_text("Variable: " + incrvar, 900, 1000, 0, 0);

which does display the updated value of incrvar as I would expect. But obviously it doesn't work this way!

The background to this is in my earlier post on magic tokens - I want to randomly display objects during navigation. keilmillerjr suggested a neat solution, but my problem is that my tweak of his solution randomly crashes AM. Showing or hiding the element based on the value of a variable works fine though, and this strategy would be handy for some other ideas I have. So to redefine the problem: can I update the value of a variable in response to navigation such that other functions can then access and act on the value of that variable? Hope this makes sense.

6
Scripting / Problem writing magic token functions [SOLVED]
« on: March 02, 2017, 02:58:40 PM »
Me again, another magic token issue. I need to write my own magic token functions which can set the values of variables to be used by other functions. Here's a simple example:

Code: [Select]
local incrvar = 0;

function incrementit() {
incrvar = incrvar + 1;
return incrvar;
}

local showToken = fe.add_text("Token: " + "[!incrementit]", 900, 900, 0, 0);
local showVariable = fe.add_text("Variable: " + incrvar, 900, 1000, 0, 0);


If I run this, the showToken text behaves as I expect, ie the value returned by the incrementit function increases by 1 each time the navigation is called (eg by stepping through the games in the list). However, the showVariable text resolutely stays on the initial value of incrvar and never updates. This seems to be a general rule, ie it doesn't matter how the variables are changed within the magic token function, they remain unchanged when accessed from outside the function. I wondered if it was an issue of scope, but declaring the variable as a global makes no difference. Clearly I'm missing something, probably obvious to someone who knows what they're doing.

In case it helps in diagnosing the problem, the absolute values returned in the text strings aren't what I'd expect either, they're incremented by 2. So with the code above and the problem of the variable not updating, I'd expect showToken to display 1 and showVariable to display 0 in the first instance, but they display 3 and 2 respectively. showToken will then increment with navigation, but showVariable remains at 2. I have no idea what's happening here (story of my life) so if anyone can help out it'd be much appreciated.

7
Scripting / Re: Parsing Magic Tokens as Integers
« on: February 22, 2017, 01:59:43 PM »
Thanks so much - very neat solution! I just had to do a bit of tweaking and then adapt the showStopper function:

Code: [Select]
function showStopper() {
  if (bRand()){
Showelement();
return "On";
}
else {
Hideelement();
return "Off";
}
}

The Showelement and Hideelement functions do the obvious thing of showing and hiding the element in question.

Thanks for the prompt and helpful advice, especially since you have clearly got a lot going on in your life right now.

8
Scripting / Re: Parsing Magic Tokens as Integers
« on: February 21, 2017, 06:59:45 PM »
Wow. That's a very generous offer, thanks. If it turns out to be a major hassle and you can give me a couple of pointers I'd be happy to take a crack at it.

9
Scripting / Re: Parsing Magic Tokens as Integers
« on: February 21, 2017, 04:35:50 PM »
Thanks for this suggestion - I guess my problem is that magic tokens don't seem to return as strings (or tointerger() would work, right?). Any guidance here would be very welcome, thanks.

10
Scripting / [Solved] Parsing Magic Tokens as Integers
« on: February 20, 2017, 03:16:08 PM »
I'm having some difficulty extracting an integer value from a magic token, hope some kind soul can give me a hand.

I'm designing a layout which requires that elements are randomly displayed on the screen when a game is highlighted. I can write a function to determine if a particular element should be displayed or not:

Code: [Select]
function Getrand()
    {
        local r = rand() + 1;
   r = r % 2;
        return r;
    }

So this will return 0 or 1, then I'll use this value to hide (if Getrand returns 0) or display (if Getrand returns 1) the element.

Now I need this to update with the navigation, happily magic tokens do exactly that, so I wrote this:

Code: [Select]
local randVal = "[!Getrand]";

And if I display this value on the screen with:

Code: [Select]
local showText = fe.add_text(randVal, 900, 870, 0, 0); 

It seems to do exactly what I want, the value randomly changing between 0 and 1 as I scroll through the games. So far so good.

Clearly I now need to parse the value as an integer in order to evaluate it, and this is where my meagre coding skills desert me. I innocently assumed that the magic tokens would return as strings, but tointeger() fails every time so something's not right here. All advice (including any entirely different solutions, I'm not proud) would be very welcome, thanks.



Pages: [1]