Author Topic: M&B Scripting Q&A  (Read 113085 times)

Offline Ahadhran

  • Mysterious Old Man
  • Craftsman
  • **
  • Posts: 137
    • View Profile
Re: M&B Scripting Q&A
« Reply #195 on: August 11, 2007, 12:16:32 pm »
I find it's good programming practice to zero variables before using them, since I'm not certain whether or not MBScript does any initialization beforehand.

As far as memory goes, I would think not. I would be surprised if that were the case, given the amounts of memory available on modern boxen.

I usually do zero variables before use, I just didn't think it would be necessary with local variables. Clearly I was wrong. Oh well, its a very simple solution to make everything work right.

Offline Winter

  • I am Tek Jansen!
  • Administrator
  • *****
  • Posts: 476
  • MBX Forum owner
    • View Profile
    • Street of Eyes: The Writing of Ryan A. Span
Re: M&B Scripting Q&A
« Reply #196 on: August 12, 2007, 11:14:39 am »
Always, ALWAYS zero your local variables before use. They use an array of data slots that is constantly re-used in a cyclic pattern -- basically, when an operations block is called, it assigns the local vars to this array of slots, and when the block is finished and the next block begins, the new block's locals will use the exact same slots used by the old block. This happens over and over and over, and Native scripts use locals extensively, so you can never know what leftover value a local var might end up containing unless you zero it first.

Offline Ahadhran

  • Mysterious Old Man
  • Craftsman
  • **
  • Posts: 137
    • View Profile
Re: M&B Scripting Q&A
« Reply #197 on: August 12, 2007, 04:03:24 pm »
Always, ALWAYS zero your local variables before use. They use an array of data slots that is constantly re-used in a cyclic pattern -- basically, when an operations block is called, it assigns the local vars to this array of slots, and when the block is finished and the next block begins, the new block's locals will use the exact same slots used by the old block. This happens over and over and over, and Native scripts use locals extensively, so you can never know what leftover value a local var might end up containing unless you zero it first.

Thank you for the clarification Winter, I now understand how it works! One other question I would be curious to know the answer to is: how many local variables can you use at once(within one operation block)? Its probably a very large number, but it might be worth knowing anyway.

Offline fujiwara

  • Master
  • *****
  • Posts: 858
    • View Profile
Re: M&B Scripting Q&A
« Reply #198 on: August 13, 2007, 11:36:11 am »
Always, ALWAYS zero your local variables before use. They use an array of data slots that is constantly re-used in a cyclic pattern -- basically, when an operations block is called, it assigns the local vars to this array of slots, and when the block is finished and the next block begins, the new block's locals will use the exact same slots used by the old block. This happens over and over and over, and Native scripts use locals extensively, so you can never know what leftover value a local var might end up containing unless you zero it first.

Interesting. Has anyone tried to exploit this 'carry-over' effect? Such that if I declare ':var1' in the first op block, then refer to ':var1' in the second block, is it guaranteed to store the same value as in the first block, or are locals assigned slots based on first-come-first-served?

The horses tend to get their legs caught in the catapult rather than being properly launched.

Offline Winter

  • I am Tek Jansen!
  • Administrator
  • *****
  • Posts: 476
  • MBX Forum owner
    • View Profile
    • Street of Eyes: The Writing of Ryan A. Span
Re: M&B Scripting Q&A
« Reply #199 on: August 16, 2007, 07:50:20 am »
Interesting. Has anyone tried to exploit this 'carry-over' effect? Such that if I declare ':var1' in the first op block, then refer to ':var1' in the second block, is it guaranteed to store the same value as in the first block, or are locals assigned slots based on first-come-first-served?

No, it's not guaranteed to store the same value, I'm afraid. Otherwise why would you need script params at all?

Marcus

  • Guest
Re: M&B Scripting Q&A
« Reply #200 on: October 07, 2007, 11:43:58 am »
Ok, just a quick question, how would I use a dialog to make the player join a faction depending on what choice he'/she makes? You could also point me to the code that makes you join a faction once you've spoken to the king.

