• 🏆 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] What did i do wrong?

Status
Not open for further replies.
Level 8
Joined
Jul 28, 2008
Messages
211
I'm trying to learn to use structs and things like that. I came up with this:

JASS:
struct lvl
     local real damage
    method level takes integer level returns real
     if level == 1 then
     set damage = 50
      elseif level == 2 then
      set damage = 75
       else
       set damage = 125
    endif
     return damage
 endmethod
endstruct

When i check it, it reports a FEW problems. So can someone help me?
Thx! :wink:
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
method level takes integer level?

Also, struct members should be preceded with a dot when referenced inside the struct.
In your example - ".damage". This shortens "this.damage", which you can also write if you want.

Also do notice that this struct is useless.
 
Level 4
Joined
Nov 23, 2007
Messages
113
You can move the "local real damage" declaration inside the method. However, the struct only exists to return a calculated value through a method call, so using a simple function would be more appropriate unless you intend on adding more code to that struct or modifying struct members.

If what you wanted to do was declare "damage" as a member of the struct, you don't need to use the "local" keyword.

And as pointed out, you should use different names for methods/functions and their parameters and other vars. For example:

method GetDamage takes integer level returns real
 
Level 3
Joined
Aug 9, 2008
Messages
60
JASS:
struct lvl
    real damage
    method level takes integer l returns real
        if l == 1 then
            set .damage = 50
        elseif l == 2 then
            set .damage = 75
        else
            set .damage = 125
        endif
        return .damage
    endmethod
endstruct

That's your struct in the correct form, but as someone mentioned earlier, this struct is completely useless.
 
Status
Not open for further replies.
Top