Repeatedly Execute code too slow

Started by Nixxon, Mon 19/01/2015 05:02:19

Previous topic - Next topic

Nixxon

Hi Guys,

Running this script in one of my rooms (see below), It checks if they player has a certain amount of pebbles using Global int functions and adds to the players inventory accordingly.

The code works fine. However, it's very slow. So for instance, when we Add Inventory (iPebble3), it takes a few seconds (roughly 5) for (iPebble2) to be removed from the inventory. I was to understand that the repeatedly execute code ran 40 times per second. Anyway I can speed this up??

----------------------------------------------------------------------------------------------------------
function Pebbles_Interact()
{
pebblecount += 1;
cEgo.Walk (500,  330,  1);
}

function repeatedly_execute_always()
{
if (pebblecount == 1)
cEgo.AddInventory(iPebble);
if (pebblecount == 2)
cEgo.AddInventory(iPebble2);
if (pebblecount == 3)
cEgo.AddInventory(iPebble3);
if (pebblecount == 2)
cEgo.LoseInventory(iPebble);
if (pebblecount == 3)
cEgo.LoseInventory(iPebble2);
}

cadeultra3

Curious to why this needs to be run in the execute always function?

Nixxon

#2
Not sure of a better way to do it to be honest??

There will be another scene in which the pebbles are removed from the inventory accordingly, so I thought a Global Variable would be the best option.

EDIT: perhaps you mean 'always' as opposed to the normal repeatedly execute. I used the always function thinking perhaps the script was being blocked or something, it made no difference.

Gurok

Try this (and remove the repeatedly_execute_always function):

Code: ags
function Pebbles_Interact()
{
    pebblecount += 1;
    if(pebblecount == 1)
    {
        cEgo.AddInventory(iPebble);
    }
    else
        if(pebblecount == 2)
        {
            cEgo.LoseInventory(iPebble);
            cEgo.AddInventory(iPebble2);
        }
        else
            if(pebblecount == 3)
            {
                cEgo.LoseInventory(iPebble2);
                cEgo.AddInventory(iPebble3);
            }
    cEgo.Walk (500,  330,  1);
}
[img]http://7d4iqnx.gif;rWRLUuw.gi

monkey0506

If the only reason you're using different inventory items is to keep track of how many pebbles the player has, then I'm afraid to tell you that you're doing it completely wrong. You should set this up as a single inventory item (iPebbles) and use the InventoryQuantity to control how many pebbles the player has as well as updating the graphic:

Code: ags
function on_event(EventType event, int data)
{
  if (((event == eEventAddInventory) || (event == eEventLoseInventory)) && (data == iPebbles.ID))
  {
    int quantity = player.InventoryQuantity[data];
    if (quantity == 1) iPebbles.Graphic = PEBBLES_1_GRAPHIC; // fill in actual sprite slot numbers
    else if (quantity == 2) iPebbles.Graphic = PEBBLES_2_GRAPHIC;
    else if (quantity == 3) iPebbles.Graphic = PEBBLES_3_GRAPHIC;
  }
}

function Pebbles_Interact()
{
  player.Walk(500, 330, eBlock); // '1' is not a valid value for BlockingStyle, either use eNoBlock or eBlock
  player.AddInventory(iPebbles);
}


Edit: Gurok posted while I was typing, but I stand behind my solution. :P

Gurok

Yeah, if the initial solution was accepted, I was going to post something more efficient. I just like to modify the code as little as possible so that people with questions understand what's going on. Nixxon, use Monkey's solution!
[img]http://7d4iqnx.gif;rWRLUuw.gi

Nixxon

Thanks Guys,

I'm super green with coding and really appreciate the swift responses.

Regarding Monkey's solution, do I place this in the regular room repeatedly execute? If not, where so I paste that particular code?

Thanks Gurok, helps to understand how to better the scripting I had already implemented for future reference :)

Vincent

I guess you should create the function in your Global Script or Room Script as you prefer to keep it work.
No any repeatedly execute required in this case.

Code: ags

// main global script file
function on_event(EventType event, int data)
{

}

/////////////////////////////////////////////

// room script file
function on_event(EventType event, int data)
{

}

Nixxon

#8
Holy Shit, It worked... That's awesome!!!!

Nixxon

Hi Guys,

Just another quick one, I want the player to stop being able to pick up the pebbles after he has 3.

Am I best to use the repeatedly execute for this to turn off the hotspot will a Global int value?

ChamberOfFear

Only put code in repeatedly_execute that you literally want to repeatedly execute. What good reason could you possibly have for constantly turning off a hotspot?

To answer your question, the simplest solution to me seem to just deny the player to pick up once the quota has been met or exceeded. So we could perhaps abort the Pebbles_Interact function before adding to inventory? Based off monkey's code earlier..

Code: ags

function Pebbles_Interact()
{
  int quantity = player.InventoryQuantity[iPebbles.ID];
  if (quantity >= 3)
    return; // Abort function if pebbles quota has been met or exceeded.

  player.Walk(500, 330, eBlock); // '1' is not a valid value for BlockingStyle, either use eNoBlock or eBlock
  player.AddInventory(iPebbles);
}

monkey0506

#11
Simply returning from the function isn't really very user friendly. Probably best to at least have the player say something first, and then call return:

Code: ags
  if (quantity >= 3)
  {
    player.Say("I've already got plenty of those.");
    return;
  }


;)


Sorry if my earlier snippet wasn't clear on where it needed to go. As a head's up, any time you see "function XXX_YYY(...)", then that's a pretty clear indication the code doesn't belong inside of any other functions (like rep execute). If you haven't already, make sure to run through the scripting tutorial in the manual, it should help clarify these things.

As for this snippet (in this post), it should be merged into the Pebbles_Interact function as Chamber has shown, I just added the hint for the player so they know why they can't pick up more.

Nixxon

Once again, Effing brilliant!!

Pasted pretty much the same code into another room where the player uses the pebbles, using LoseInventory instead and it works brilliantly.

Thank you so much for your efforts.

SMF spam blocked by CleanTalk