Learning how to script basics - Problem with basic script

Hi! I am trying to develop some basic commands I can't do using the RPGVXA engine so resorting to scripting on my own. I've never made a script before, am new at this, trying to learn and could use some help please, in the form of answering and pointing out problems with some basic script commands i am trying out.

I just tried to do a basic conditional command in rpgvxa using events command -> advanced -> script

I simply wanted to tell the game to, 

if party has item 33,

party lose item 33 by 1 quantity count,

else

party gain item 33 by 1 quantity count,

end

here's the code i input

when result = $game_party.has_item?($data_items[33[1]]) $game_party.lose_item($data_items[33], 1)else $game_party.gain_item($data_items[33], 1)endI received script game interpreter line 1411 syntax error occurred. Something wrong with my first line of code. 

What did I do wrong? T_T
1) the ruby command is IF, not when

2) = is an assignment operator, for comparisons you'll need ==, but in this case you don't even need the result, just make it a direct check

Scripting in RM is basically programming in Ruby, and you need to learn Ruby before you can continue to more complex parts, especially if you have no previous experience in program syntax and logics. I suggest working through some of the tutorials linked to learning Ruby in the forum.
1) the ruby command is IF, not when

2) = is an assignment operator, for comparisons you'll need ==

Scripting in RM is basically programming in Ruby, and you need to learn Ruby before you can continue to more complex parts, especially if you have no previous experience in program syntax and logics. I suggest working through some of the tutorials linked to learning Ruby in the forum.
Thank you for helping!

Will definitely try to spend spare time when I can to learn more about Ruby. 
You can just change that to

if $game_party.has_item ...

you don't need to assign the result to a temporary variable (which you never use).

So ...

Code:
if $game_party.has_item?($data_items[33[1]])    $game_party.lose_item($data_items[33], 1)else     $game_party.gain_item($data_items[33], 1)end
or perhaps...

(p=$game_party)::gain_item(i=$data_items[33], p::has_item?(i) ? -1 : 1)edit:

also, its likely that your error was caued by this -> '$data_items[33[1]]' -- using the [1] like you have there, you are getting the bit index 1 from the binary form of the number 33. this binary bit is then being passed to has_item?, which of course is going to throw an error because binary numbers do not reside within containers like items weapons and armors data does. 🙂
you can just change that to

if $game_party.has_item ...

you don't need to assign the result to a temporary variable (which you never use).

So ...

if $game_party.has_item?($data_items[33[1]]) $game_party.lose_item($data_items[33], 1)else $game_party.gain_item($data_items[33], 1)end
When I tried this, changing to:

if $game_party.has_item?($data_items[33]) $game_party.lose_item($data_items[33], 1)else $game_party.gain_item($data_items[33], 1)endThe script totally failed to work.

My original version below does works.

if result = $game_party.has_item?($data_items[33[1]]) $game_party.lose_item($data_items[33], 1)else $game_party.gain_item($data_items[33], 1)endHow should I edit it so it simply checks if the item exist, without need to check how many of the items there are?
How should I edit it so it simply checks if the item exist, without need to check how many of the items there are?
the game party has_item? method does not check how many of the item there is..

also - you might want to double check that your original version 'works'.. cause its pretty much always going to return the same thing - due to you checking 33[1], which as i mentioned before, is performing binary calculations.

to give you more details...

you are calling has_item, and passing it the value of $data_items[33[1]].. as mentioned, 33[1] is getting the bit contained within the bit index 1 of the binary form of 33, which turns out to be the number 0.. So, you are basically checking if the party has item 0, which they will obviously never have, so its likely that your original version 'working', is actually not, and its always going to pass to the else clause.
the game party has_item? method does not check how many of the item there is..
I took that piece of code directly from game interpreter's conditional branch. It reads:

result = $game_party.has_item?($data_items[@params[1]])what does @params and the [1] inside the bracket stand for or refer to?

I'm guessing params refer to paremeter = the game item's id number, I'm guessing [1] means checking if there is at least a unit of 1 of that item? Thereby confirming the item exists in party inventory if at least 1 of the item exists. Correct?
@params is an array of data, where index 1 of said array is an integer value containing the events gain item's selected item index - ie, the value 1.

so '$data_items[@params[1]]' is checking the @params array index 1, which contains a single integer for the id, then its using said integer to get the item data for the $data_items at that index, then sending that item to has item to perform the check. 🙂
And to add a bit more info: the params array usually contains the data transferred from the event command window to the interpreter - that is the numbers and settings that you give the command, and therefore is different for different event commands.
Did even more testing, the previous version actually did not work correctly. It gave me +1 to the item even when I don't have it. Tweaked the code a little and now I confirm this below version is the correct one to use.

if result = $game_party.has_item?($data_items[33]) $game_party.lose_item($data_items[33], 1)else $game_party.gain_item($data_items[33], 10)endI tweaked the game to give 10 if party does not have item to get more accurate test results =)
A one-liner version of your code:

Code:
(p=$game_party)::gain_item(i=$data_items[33], p::has_item?(i) ? -1 : 1)
Need further help please. Working further on the script:

