Jump to content


Photo

Having trouble understanding arguments


  • Please log in to reply
11 replies to this topic

#1 Mr Weird Guy

Mr Weird Guy

    GMC Member

  • New Member
  • 69 posts

Posted 27 April 2012 - 10:28 PM

FIXED.



Title Screen:
global.timeRecord01 = 9999999

I have a persistent object with this code:
scr_BossArrays()

scr_BossArrays:
global.timeRecord[1] = global.timeRecord01

Boss Create Event:
global.bossNo = 1

When the boss dies:
if global.bossTotalHP <= 0
    {
    script_execute(scr_EndFight,boss01_A,boss01_B,boss01_C,global.timeRecord01)
    }

scr_EndFight:
if global.timeValue < global.timeRecord[global.bossNo] && global.title != 12
    {
    global.timeRecord[global.bossNo] = global.timeValue
    }

Here's the relevant code that I need help with.

if global.bossTotalHP <= 0
    {
    script_execute(scr_EndFight,boss01_A,boss01_B,boss01_C,global.timeRecord01)
    }
boss01_A, B, and C are all constants that work perfectly.
global.timeRecord01 is your best time on the first boss, in tenths of a second.

scr_EndFight:
if global.timeValue < argument3
    {
    argument3 = global.timeValue
    }

This code changes your best time if you beat the boss more quickly. However, it always changes, regardless of your time.
So what can I do to fix this? There's obviously something I don't fully understand.

Edited by Mr Weird Guy, 01 May 2012 - 05:01 AM.

  • 0

#2 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9314 posts
  • Version:Unknown

Posted 27 April 2012 - 10:38 PM

The variable argument3 (and really, any of the argumentN variables) only exist within the scope of the script. Once the script ends, those variables disappear. So setting argument3=global.timeValue and then ending the script does absolutely nothing.

I think you want to set the global.timeValue=argument3 instead of the other way around, right? Remember: when programming, a=b is NOT the same as b=a. The thing on the right is always stored in the variable name on the left.

-IMP
  • 1

#3 Mr Weird Guy

Mr Weird Guy

    GMC Member

  • New Member
  • 69 posts

Posted 27 April 2012 - 10:55 PM

Your suggestion to set global.timeValue = argument3 wouldn't work.
global.timeValue is the amount of time it just took to beat the boss.
argument3 (or global.timeRecord01) is your current record on the boss.

So what I need to happen is that global.timeRecord01 (or 02, 03, 04, etc.) should be set to global.timeValue if it's a lower time.
This seems a bit tricky to do if arguments don't affect the variables that they reference. But as I have it now, argument3 = global.timeValue does in fact seem to change global.timeRecord01; but it always changes it. So I'm kinda stumped here.
  • 0

#4 integrate

integrate

    GMC Member

  • GMC Member
  • 255 posts
  • Version:GM8

Posted 28 April 2012 - 12:00 AM

Your suggestion to set global.timeValue = argument3 wouldn't work.
global.timeValue is the amount of time it just took to beat the boss.
argument3 (or global.timeRecord01) is your current record on the boss.

So what I need to happen is that global.timeRecord01 (or 02, 03, 04, etc.) should be set to global.timeValue if it's a lower time.
This seems a bit tricky to do if arguments don't affect the variables that they reference. But as I have it now, argument3 = global.timeValue does in fact seem to change global.timeRecord01; but it always changes it. So I'm kinda stumped here.

It seems to me that since your fourth argument (argument3) is actually a global variable, why pass it in a function call at all? Why not just write:
if global.bossTotalHP <= 0 
  scr_EndFight(boss01_A,boss01_B,boss01_C,global.timeRecord01); //shortened this a bit - don't need 'script_execute'

//in scr_EndFight:
if global.timeValue < argument3
   global.timeRecord01 = global.timeValue;  //simply tell global.timeRecord01 to equal the new timeValue

Does that work for your needs?
  • 0

#5 Mr Weird Guy

Mr Weird Guy

    GMC Member

  • New Member
  • 69 posts

Posted 28 April 2012 - 12:19 AM

Well, yes and no. It would work for now, since I only have one boss in the game. But once I start adding more, I'm gonna be using more than just global.timeRecord01. With your suggestion, I'd have to create 24 other scripts for global.timeRecord02 to global.timeRecord25. It's more complex than just one variable, though. There's actually 9 more global variables that the script deals with per boss. I mean, I'll create those extra scripts if I absolutely have to, but it seems like a lot of unecessary work, not to mention that if I want to make a change to one script, I have to make the same change to the other 24 scripts.
  • 0

