找回密码
 点一下
查看: 2863|回复: 3

一个关于FirstOfGroup()的测试

[复制链接]
发表于 2007-7-9 20:56:50 | 显示全部楼层 |阅读模式
我做了一个针对loop/ FirstOfGroup() / GroupRemoveUnit() / endloop的测试,目的是了解每做一次这样的循环,一个组中单位的排列变化。

准备工作:
首先我们需要一些全局变量,还需要一个能返回某单位的内存位置的函数:
  1. // testgroup 是用于测试的组。
  2. globals
  3.     public group testgroup = CreateGroup()
  4.     public integer num = 6
  5. endglobals
  6. // u2i 函数,返回某单位的内存整数地址。
  7.     function u2i takes unit u returns integer
  8.         return u
  9.         return 0
  10.     endfunction
复制代码

然后我做了几个自定义函数,分别用于在游戏中显示,添加,或删除组中的单位:
  1.     function Showgroupunit takes nothing returns nothing
  2.         local string message
  3.         local group g = CreateGroup()
  4.         local unit u
  5.         call GroupAddGroup( testgroup, g )
  6.         loop
  7.             set u = FirstOfGroup( g )
  8.             exitwhen u == null
  9.             set message = I2S(u2i(u))
  10.             call BJDebugMsg( message )
  11.             call TriggerSleepAction( 1.0 )
  12.             call GroupRemoveUnit( g, u )
  13.         endloop
  14.         call DestroyGroup( g )
  15.         set g = null
  16.         set message = null
  17.     endfunction
  18.    
  19.     function Addunit takes nothing returns nothing
  20.         local unit u = CreateUnit( Player(0), 'hpea', GetStartLocationX( GetPlayerStartLocation( Player(0))), GetStartLocationY( GetPlayerStartLocation( Player(0))), 0.0 )
  21.         call GroupAddUnit( testgroup, u )
  22.         set u = null
  23.     endfunction
  24.    
  25.     function Removegroupunit takes nothing returns nothing
  26.         local group g = CreateGroup()
  27.         local unit u
  28.         call GroupAddGroup( testgroup, g )
  29.         set u = FirstOfGroup( g )
  30.         call GroupRemoveUnit( g, u )
  31.         call DestroyGroup( g )
  32.         set u = null
  33.         set g = null
  34.     endfunction
复制代码

接着我添加了以下触发,用以在游戏中通过输入聊天信息来触发以上的函数:
  1. Show
  2.     Events
  3.         Player - Player 1 (Red) types a chat message containing start as An exact match
  4.     Conditions
  5.     Actions
  6.         Custom script:   call Showgroupunit()
  7. Add
  8.     Events
  9.         Player - Player 1 (Red) types a chat message containing add as An exact match
  10.     Conditions
  11.     Actions
  12.         Custom script:   call Addunit()
  13. Remove
  14.     Events
  15.         Player - Player 1 (Red) types a chat message containing remove as An exact match
  16.     Conditions
  17.     Actions
  18.         Custom script:   call Removegroupunit()
复制代码

测试开始了,在游戏中,我首先输入6次 ”add”,然后输入”start”,就看到以下信息:

1048680
1048681
1048682
1048683
1048684
1048685

分别是组内6个单位在内存中的整数地址,然后无论我输入多少次”start”,这六个数字都按照同样的排列方式显示。

紧接着我又做了另一个测试,分别加上一个自定义函数,并且在地图初始化部分加载之:
  1.     function Setuptestgroup takes nothing returns nothing
  2.         local unit u
  3.         local integer f = 0
  4.         loop
  5.             exitwhen f > num
  6.             set u = CreateUnit( Player(0), 'hpea', GetStartLocationX( GetPlayerStartLocation( Player(0))), GetStartLocationY( GetPlayerStartLocation( Player(0))), 0.0 )
  7.             call GroupAddUnit( testgroup, u )
  8.             set u = null
  9.             set f = f + 1
  10.         endloop
  11.         set u = null
  12.     endfunction
复制代码
触发:
  1. Melee Initialization
  2.     Events
  3.         Map initialization
  4.     Conditions
  5.     Actions
  6.         Visibility - Create an initially Enabled visibility modifier for Player 1 (Red) emitting Visibility across (Playable map area)
  7.         Custom script:   call Setuptestgroup()
复制代码

然后我用类似的方法进行测试,先键入两次”add”,然后”start”:

1048687 // 第一个新加入的单位
1048679
1048680
1048681
1048682
1048683
1048684
1048688 // 第二个新加入的单位

我们可以看到,第一个加入的单位被放在了最前面,而从第二个开始,就会被放在最后。之后无论我再输入多少次”add”,新的单位都排在后面。

结论:
对于一个游戏中相同的组,每次当我们用loop/ FirstOfGroup() /call GroupRemoveUnit(), endloop来选取组中每个单位的时候,每次选取的顺序都会是相同的。

但是如果是游戏初始化创建的组,我们在游戏中再加入单位,第一个加入的单位会排在首位,其余的则放在最后。
发表于 2007-7-10 02:20:14 | 显示全部楼层
偶强大的兔子
回复

使用道具 举报

发表于 2007-7-10 02:25:09 | 显示全部楼层
对于最后的结论表示神奇~
回复

使用道具 举报

发表于 2007-8-14 15:00:24 | 显示全部楼层
用途不大 结论可嘉
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 点一下

本版积分规则

Archiver|移动端|小黑屋|地精研究院

GMT+8, 2024-5-22 13:22 , Processed in 0.049292 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表