if result = $game_party.has_item?($data_items[33]) if $game_party.item_number($data_items[33]) >= 15 - ($data_items[34]) $game_party.gain_item($data_items[34]) = 15 - ($data_items[34]) else end elseend

Getting syntax error. 

Unexpected '=', expecting keyword _end

it referred to this line: $game_party.gain_item($data_items[34]) = 15 - ($data_items[34])

Please heeeelp ;(
bump
red-codec, please do not bump your topic unless it has been 72 hours later since your last post. You can review our forum rules here. Thank you.

You have to take into account that people are in different time zones, have different work/leisure schedules and that everyone who answers here is doing so in their free time and have other commitments as well.  You therefore need to be a bit patient.  That 72 hours buffer is there for a good reason.
if result = $game_party.has_item?($data_items[33]) if $game_party.item_number($data_items[33]) >= 15 - ($data_items[34]) $game_party.gain_item($data_items[34]) = 15 - ($data_items[34]) else end elseend
All of this is just wrong on so many levels...

the line with the result in it -> if result = $game_party.has_item?($data_items[33]), can be changed completely, which shaz mentioned posts and posts ago.., the 'result =' is doing absolutely nothing in your code, and can be removed for optimization, like so...

if $game_party.has_item?($data_items[33]) # Do Other Stuff:endSecondly, what the hell are you trying to achieve with this line -> '15 - ($data_items[34])' ??

I would assume that you are trying to do 15 - the current number of item 34, which is completely different from $data_items[34], if 15 - $data_items is not causing an error, thats surprising, but its certainly not going to work as intended either..

If that is what you are trying to do, this would be the best approach:

numberhas = $game_party.item_number($data_items[34])if $game_party.item_number($data_items[33])>= (15 - numberhas) # Do stuff:end Finally ->  $game_party.gain_item($data_items[34]) = 15 - ($data_items[34])

This code is just wrong on so many levels..

For a start, you are not passing a quantity value into the gain_item method... secondly, you are then trying to set the value returned from gain items to the value of 15 - $data_items[34], which just wont happen..

I honestly have no idea what you are trying to achieve, so more information would be helpful.
@■ ᴆᴈᴋᴉᴦᴀ ■

Thanks for replying back. Sorry for not making it clear enough. I am trying to do this:

If game party has data item 33

(AND) If number of data item 33 is equal or greater than ( 15 – number of data item 34 party has )

Do this:

party gains number of data item 34 equal to (15 – number of data item 34 party has )

(AND) party loses number of data item 33 equal to (15 – number of data item 34 party has )

else

if number of data item 33 is less than ( 15 – number of data item 34 party has ) (automatically implied)

Do this:

party gains number of data item 34 equal to number of data item 33 player has

else

end
Really need and appreciate the help please.

Meanwhile going to study what you wrote and see if I can fit that all in and make it all work. :/

Updated:

Secondly, what the hell are you trying to achieve with this line -> '15 - ($data_items[34])' ??

I would assume that you are trying to do 15 - the current number of item 34, which is completely different from $data_items[34], if 15 - $data_items is not causing an error, thats surprising, but its certainly not going to work as intended either..

If that is what you are trying to do, this would be the best approach:

numberhas = $game_party.item_number($data_items[34])
if $game_party.item_number($data_items[33])>= (15 - numberhas)
# Do stuff:
end 
As you said yes that is what I am trying to do. 

Finally ->  $game_party.gain_item($data_items[34]) = 15 - ($data_items[34])

This code is just wrong on so many levels..

For a start, you are not passing a quantity value into the gain_item method... secondly, you are then trying to set the value returned from gain items to the value of 15 - $data_items[34], which just wont happen..
Yea you are right. Could you please tell me what I should write instead in order to get what I intend to work?
like this?

Code:
num_item33 = $game_party::item_number(item33 = $data_items[33])num_item34 = $game_party::item_number(item34 = $data_items[34])newitemnum = [15 - num_item34, 0]::maxif (num_item33 >= newitemnum)   $game_party::lose_item(item33, newitemnum)  $game_party::gain_item(item34, newitemnum)else  $game_party::gain_item(item34, num_item33)end
like this?

num_item33 = $game_party::item_number(item33 = $data_items[33])num_item34 = $game_party::item_number(item34 = $data_items[34])newitemnum = [15 - num_item34, 0]::maxif (num_item33 >= newitemnum) $game_party::lose_item(item33, newitemnum) $game_party::gain_item(item34, newitemnum)else $game_party::gain_item(item34, num_item33)end
Exactly yes! It has helped alot thank you!

I got a new minor problem. I am trying to add in another conditional that checks:

if number of item 34 = 15
Actual code I input is:

if (num_item34 = 15)I think the game completely ignores this line of code, either this code is wrong or my structure of the entire code is wrong. Could you please tell me if this single line of code has anything wrong with it? Is it sound?
if (num_item34 = 15)
As said above (a lot of posts above), = is the assignment operator - for comparisons you need ==The correct code would be

Code:
if num_item34 == 15

Original thread: https://forums.rpgmakerweb.com/threads/learning-how-to-script-basics-problem-with-basic-script.43999/