Main Menu
Daily Lucky Numbers:
6
15
24
30
33
47

New stat

Started by battler, November 11, 2014, 08:51:34 PM

Previous topic - Next topic

battler

Hello, I was trying to make a new variable that every member has (like atk, def, points, etc...) but to be honest I have no idea how to do it. I'm going to use it for classes that a user can pick from that gives stat changes, cosmetic changes and some other things but the one part I'm having a huge amount of trouble with is actually giving users a new variable that I can work with (and check for in battle_opps, battle_template, etc...). I've tried putting it in the arrays but it always gets set to 0 (probably because of the isset). I think variables need to be installed in the database with the actual battle game but I'm pretty lost. Anyone have any idea of how I can do this?

Chen Zhen

battler,

  If you want the installer to add a column to the members table then do the following:


  • Unzip the mod's archive
  • Edit file: installScriptX.php (see example below)
  • Compress files anew (as zip or gzip)
  • Uninstall and then delete current Battle version within the SMF installer
  • Install your modified version

File: installScriptX.php
find:

$smcFunc['db_add_column']('{db_prefix}members',
array(
'name' => 'atk',
'type' => 'int',
        'size' => 10,
        'unsigned' => true,
        'null' => false,
'default' => '100',
),
array(),
false
);


Add your changes prior to the above code. You can just copy what is shown and then edit your specifics. I am not sure if you want an integer or string because you did not specify this in your request. An example of adding another integer column to the members table is shown below:


$smcFunc['db_add_column']('{db_prefix}members',
array(
'name' => 'battle_class_x',
'type' => 'int',
        'size' => 10,
        'unsigned' => true,
        'null' => false,
'default' => '0',
),
array(),
false
);


.. or perhaps a standard string?

$smcFunc['db_add_column']('{db_prefix}members',
array(
'name' => 'battle_class_y',
'type' => 'varchar',
        'size' => 255,
        'unsigned' => true,
        'null' => false,
),
array(),
false
);


Regards.

battler

First, thanks for the reply!

I managed to add the variable to the database but I'm having trouble updating it. I added it as a string and added the following codes in load.php:

In the user_info array:
'class' => isset($user_settings['class']) ? $user_settings['class'] : '',

and in the memberContext[$user] array (although I'm not entirely sure what this array does):
'class' => $profile['class'],

I displayed the class on the game so I could check what the variable was at when I tried changing it and it starts at 0. Whenever I try setting it to anything (by using something like: $user_info['class'] = 'something'; ) within the game it will reset back to 0. I was wondering why that's happening and what other lines of code I have to add so I can update the variable.

Thanks again for your help!

Chen Zhen


$user_info['class'] = 'something';


This is a string. Did you set up the installer with my second example for a string (varchar)?
If you set it as an integer (int) it will show 0 when you attempt to write a string to it.
Most likely you will also have errors in your log regarding improper type - integer expected.

battler

Yes, I did set it up as a string using the second code you provided. There's also no errors in the error log. I'll post a picture of what happens when I try to update the string:





Right now I have it set so if you win a battle it updates class to 'something' just so I can test it. However, the string is defaulted to 0 for some reason and whenever I try to update it, it resets to 0. The codes I put in the last post were the only other lines I added. Is there other lines of code I have to add somewhere else I forgot?

Chen Zhen

#5
battler,

  So far from what I gather you have only written code to gather that info from the member table.
If you want to write data to it for that new column you created during battling monsters, you will have to edit the explore_custom() function located in the file of ../Sources/Subs-Battle.php

Look for where it executes the function of updateMemberData and add to that array where necessary.
This is what writes data to the member table.
 
ie.

updateMemberData($user_info['id'], array($row['outcome2_action'] => $user_info[$row['outcome2_action']], $modSettings['bcash'] => $user_info[$modSettings['bcash']], 'class' => 'something'));

battler

It's still not working. I think I'm missing a lot of code. Perhaps it would be easier starting from the beginning.

After I add in installscriptX.php:

$smcFunc['db_add_column']('{db_prefix}members',
array(
'name' => 'battle_class_y',
'type' => 'varchar',
        'size' => 255,
        'unsigned' => true,
        'null' => false,
),
array(),
false
);


what else do I need to add to make the user have the new variable? (In load.php, etc...)

battler

I actually got it working finally. The problem was that even though I made the variable a string in installscriptx.php, I made it an integer earlier and forgot about it so it was stuck in the database as an integer. After I found that out, I deleted it and reinstalled it as a string and putting it in the UpdateMemberData array worked.

Thanks for all the help!

Chen Zhen

battler,

  Yes when you said it was defaulting to 0 it was behaving like an integer column which is why I had thought you might have done that.
I am glad to hear you have figured things out.

Regards.