#6 flexaplex

flexaplex

    GMC Member

  • Global Moderators
  • 4811 posts
  • Version:GM8

Posted 28 April 2012 - 01:16 AM

Use arrays instead of variables.

if (global.bossTotalHP <= 0)
    {
    script_execute(scr_EndFight, boss01_A, boss01_B, boss01_C, 0); //can change argument 3 depending on which timeRecord you're checking
    }

scr_EndFight:

if (global.timeValue < global.timeRecord[argument3])
    {
    global.timeRecord[argument3] = global.timeValue;
    }

  • 0

#7 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5417 posts
  • Version:GM:Studio

Posted 28 April 2012 - 01:16 AM

you could return the smallest value
if global.bossTotalHP <= 0
  global.timeRecord01 = scr_EndFight(boss01_A,boss01_B,boss01_C,global.timeRecord01)

scr_EndFight
return min(argument3,global.timeValue)

that make the script look unessesary as you just could have written..
if global.bossTotalHP <= 0
  global.timeRecord01 = min(global.timeRecord01,global.timeValue)
instead since you don't use the other arguments
 
but if you use arrays, then you can have one variable that tell the number of the boss they are at, if it is keept there and just changed as the boss appears:

global.bossnr = 2;
then it can be added to all your arrays so if global.bossnr is 2 then it will use global.timeRecord[2] .. simply by writing:
global.timeRecord[global.bossnr] = something

then you don't need any arguments at all if you were just gonna use it for the time:
if global.bossTotalHP <= 0
  scr_EndFight()
scr_EndFight
global.timeRecord[ global.bossnr ] = min(global.timeRecord[ global.bossnr ],global.timeValue)

  • 0

#8 Mr Weird Guy

Mr Weird Guy

    GMC Member

  • New Member
  • 69 posts

Posted 28 April 2012 - 04:18 AM

EDIT: I wasn't actually watching the variable until now... it turns out that different variables (which are supposed to change) is changing, and global.timeRecord01 is remaining untouched.
The reason the other variables are changing is because global.timeRecord01 is set to 9999999 at the start of the game, and if that variable's not getting changed, the other variables (which display the time in minutes, seconds, and tenths of a second) are always getting changed after every fight.

Well, I tried using arrays. The same problem's still happening. It's still changing the current boss record even if it's a slower time.

In the Boss' create event, I set global.bossNo to 1.

Before the main script, I call a simple one line script:
global.timeRecord[1] = global.timeRecord01

And the main script:
if global.timeValue < global.timeRecord[global.bossNo]
    {
    global.timeRecord[global.bossNo] = global.timeValue
    }

So let's say global.timeRecord01 is 900, and global.timeValue is 742.
742 is less than 900, so it should change the 900 to 742. But it's not and I have no clue.

Edited by Mr Weird Guy, 28 April 2012 - 09:06 AM.

  • 0

#9 Mr Weird Guy

Mr Weird Guy

    GMC Member

  • New Member
  • 69 posts

Posted 30 April 2012 - 09:54 PM

Bump, first post has been updated.
  • 0

#10 Desert Dog

Desert Dog

    GMC Member

  • Global Moderators
  • 6409 posts
  • Version:Unknown

Posted 30 April 2012 - 10:32 PM

Mr Weird Guy,

Don't just say bump. Put the new details in that post!

Updating the OP makes things confusing. What where the earlier posts replying to? Question 1, or question 2?

Best keep things simple. ;)
  • 0

#11 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5417 posts
  • Version:GM:Studio

Posted 01 May 2012 - 12:08 AM

the timeRecord array was supposed to be instead of timeRecord01 ..

you can set all timerecords to 9999999, then you can drop the timeRecord01 variable:
for(i=0;i<10;i+=1)
  global.timeRecord[i] = 9999999;
that is the same as writing:
global.timeRecord[0] = 9999999;
global.timeRecord[1] = 9999999;
// ... and so on
global.timeRecord[9] = 9999999;

it shouldn't be nessesary to do what you do in scr_BossArrays .. because then you would have to make a script for each boss again, which destroys the purpose.. just write timeRecord[1] where you would write timeRecord01
  • 0

#12 Mr Weird Guy

Mr Weird Guy

    GMC Member

  • New Member
  • 69 posts

Posted 01 May 2012 - 05:00 AM

Wow, it was really that simple. I guess I was just overthinking it. Now that I finally understand arrays, I feel like a huge window has opened up for me.
Thanks, this topic's been a great help, even if it did make me look a little stupid. :V
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users