Offline Fisheye

  • Blues Brother
  • Moderator
  • *****
  • Posts: 427
  • My scalp's so bright, I gotta wear shades.
    • View Profile
Re: M&B Scripting Q&A
« Reply #201 on: October 07, 2007, 11:56:57 am »
script_player_join_faction

search for that in module_dialogs to see when it is used.

Marcus

  • Guest
Re: M&B Scripting Q&A
« Reply #202 on: October 07, 2007, 12:48:50 pm »
Seen it, what bit of code activates the quest to find the lord and swear allegiance?

Offline Fisheye

  • Blues Brother
  • Moderator
  • *****
  • Posts: 427
  • My scalp's so bright, I gotta wear shades.
    • View Profile
Re: M&B Scripting Q&A
« Reply #203 on: October 07, 2007, 12:50:58 pm »
Search for:

# Offer player to join faction

in module_simple_triggers

Marcus

  • Guest
Re: M&B Scripting Q&A
« Reply #204 on: October 07, 2007, 12:55:50 pm »
Faster than lightening you are. ;)

Because that's a randomised menu event as such, I'm not sure how I would implement it to a dialog to offer to join the swadians, any ideas?
« Last Edit: October 07, 2007, 12:57:51 pm by Marcus »

grailknighthero

  • Guest
Re: M&B Scripting Q&A
« Reply #205 on: October 16, 2007, 08:12:59 am »
Faster than lightening you are. ;)

Because that's a randomised menu event as such, I'm not sure how I would implement it to a dialog to offer to join the swadians, any ideas?
Take out the simple trigger or modify it.  I have heavily modified it for my mod.

Offline LCJr

  • Journeyman
  • ***
  • Posts: 219
    • View Profile
Re: M&B Scripting Q&A
« Reply #206 on: October 17, 2007, 06:57:43 pm »
Is "mission_time" measured in seconds?

For example:
Code: [Select]
(try_begin), # charge everyone after a while
        (eq, "$everyone_charge_order_given", 0),
        (ge,":mission_time",300),
        (assign, "$everyone_charge_order_given", 1),
        (team_give_order, "$ai_team", grc_everyone, mordr_charge),
        (team_get_leader, ":ai_leader", "$ai_team"),
        (agent_set_speed_limit, ":ai_leader", 60),
      (try_end),

grailknighthero

  • Guest
Re: M&B Scripting Q&A
« Reply #207 on: October 17, 2007, 11:20:47 pm »
Is "mission_time" measured in seconds?

For example:
Code: [Select]
(try_begin), # charge everyone after a while
        (eq, "$everyone_charge_order_given", 0),
        (ge,":mission_time",300),
        (assign, "$everyone_charge_order_given", 1),
        (team_give_order, "$ai_team", grc_everyone, mordr_charge),
        (team_get_leader, ":ai_leader", "$ai_team"),
        (agent_set_speed_limit, ":ai_leader", 60),
      (try_end),

It was in .808, I havent messed around with it in .89x but I assume it hasn't changed.

Highlander

  • Guest
Re: M&B Scripting Q&A
« Reply #208 on: October 30, 2007, 08:51:39 am »
Does anyone know how sqrt works?
 (store_sqrt, <destination_fixed_point>, <value_fixed_point>),
I have no idea about the fixed point stuff.

Offline Fisheye

  • Blues Brother
  • Moderator
  • *****
  • Posts: 427
  • My scalp's so bright, I gotta wear shades.
    • View Profile
Re: M&B Scripting Q&A
« Reply #209 on: October 30, 2007, 09:17:03 am »
Does anyone know how sqrt works?
 (store_sqrt, <destination_fixed_point>, <value_fixed_point>),
I have no idea about the fixed point stuff.

http://mbx.streetofeyes.com/index.php/topic,956.0.html