查看: 2825|回复: 5

[已经解决] 打印$game_party.actors[i].skills[j].id时

[复制链接]
梦石
0
星屑
273
在线时间
20 小时
注册时间
2018-4-6
回帖
12
发表于 2020-2-16 22:54:16 来自手机 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
显示的是数据库里对应技能ID的两倍加1,请教一下这中间的机理是什么
梦石
0
星屑
13562
在线时间
2753 小时
注册时间
2014-10-4
回帖
705

R考场第七期纪念奖

发表于 2020-2-17 00:50:21 | 显示全部楼层
这个和脚本本身没关系吧
确切的问题大概是 为什么 Fixnum 对象的 id 方法 返回其本身的两倍 + 1 这样
这个..ruby设计上就是这样子 别问我 我不知道
ruby-1.8.7-p302---gc.c--2083-2148
[pre lang="c"]/*
*  Document-method: __id__
*  Document-method: object_id
*
*  call-seq:
*     obj.__id__       => fixnum
*     obj.object_id    => fixnum
*
*  Returns an integer identifier for <i>obj</i>. The same number will
*  be returned on all calls to <code>id</code> for a given object, and
*  no two active objects will share an id.
*  <code>Object#object_id</code> is a different concept from the
*  <code>:name</code> notation, which returns the symbol id of
*  <code>name</code>. Replaces the deprecated <code>Object#id</code>.
*/

/*
*  call-seq:
*     obj.hash    => fixnum
*
*  Generates a <code>Fixnum</code> hash value for this object. This
*  function must have the property that <code>a.eql?(b)</code> implies
*  <code>a.hash == b.hash</code>. The hash value is used by class
*  <code>Hash</code>. Any hash value that exceeds the capacity of a
*  <code>Fixnum</code> will be truncated before being used.
*/

VALUE
rb_obj_id(VALUE obj)
{
    /*
     *                32-bit VALUE space
     *          MSB ------------------------ LSB
     *  false   00000000000000000000000000000000
     *  true    00000000000000000000000000000010
     *  nil     00000000000000000000000000000100
     *  undef   00000000000000000000000000000110
     *  symbol  ssssssssssssssssssssssss00001110
     *  object  oooooooooooooooooooooooooooooo00        = 0 (mod sizeof(RVALUE))
     *  fixnum  fffffffffffffffffffffffffffffff1
     *
     *                    object_id space
     *                                       LSB
     *  false   00000000000000000000000000000000
     *  true    00000000000000000000000000000010
     *  nil     00000000000000000000000000000100
     *  undef   00000000000000000000000000000110
     *  symbol   000SSSSSSSSSSSSSSSSSSSSSSSSSSS0        S...S % A = 4 (S...S = s...s * A + 4)
     *  object   oooooooooooooooooooooooooooooo0        o...o % A = 0
     *  fixnum  fffffffffffffffffffffffffffffff1        bignum if required
     *
     *  where A = sizeof(RVALUE)/4
     *
     *  sizeof(RVALUE) is
     *  20 if 32-bit, double is 4-byte aligned
     *  24 if 32-bit, double is 8-byte aligned
     *  40 if 64-bit
     */
    if (TYPE(obj) == T_SYMBOL) {
        return (SYM2ID(obj) * sizeof(RVALUE) + (4 << 2)) | FIXNUM_FLAG;
    }
    if (SPECIAL_CONST_P(obj)) {
        return LONG2NUM((long)obj);
    }
    return (VALUE)((long)obj|FIXNUM_FLAG);
}[/pre]
ruby内一切对象即VALUE 参考上面
TYPE(xx) 返回 xx 的类型
SYM2ID(xx) 将xx右移8位
FIXNUM_FLAG = 1
SPECIAL_CONST_P(xx) xx是否为false true nil undef symbol or fixnum
LONG2NUM(xx) 把c数据xx转换为ruby数值对象
RVALUE  同文件309行起
[pre lang="c"]#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
#pragma pack(push, 1) /* magic for reducing sizeof(RVALUE): 24 -> 20 */
#endif

typedef struct RVALUE {
    union {
        struct {
            unsigned long flags;        /* always 0 for freed obj */
            struct RVALUE *next;
        } free;
        struct RBasic  basic;
        struct RObject object;
        struct RClass  klass;
        struct RFloat  flonum;
        struct RString string;
        struct RArray  array;
        struct RRegexp regexp;
        struct RHash   hash;
        struct RData   data;
        struct RStruct rstruct;
        struct RBignum bignum;
        struct RFile   file;
        struct RNode   node;
        struct RMatch  match;
        struct RVarmap varmap;
        struct SCOPE   scope;
    } as;
#ifdef GC_DEBUG
    char *file;
    int   line;
#endif
} RVALUE;

#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
#pragma pack(pop)
#endif[/pre]
我对这个没研究过 不画蛇添足强行解释了

对 f 返回 2f+1 直接原因是VALUE被转换为ruby数值了
而Fixnum的储存方式又是 2f+1
比方对于1 VALUE = 1<<1 | 1 = 3 又把这个 3 转换为 3 << 1 | 1 = 7 返回
而这个 7 又被ruby解释为 7>>1 = 3...

深层的设计上原因
我并不知道 但我觉得这样设计挺简洁美的

评分

参与人数 1星屑 +20 收起 理由
RyanBern + 20 我很赞同

查看全部评分

回复

使用道具 举报

梦石
0
星屑
39236
在线时间
11155 小时
注册时间
2009-3-15
回帖
4768
发表于 2020-2-17 12:27:11 | 显示全部楼层
乘2+1是RMXP的数据在内存上的加密..
回复

使用道具 举报

梦石
0
星屑
9862
在线时间
580 小时
注册时间
2017-9-28
回帖
203
发表于 2020-2-17 15:26:33 | 显示全部楼层
是不是你这个角色携带的技能刚好是编号 1, 3, 5... 的技能

点评

这喵喵喵  发表于 2020-2-19 10:04
喵喵喵
回复

使用道具 举报

梦石
0
星屑
4881
在线时间
425 小时
注册时间
2019-10-22
回帖
647
发表于 2020-2-17 15:43:18 | 显示全部楼层
xp版老人就是多  一下炸出楼上3位lv4  恐怖如斯
















回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部