• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] random number problem...

Status
Not open for further replies.
Level 4
Joined
Aug 11, 2005
Messages
101
ok well im just learning how to code JASS and i have a question. basically i made a local string array called f that has like values for 0 1 and 2 and they all equal a different word. anyway i made it so that it messages all players that "you bought a "+f[GetRandomInt(0,2) ] anyway it all works fine and dandy but how can i call up the random number that it picked? like i said i just started learning JASS and i dont know all the functions yet so help would be appreciated. what this trigger is supposed to do is pick a random fruit(f) and than tell you how much it costs f is the fruit and g is the cost:
Code:
function fruit takes nothing returns nothing
 local string array f
 local integer array g
    set f[0]=" Apple "
    set f[1]=" Orange "
    set f[2]=" Banana "
    set g[0]= 0.50
    set g[1]= 0.60
    set g[2]= 0.45
    call DisplayTextToForce( GetPlayersAll(), "You bought a "+f[GetRandomInt(0,2)] )
is what i have so far and i want it to say to the player after that "This was "+g[the random number that was created for f] hope that wasnt too confusing XD
 
Level 2
Joined
Sep 21, 2005
Messages
27
Well, if you're coding stuff for this map(and not to be portable), then there's a couple ways you can do it. If you have to find it out later...like for a player to accept, then you have to store it in something non-local.

I'd probably use a global variable, but I'm a JASS n00b myself. One thing to consider though...if you have lots of global variables for things then it can get messy very quickly. You might want to just create an array called something like "TownData" that stores ints that reflect every single state of whatever in that town. Whether it's what fruit you tried to purchase, or which of the 20 people you have talked to(and how far you are on your quests), it can greatly cleanup your readability, although you have to remember or mark down which array slots point where.

Assuming a Global Int "FruitAskedFor" exists....(create in trigger editor)
JASS:
function fruit takes nothing returns nothing
local string array f
local real array g

set udg_FruitAskedFor = GetRandomInt(0, 2)
    set f[0]=" Apple "
    set f[1]=" Orange "
    set f[2]=" Banana "
    set g[0]= 0.50
    set g[1]= 0.60
    set g[2]= 0.45
 call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "You bought a "+f[udg_FruitAskedFor]+".")
endfunction

Ints don't allow decimals. GetLocalPlayer() should still display to all players unless you check if GetLocalPlayer() equals the player that asked for fruit.
 
Level 4
Joined
Aug 11, 2005
Messages
101
ok well i figured it out thank you. i also changed g to a real array so that i can display decimals. but now i want to figure out how to add two variables this is what that script is now:
JASS:
function Trig_fruit_Actions takes nothing returns nothing
 local string array f
 local real array g
 local integer h
 local integer i
 local integer j
    set f[0]=" Apple "
    set f[1]=" Orange "
    set f[2]=" Banana "
    set g[0]= 0.50 
    set g[1]= 0.60 
    set g[2]= 0.45 
    set h= GetRandomInt(0,2)
    set i= GetRandomInt(0,2)
    call DisplayTextToForce( GetPlayersAll(), "You bought a "+f[h],"and a "+f[i]  )
    set j= g[h]+g[i]
    call DisplayTextToForce( GetPlayersAll(), "This costed you $"+I2S(j)  )
endfunction
//===========================================================================
function InitTrig_fruit takes nothing returns nothing
    set gg_trg_fruit = CreateTrigger(  )    
    call TriggerRegisterPlayerEventEndCinematic( gg_trg_fruit, Player(0) )
    call TriggerAddAction( gg_trg_fruit, function Trig_fruit_Actions )
endfunction
where it sais set j= g[h]+g i want it to add those two variables together but whenever i try to do that i get an error for Too many arguements.
 
Level 2
Joined
Sep 21, 2005
Messages
27
I don't know. If you're sure its that line, then try:
JASS:
set j = (g[h]+g[i])
 
Level 2
Joined
Sep 21, 2005
Messages
27
Ooh, fascinating. I wasn't aware JASS couldn't auto-convert two reals to an integer. Glad I could help though. :D
 
Status
Not open for further replies.
Top