PDA

View Full Version : Help w/ script


Mike_1990
Apr 25, 2018, 12:09 PM
Could somebody skillful in scripting please help me with a scrip I'm working on?

I was thinking of making a shop in my levels, where you can browse items by pressing left and right. So I made a code that reads the input and stores integer depending on input and what item you're on (or so I thought). The problem is, as it is now, it cycles through items (just text right now) randomly. I know it's probably some stupid mistake but I'm not a programmer so...

Here's the code:

int shop_item;
void onPlayerInput(jjPLAYER@ player){
if (((player.yPos >= 42*32) && (player.yPos <= 44*32)) and ((player.xPos >= 146*32) && (player.xPos <= 148*32))){
if (jjKey[0x27] == true){
switch (shop_item){
case 0:
jjPlayers[32].showText("@@@@@Item1", STRING::SMALL);
shop_item = 1;
break;
case 1:
jjPlayers[32].showText("@@@@@Item2", STRING::SMALL);
shop_item = 2;
break;
case 2:
jjPlayers[32].showText("@@@@@Item3", STRING::SMALL);
shop_item = 3;
break;
case 3:
jjPlayers[32].showText("@@@@@Item4", STRING::SMALL);
shop_item = 4;
break;
case 4:
jjPlayers[32].showText("@@@@@Item5", STRING::SMALL);
shop_item = 5;
break;
case 5:
jjPlayers[32].showText("@@@@@Item1", STRING::SMALL);
shop_item = 1;
break;
}
}
if (jjKey[0x25] == true){
switch (shop_item){
case 0:
jjPlayers[32].showText("@@@@@Item1", STRING::SMALL);
shop_item = 1;
break;
case 1:
jjPlayers[32].showText("@@@@@Item5", STRING::SMALL);
shop_item = 5;
break;
case 2:
jjPlayers[32].showText("@@@@@Item1", STRING::SMALL);
shop_item = 1;
break;
case 3:
jjPlayers[32].showText("@@@@@Item2", STRING::SMALL);
shop_item = 2;
break;
case 4:
jjPlayers[32].showText("@@@@@Item3", STRING::SMALL);
shop_item = 3;
break;
case 5:
jjPlayers[32].showText("@@@@@Item4", STRING::SMALL);
shop_item = 4;
break;
}
}
}
}

Violet CLM
Apr 25, 2018, 06:38 PM
Looking good so far! Your problem is that <code>jjKey[0x27]</code> (and <code>jjKey[0x25]</code>) is <code>true</code> not only when the key on the keyboard is <em>first</em> pressed, but <em>as long as it is pressed</em>. Unless you can manage to touch the keyboard key for only a seventieth of a second, your script will change the value of <code>shop_item</code> more than once.

Mike_1990
Apr 25, 2018, 07:39 PM
I see... The function seemed a little too good to be true :-) I'll try to figure out some way how to limit recognition of input so it will make just one change.

Mike_1990
Apr 26, 2018, 08:17 AM
Alright, got it now. Basically I've just added bool stop_select = true at the end of each case and if (stop_select != true) to the input conditions and then created another condition:

if ((jjKey[0x27] != true) and (jjKey[0x25] != true)){
stop_select = false;
}

So it stops reading input after first change and restores reading input as soon as player depresses left and right.

anyway thanks for reminding me how input function works.

Violet CLM
Apr 26, 2018, 08:55 AM
Glad to hear it's working!